Infinite Save Scripting

Scripting an infinite save system.

Step 1: Create a Script

Create a script or multiple scripts that will handle all save menu functionality.

For this example, we will create a single script called InfiniteSavesExample.cs.

Start the script by adding the member variables that can be set from the inspector.

public class InfiniteSavesExample : MonoBehaviour
{
    [SerializeField]
    private Button saveSlotPrefab;
    
    [SerializeField]
    private Button saveSlotCreator;
    
    [SerializeField]
    private Transform content;
    
    [SerializeField]
    private Text timeElapsedText;
    
    [SerializeField]
    private Toggle modeToggle;
}

Remember to add a GameObject to the scene and add this script to it. At this point, we should have everything to populate the inspector properties.

Properties

Save Slot Prefab: the save slot button prefab.

Save Slot Creator: the button that will create a new save.

Content: the scroll view's content object.

Time Elapsed Text: the text object that will display the time elapsed.

Mode Toggle: the toggle that will toggle the mode between load and save mode.

Step 2: Other Script Members

We will create other members that won't be visible in the inspector.

timeElapsedKey: the key (or ID) of the time elapsed for saving purposes.

slots: a list of buttons that will store all of the instantiated save slots.

timeElapsed: the time elapsed as a float.

loadMode: the isOn value of the toggle.

If load mode is true, we will make the save slots load a save. If it's false (save mode), we will make the save slots overwrite a save.

Step 3: Instantiate Existing Saves

When we enter play mode, we need to first load any existing saves that the player may have made in a previous session.

We can do this using the Start method.

Step 4: Increment Time

In the Update method, we will increment the time elapsed. This will be the only data that will be saved and loaded for this example.

Step 5: Saving and Loading Data

We will create 3 methods for saving and loading. The first one will load the data from a save file.

The second one will save (or overwrite) the data in the save file.

The third one will be used by the save slots and will load or overwrite the data depending on the mode.

Step 6: Create New Save

We have methods that require a save file, but no save file is being created yet. A save file should be created when the 'Create New Save' button is pressed, along with a save slot.

So, let's create some methods for this. The first method will create a new save file.

At the end, CreateNewSaveSlot is called. This has not been created yet. The CreateNewSaveSlot method will instantiate a new save slot, edit the save slot's text, and give the save slot an on-click event that will call LoadOrOverwriteSave.

We still need the save slot creator button to have an on-click event that creates a new save. This can be done by updating our Start method.

Done! You can now test it in play mode.

Last updated