Forum rules - please read before posting.

Saving Custom Data

Hi

I will have plenty of custom data to save across difference game objects. While I can certainly make a Remember and RememberData class for each, its cumbersome to manually copy the members in SaveData and restore it in LoadData when much of the time they are types that can be serialized/deserailized

Tonight I quickly tested a serializable container class that stores data.

[System.Serializable]
public class TimeOfDayData  : BaseContainer 
{
    public int _minutes, _hours, _days, _months, _years;    
    public int _totalTurns;
// ...
}

I serialize this to JSON and then use a simple RememberData class that saves that string. IE in SaveData()

[System.Serializable]
public class JSONRememberData : RememberData
{
    public string realData; // Stores JSON of any BaseContainer derived class
}

In Load game that string from JSONRememberData is deseralized into the correct container object.

public override void LoadData(string stringData)
{
    JSONRememberData data = Serializer.LoadScriptData<JSONRememberData>(stringData);
    if (data == null) return;
    SavePrevented = data.savePrevented; if (savePrevented) return;

    var dest = gameObject.GetComponent<TimeOfDayManager>();

    dest.data = JsonConvert.DeserializeObject(data.realData) as TimeOfDayData;
}

This thread mentions using JSON to store custom data: https://adventurecreator.org/forum/discussion/11383/how-to-save-custom-lists-that-contains-scriptableobjects

Does the above approach make sense?
Do you recommend a different way?

(Im using newtonsoft serialized, not jsonutility for other reasons)

Comments

  • I don't see any issue with the shared code, but given the context - is "time of day" data scene-independent? If so, it may be better to save it in Global Data - i.e. storing the Json output into a Global String variable.

    A tutorial on this can be found here.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.