How do I make my save data?

Unlike version 1.x the save data in this version is modular. To define data you’ll need to make your own class for it. You can make as many of these as you need to hold your data, splitting up elements of your save as needed.

For ease of use you can make your own either by hand or using a little tool in the asset, below I’ll go over both methods:

Using Tool

The tool can be accessed via the following path:

Tools > Carter Games > Save Manager > Save Object Creator

This will open this little window, from here all you need to do is enter a name for the class you’d like to make. By default the class name will be what you enter followed the “SaveObject“, so if I entered Cake it would default the name to CakeSaveObject.cs

When you press the create button it will ask you tochoose save location for the new class. Note this needs to be in the projects Assets/ folder. You can rename the file if you are not happy with the generated name. Once you press save the file wil be generated.

Once the script has been generated and the project has recompiled, you will be prompted with the option to make a new instance of the save object you just made, if you press cancel nothing will happen and you’re all set. If you press yes a new save object (scriptable object) will be generated at the same path the class was just created to so you are all ready to use the new object in your project.

The creator uses a template file provided with the asset which I’ll show below, it has a comment for where you should put your save values. You can also change the namespace and createassetmenu setup if you wish.

using CarterGames.Assets.SaveManager;
using UnityEngine;

namespace Save
{
    [CreateAssetMenu(fileName = "%SaveObjectName%")]
    public class %SaveObjectName% : SaveObject
    {
        // Enter your save values here...
    }
}

Manual Creation

You’ll need to make a new C# class first. Once you’ve made a new class you’ll need to inherit from SaveObject like so:

public class MyClass : SaveObject
{

}

If your code is in its own assembly definition you’ll need to reference the Save Manager Runtime assembly for the inheritance to work.

From here you now have a save object to add values to. Note that this is a scriptable object, so you’ll need to add the [CreateAssetMenu] attribute to it to allow you to make an instance in your project.

[CreateAssetMenu]
public class MyClass : SaveObject
{

}

Now all there is to do is to add the data you want to save. Note that whatever you want to save has to be serializable. Most common are, but if you are saving custom classes etc. you’ll need to make sure this is the case. The Save Manager using a JSON based save setup so elements such as vectors will work just fine. The asset also comes with a basic serializable dictionary class which can be used to save dictionaries should you wish.

To make a new save value use the SaveValue<T> generic class and pass in the type you want to save. If you want to customise the default value or key you can do so by initializing the field on declaration. Note that the save keys MUST be unique for the save to work. If left without a declaration it will have a random save key & the default as the type default.

[CreateAssetMenu]
public class MyClass : SaveObject
{
    public SaveValue<bool> myBoolSaveValue;
    public SaveValue<Vector2> myVecSaveValue = new SaveValue<Vector2>("MySaveKey");
    [SerializedField] private SaveValue<int> myIntSave = new SaveValue<int>("MyIntSaveKey", 100);
}