Save and Load
v4.0
Last updated
Was this helpful?
v4.0
Last updated
Was this helpful?
You may see a yellow "warning" in the Console about the serialization depth limit being reached.
Yellow "Warnings" in the console are not errors, and do not always mean something is wrong!
This is because the Dictionaries often are already a few levels deep into an object hierarchy, and have a fair number of levels themselves. If you store a Prefab
in the dictionaries which also had a dictionaries, this warning will appear more often.
THIS IS NOT ALWAYS A PROBLEM.
Depending on the project, you may not need to serialize the dictionaries, so "data not saved" is not "data lost".
Keep in mind that the "Dictionaries" on a "Game[TYPE]", such as "GameItemObject", can be changed and saved at runtime. But in many cases, the values needed are stored in the Parent object (such as the "ItemObject" scriptable object), which is not changed at runtime, and is not serialized.
However, if you do wish to serialize the data and the depth is past the limit, there are two methods that can be used to compress the data prior to saving.
SaveData()
and LoadData()
SaveData()
will serialize the dictionaries data into a JSON string. This string can then be saved in your class structure, during the PreSave()
step in the flow.
LoadData()
will deserialize the string back into a dictionaries object, which can be done during the PostLoad()
step.
The JSON data will encode each KeyValue
into an item of a List<string>
, and then the entire Dictionaries into a string. The data will be decoded from those strings into KeyValue
objects.