Skip to content

Save and Load SocialEngine State

Shi Johnson-Bey edited this page Feb 21, 2024 · 3 revisions

How to Save/Load the SocialEngine State

TDRS provides the TdrsJsonExporter and TdrsJsonImporter classes to save and load the social engine's state to and from JSON strings.

Saving the social engine to JSON

using TDRS.Serialization;
using UnityEngine;

// A MonoBehaviour class that saves and loads the engine state
public class SaveManager : MonoBehaviour
{
    public void HandleCreateSave()
    {
        var socialEngine = SocialEngineController.Instance.State;
        var jsonExporter = new TdrsJsonExporter();

        string jsonString = jsonExporter.Export(socialEngine);

        // Save jsonString to file in the persistent data path
    }

    public void HandleLoadSave()
    {
        var socialEngineController = SocialEngineController.Instance;

        var jsonImporter = new TdrsJsonImporter();

        string jsonString = "Load json data from somewhere";

        SocialEngine socialEngine = jsonImporter.Import(jsonString);

        socialEngineController.State = socialEngine;
    }
}