04 Saving & Loading

This example shows you how to save and load data in a blackboard.

These files can be found in the /Examples/[example] and /Blackboards/Examples folders.

SavingBlackboard.cs

using CrashKonijn.Blackboard.Contracts;

namespace CrashKonijn.BlackboardPro.Blackboards.Examples
{
    public partial class SavingBlackboard : BlackboardBehaviour
    {
        // By default, all signals are saved and loaded.
        private int health = 100;
        
        // Sometimes you don't want values to be saved/loaded, you can use the [DoNotSave] attribute to prevent this.
        [DontSave]
        private int lowHealth = 40;
        
        // Computed props are never saved, they are recalculated on load.
        private static bool isLowHealth(Signals.Health health, Signals.LowHealth lowHealth) => health.Value <= lowHealth.Value;
    }
}

SavingBehaviour.cs

using CrashKonijn.Blackboard.Contracts;
using CrashKonijn.BlackboardPro.Blackboards.Examples;
using UnityEngine;

namespace CrashKonijn.BlackboardPro.Examples._04_saving
{
    [RequireComponent(typeof(SavingBlackboard))]
    public class SavingBehaviour : MonoBehaviour
    {
        [TextArea]
        [SerializeField]
        private string json;
        
        /* Simple hack to show a button in the editor */
        [Header("Save the data to json")]
        [Button(nameof(SaveJson))]
        public string saveJsonButton;
        
        /* Simple hack to show a button in the editor */
        [Header("Load the data from json")]
        [Button(nameof(LoadJson))]
        public string loadJsonButton;
        
        [SerializeField]
        private SavingBlackboardData blackboardData;
        
        /* Simple hack to show a button in the editor */
        [Header("Save the data as data")]
        [Button(nameof(SaveData))]
        public string saveDataButton;
        
        /* Simple hack to show a button in the editor */
        [Header("Load the data from data")]
        [Button(nameof(LoadData))]
        public string loadDataButton;
        
        private SavingBlackboard blackboard;
        public SavingBlackboard Blackboard
        {
            get
            {
                if (this.blackboard == null)
                {
                    this.blackboard = this.GetComponent<SavingBlackboard>();
                }

                return this.blackboard;
            }
        }

        // This package provides a ToJson and FromJson method to save and load the blackboard.
        // Please be aware that this is a simple implementation and might not work for all cases such as GameObjects or interfaces.
        public void SaveJson()
        {
            this.json = this.Blackboard.ToJson();
        }

        public void LoadJson()
        {
            if (string.IsNullOrEmpty(this.json))
            {
                Debug.Log("Can't load empty json");
                return;
            }
            
            this.Blackboard.FromJson(this.json);
        }

        // This package provides a ToData and FromData method to save and load the blackboard.
        // The package will create a data class within the same namespace for you that you can use to save and load the blackboard.
        // The name of the data class is the name of the blackboard with the suffix Data.
        public void SaveData()
        {
            this.blackboardData = this.Blackboard.ToData();
        }

        public void LoadData()
        {
            this.Blackboard.FromData(this.blackboardData);
        }
    }
}

Last updated