Loading external file data into Scriptable Object
I've been at this for two long days, trying to figure out a way to handle saving and loading data for a mobile puzzle game I'm working on. The game has level packs and each level pack has multiple levels. Level packs can be purchased via IAP.
I need access to the fixed data and an easy way to add new data, so I used Scriptable Objects to store my Level Pack and Level data:
LevelPack
id
type (paid, free)
IAPproductId
levels (list of levels)
Level
id
totalTime (total time to solve puzzle)
warningTime (give a warning during level)
levels (list of levels)
The problem is that I need to also store the following dynamic data, which can change during runtime:
completedTime for each level
isPurchased for each level pack
I tried adding these to the scriptable objects and it's really easy to maintain and work with. But I can't figure out a way to persist that data. I created a save system that saves just the completed levels to an external file using binary formatter. So every time a level is completed, I change Level.completedTime
to the new time on the scriptable object, then do GameState.Instance.UpdateLevelProgress(levelPack.id, levelId, time);
to save the file that contains a dictionary with all the completed times.
public void UpdateLevelProgress(int levelPackId, int levelId, float time)
{
if (!levelProgress.ContainsKey(levelPackId))
{
levelProgress.Add(levelPackId, new Dictionary<int, float>());
}
// set time
levelProgress[levelPackId][levelId] = time;
// save to file
SaveGame();
}
Even though I figured out how to save, I don't know how to load the data from the file into each Level scriptable object. There is an onEnable
function, but that runs way before my GameState.LoadGame()
function that runs inside a preload scene. So GameState.Instance.levelProgress
is empty at that point.
Is there a better, cleaner way to do this?
Your answer
Follow this Question
Related Questions
Scriptable Objects inside other SO's not loading properly from Asset Bundle 1 Answer
Referencing another object's instance 0 Answers
How to use RenderStaticPreview OR get project viewer thumbnails for ScriptableObject assets? 2 Answers
Assign ScriptableObject (Inventory DataBase) 0 Answers
Help with a C# script .SetActive 1 Answer