Save & Load Add-On
  • Welcome to Save & Load Add-On for FPS Engine!
  • BEFORE WE START
    • Early Considerations
    • Add-On Compatibility
    • List of Tutorials
  • GETTING STARTED
    • 01. Basic Set-Up
    • 02. Roadmap
  • CONTENT
    • 03. Content
      • 03.1 DataPersistence
        • DataPersistence_SO
        • DataPersistenceManager
        • FileDataHandler
        • GameData
        • GameDataManager
      • 03.2 Editor
      • 03.3 Item Registry
        • ItemRegistry
        • ItemRegistryEditor
        • ItemRegistryAssetProcessor
      • 03.4 Main Menu
        • SaveSlot
        • SaveSlotsMenu
      • 03.5 Objects Save Data
        • CustomSaveData
      • 03.6 Serialization
        • SerializableVector3
        • SafeSerializationBinder
      • 03.7 Triggers
        • SaveTrigger
        • LoadTrigger
        • ToggleAutoSave
      • 03.8 Type Registry
        • TypeRegistry
        • TypeToPrefabMappingDrawer
      • 03.9 UI
        • SaveLoadButton
    • 04. Shared Content for Add-Ons
      • ToastManager
      • WorkLight
  • HOW TO USE & GUIDES
    • Save & Load Games Programmatically
    • Saving Custom Data: Simple Method
    • Saving Custom Data: Advanced Method
    • Loading Custom Data
    • Saving Instantiated Objects
    • Save & Load System during Development
    • Working with Save & Load in Cowsins Manager
    • Uninstalling the Add-On: Fixing Errors
    • Type to Prefab Mappings in Game Data Manager
    • How to add Buttons to Save & Load a game
    • Non-Saveable Scenes
    • Loading Scenes & Load Player Data
  • FAQ
    • FAQ
  • SUPPORT
    • Support
Powered by GitBook
On this page
  • Early Considerations
  • Saving Custom Data
  • Checklist
  • What to do now? Loaded State.
  1. HOW TO USE & GUIDES

Saving Custom Data: Simple Method

PreviousSave & Load Games ProgrammaticallyNextSaving Custom Data: Advanced Method

Last updated 1 month ago

Early Considerations

For most scenarios, the simple method is more than enough to successfully save custom data. Except specific cases from data that needs to be saved from the Inventory Pro Add-On, the rest of the data in the Save & Load Add-On is Saved by implementing this method.

Pros
Cons

Simple & Fast to Implement

Only supports Serializable Data Types ( int, floats, bools, etc... )

Attribute Based [SaveField]

No-Code Required

Saving Custom Data

Imagine you’ve created a custom script that holds custom data that needs to be saved. For this example, let’s say you have a counter that increments an int every 5 seconds, and you want to store the value of that int whenever the game is saved.

IMPORTANT: The must exist in the scene for data to be saved.

You don’t need to manually add the DataPersistenceManager to each scene, as it is marked with [DontDestroyOnLoad]. Simply place the DataPersistenceManager in your Main Menu scene. When starting the game from a gameplay scene, data won’t be saved. However, when starting from the Main Menu, the DataPersistenceManager will persist across different scenes, ensuring data can be saved.

Below, you can take a look at the example code:

using System.Collections;
using UnityEngine;
public class CounterSave : MonoBehaviour
{
    // The counter value that we want to save
    public int counter = 0;

    // Time interval (in seconds) to increment the counter
    private float timer = 0f;
    private float interval = 5f;  // Increment every 5 seconds

    private void Update()
    {
        // Increment the counter every 5 seconds
        timer += Time.deltaTime;
        if (timer >= interval)
        {
            counter++;
            timer = 0f;
        }
    }
}

Now, we need to tell the Save & Load system which variable we want to save.

1

Add cowsins.SaveLoad library

using cowsins.SaveLoad;
2

Mark Saveable Variables with [SaveField]

In this example:

[SaveField] private int counter = 0;
3

Store Value in GameDataManager

StoreData();
using System.Collections;
using UnityEngine;
using cowsins.SaveLoad;  // Make sure to include this to work with Save & Load Add-On

// Ensure your Script inherits from Identifiable
public class CounterSave : Identifiable
{
    // Saveable fields must implement [SaveField]. It must be a serializable field.
    [SaveField] public int counter = 0;

    private float timer = 0f;
    private float interval = 5f;

    private void Update()
    {
        timer += Time.deltaTime;
        if (timer >= interval)
        {
            counter++;
            timer = 0f;
            // Send the new value to GameDataManager to store it.
            StoreData();
        }
    }
}

Checklist

  1. Your custom script has data that can be serialized and saved.

  2. Your custom script implements cowsins.SaveLoad library.

  3. Your custom script inherits from Identifiable.

  4. You update the value to be saved and notify GameDataManager through StoreData() method.


What to do now? Loaded State.

Once you are done Saving & Loading data, you can run custom behaviour based on the loaded data. Whenever LoadFields is called, LoadedState() will also be called.

Just like that, value will be serialized as a into the data.json file.

You can access this value from the , at the Custom Objects Dropdown.

Check for more Information.

DataPersistenceManager
CustomSaveData
Save&Load section in the Cowsins Manager
this guide