Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Save system?

Discussion in 'Game Foundation' started by Hazsha, May 5, 2021.

  1. Hazsha

    Hazsha

    Joined:
    Aug 23, 2019
    Posts:
    4
    Saving a game state is also a common game system then why game foundation not provide this?
     
  2. erika_d

    erika_d

    Unity Technologies

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @Hazsha,

    Can you be more specific about what kind of game state you want to save? Game Foundation offers two officially supported data layer types right now: Memory Data Layer, and Persistence Data Layer. Persistence Data Layer allows you to save Game Foundation instance data locally to the player's device. There is additionally a sample scene that can be downloaded from Package Manager demonstrating how to use a provided ChilliConnect Data Layer to save data to ChilliConnect's servers for remote persistence. The intention is to add an officially supported data layer for saving game state to a server, though it likely will take a different form from the ChilliConnect one in the sample scene.

    I hope that helps!
     
  3. Carrmichaelll

    Carrmichaelll

    Joined:
    Nov 30, 2016
    Posts:
    68
    Hi, i would like to know if I could possibly use Easy save on the asset store to save and load game foundation states as their system provides more security with their encryption algorithm. besides saving and loading from the server, the game foundation save system seems very unsecure.

    regards.
     
  4. richj_unity

    richj_unity

    Unity Technologies

    Joined:
    Sep 23, 2019
    Posts:
    40
    Hi Carrmichaelll,

    Game Foundation doesn't have a provider for Easy Save built in, but a custom provider could be developed and used in place of Game Foundation's built-in LocalPersistence class. You would create a new class that implements Game Foundation's IDataPersistence interface, which has just Save and Load methods. In those methods you would write the conversion between a GameFoundationData object and the format that Easy Save can work with. Then when you're initializing Game Foundation, it would look like this:
    Code (CSharp):
    1. myEasySaveDataLayer = new PersistenceDataLayer(new EasySavePersistence());
    2. GameFoundationSdk.Initialize(myEasySaveDataLayer);
     
    Carrmichaelll and erika_d like this.
  5. DenariiGames

    DenariiGames

    Joined:
    Dec 29, 2019
    Posts:
    13
    Trying to implement my own data layer and running into issue. In my game manager, I have:

    Code (CSharp):
    1.         IEnumerator Start()
    2.         {
    3.             // MemoryDataLayer dataLayer = new MemoryDataLayer();
    4.             PersistenceDataLayer dataLayer = new PersistenceDataLayer(new SampleDataLayer());
    5.             using (Deferred initDeferred = GameFoundationSdk.Initialize(dataLayer))
    6.             {
    7.                 yield return initDeferred.Wait();
    8.  
    9.                 if (initDeferred.isFulfilled)
    10.                     OnInitSucceeded();
    11.                 else
    12.                     OnInitFailed(initDeferred.error);
    13.             }
    14.         }
    15.  
    16.  
    And then in the SampleDataLayer, I am trying to return an empty inventory on load like so.

    Code (CSharp):
    1.     using System;
    2.     using UnityEngine;
    3.     using UnityEngine.GameFoundation.Data;
    4.     using UnityEngine.GameFoundation.DefaultLayers.Persistence;
    5.     using Newtonsoft.Json;
    6.  
    7.     public class SampleDataLayer : IDataPersistence
    8.     {
    9.         public void Save(GameFoundationData content, Action onSaveCompleted = null, Action<Exception> onSaveFailed = null)
    10.         {
    11.             string jsonString = JsonConvert.SerializeObject(content);
    12.             Debug.Log(jsonString);
    13.         }
    14.  
    15.         public void Load(Action<GameFoundationData> onLoadCompleted = null, Action<Exception> onLoadFailed = null)
    16.         {
    17.             onLoadCompleted = JsonConvert.DeserializeObject("{\"inventoryManagerData\":{\"items\":[],\"itemLists\":[],\"itemMaps\":[]},\"walletData\":{\"balances\":[]},\"rewardManagerData\":{\"rewards\":[]},\"version\":0}") as Action<GameFoundationData>;
    18.         }
    19.  
     
    erika_d likes this.
  6. kevinmkchin

    kevinmkchin

    Unity Technologies

    Joined:
    May 17, 2021
    Posts:
    2
    Hi @DenariiGames,

    Thanks for your question. I think your issue might be that you are casting the result of JsonConvert.DeserializeObject as an Action<GameFoundationData> instead of GameFoundationData. You are also assigning the resulting Action to onLoadCompleted. Instead, you should invoke onLoadCompleted with the resulting GameFoundationData from JsonConvert.DeserializeObject. I’m not sure what JsonConvert is, but using JsonUtility, you should be able to do the following:

    Code (CSharp):
    1.     public class SampleDataLayer : IDataPersistence
    2.     {
    3.         public void Save(GameFoundationData content, Action onSaveCompleted = null, Action<Exception> onSaveFailed = null)
    4.         {
    5.             string jsonString = JsonUtility.ToJson(content);
    6.             Debug.Log(jsonString);
    7.         }
    8.  
    9.         public void Load(Action<GameFoundationData> onLoadCompleted = null, Action<Exception> onLoadFailed = null)
    10.         {
    11.             string emptyInvJson = "{\"inventoryManagerData\":{\"items\":[],\"itemLists\":[],\"itemMaps\":[]},\"walletData\":{\"balances\":[]},\"rewardManagerData\":{\"rewards\":[]},\"version\":0}";
    12.             GameFoundationData deserializedData = JsonUtility.FromJson<GameFoundationData>(emptyInvJson);
    13.             onLoadCompleted?.Invoke(deserializedData);
    14.         }
    15.     }
    Hope that answers your question! Let us know if you have any other questions.
     
  7. DenariiGames

    DenariiGames

    Joined:
    Dec 29, 2019
    Posts:
    13
    richj_unity, erika_d and kevinmkchin like this.