InverNessian / SafeguardsOfLeyto

For development of my game by the same name.
1 stars 0 forks source link

Data Persistance #3

Closed InverNessian closed 4 years ago

InverNessian commented 5 years ago

The game needs to be able to save and load data. Obviously the main goal for this is to create save files, but it also means I don't have to manually enter all the units' stats into Scriptable Objects.

InverNessian commented 5 years ago

It looks like there are a few ways to keep info around after a scene is gone.

  1. PlayerPrefs, for player preferences and small data.
  2. DontDestroyOnLoad(obj), for keeping specific objects between scenes. Useful for a game controller object!
  3. Serialization using binary formatters. Best method?
InverNessian commented 5 years ago

While working on this, it's become apparent that I need to re-organize my existing code and functionality. This seems to be the best method:

GameController handles only the game flow and saving game state/progress. Data Manager handles the saving and loading of character data, but otherwise serves as only a holder for the scriptable objects. The Attack classes would be the ones that handle the process of generating attacks.

InverNessian commented 5 years ago

The problem I'm running into is how to manage the Attacks. Instantiate them as MonoBehaviors? Or should I make Scriptable Objects? But you can't instantiate those, they have to be pre-made in the Asset menu.

InverNessian commented 5 years ago

I think what I can do is just have another level of inheritance. I'll go back to how it was originally? There will be Attack_Normal and Attack_Savage_Blow and stuff.

This allows me to just have one instance of the attack I guess. Then units can just store a reference to it, dynamically fetched according to talents/etc.

InverNessian commented 5 years ago

I've gotten a good pattern here. Unit Data will exist in a ScriptableObject class and be instantiated for each unique unit. This means that there will only be one UnitData for each enemy type, allowing me to cut down on RAM usage. However, I need to find a way to have individualized health for enemies.

With that in mind, I've got data persistence implemented, all that's left is to test it.

InverNessian commented 5 years ago

Apparently ScriptableObjects can't be Serialized by the C# serializer. Ugh.

On the plus side, Unity has built-in Serialization for ScriptableObjects and MonoBehaviors, or so it claims. Seems to only work sometimes. Need more investigation, as this would make things super easy.

InverNessian commented 5 years ago

It works! Apparently you just need to use EditorUtility.SetDirty(obj) to force Unity to save it on close.

InverNessian commented 5 years ago

Yeah so apparently this doesn't actually work in build versions of the game? FML.

At least I won't have to worry about making SOs this time. Just remove the SO inheritance and have them be base C# classes. This lets me implement the System.IO serialization.

InverNessian commented 5 years ago

Actually it'd probably be better to just make the Manager classes serializable. That way they can serve as wrapper classes.