Scriptable Objects and Saving System

Hello,

First of all, I wanted to say thank you for all courses you are providing to us. They are great!

I have questions about Scriptable Objects and how we can make them work with a Saving System.

Let’s say that we want to provide to the player a way to customize his weapons (name, modify damage type, …).

Here how we could define our datastructure (using ScriptableObjects)

[CreateMenuItem("RPG/DamageType")]
public class DamageType: ScriptableObject {}

Using this, I can create multiple type instances: FireDamage, WaterDamage, …

[CreateMenuItem("RPG/Damage")]
public class Damage: ScriptableObject {
  [SerializedField] DamageType type;
  [SerializedField] int lower;
  [SerializedField] int upper;
}

I can now create an instance of the Damage ScriptableObject keeping the following information “Fire Damage between 15 and 25”.

[CreateMenuItem("RPG/Weapon")]
public class Weapon: ScriptableObject {
  [SerializedField] Damage damage;
  [SerializedField] string weaponName;
}

Now, if I create a GUI allowing the player to change:

  • the damage range,
  • the damage type,
  • the weapon name

then it will modify Scriptable Object instances and their default values will be lost.

My first idea to keep default values was to create a default save of those objects to keep these values in order to restore these values in case of “New Game”. Also, the player could have make changes between saves and I want to be able to restore states using save files.

The problem is that when I try to serialize a ScriptableObject, it will save its instance id but not the value in it. With the code above, it means that if I save a Weapon, the [SerializedField] Damage damage; will be serialized as an instance id and so its default values will not be saved.

Since the Damage ScriptableObject instance can be referenced by other weapons, I believe that keeping the instance id of a ScriptableObject during serialization allows unity to restore easily those links.

:grey_question: Is there any simple way to save/load nested ScriptableObject’s values? How would you perform such customization management?

:grey_question: Generally speaking, how game states held by ScriptableObject can be save/load?

I feel like ScriptableObjects are amazing and provide way to edit data in the editor instead of writing code (which is definitly great when you want to work with designers, …) but I am convinced I have to learn more about them and about best practices to use them.

Do not hesitate to share any articles/videos/courses on the subject :slight_smile:

Have a good day,

3 Likes

@sampattuzzi

I would suggest having a seperate set of objects for storing what can change at runtime. Essentially Having a Weapon and a WeaponConfig. This way all data that can’t be changed by the player lives in the config. Anything that changes lives in the Weapon. The defaults can also live in the config.

This way the save system only has to worry about saving the data that can be changed.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms