Saving & Loading

The Blackboard system provides a flexible way to save and load data, making it easier to persist state across sessions or to initialize your system with predefined data. This functionality is encapsulated in two pairs of methods: ToJson/FromJson and ToData/FromData. A [BlackboardClass]Data object is generated with your blackboard that can be used to easily save and load your blackboard!

Data Methods

The ToData method converts the Blackboard's state into a data class instance. This data class is automatically generated and named after the Blackboard with the suffix Data. This method is useful for scenarios where you prefer working with strongly-typed data structures over JSON strings.

Example

Given the following blackboard:

public partial class ExampleBlackboard : IBlackboard
{
    private int _health = 100;
    private int _ammo = 50;
    private int _lives = 3;
}

The source generator will generate the following code:

public partial class ExampleBlackboard : IExampleBlackboard
{
    public ExampleBlackboardData ToData()
    {
        return new ExampleBlackboardData
        {
            Health = this.HealthSignal.Value,
            Ammo = this.AmmoSignal.Value,
            Lives = this.LivesSignal.Value,
        };
    }

    public void FromData(ExampleBlackboardData data)
    {
        this.HealthSignal.Value = data.Health;
        this.AmmoSignal.Value = data.Ammo;
        this.LivesSignal.Value = data.Lives;
    }
}

public class ExampleBlackboardData
{
    public int Health;
    public int Ammo;
    public int Lives;
}

Saving to Data

public void SaveData()
{
    var blackboardData = this.Blackboard.ToData();
    // Use 'blackboardData' as needed
}

Loading from Data

To load Blackboard data from a data class instance, use the FromData method. This method updates the Blackboard's state to reflect the data in the provided instance.

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

DontSaveAttribute

This attribute can be used to prevent a value from being saved when calling ToData(). This can be useful for values that are only temporary or are calculated.

using Blackboard.Contracts.Attributes;

public partial class ExampleBlackboard : BlackboardBehaviour {
    [DontSave] private int _health;
}

JSON Methods

Saving to JSON

To save the current state of the Blackboard to a JSON string, use the ToJson method. This method serializes the Blackboard's data into a JSON format string, which can then be saved to a file, PlayerPrefs, or any other storage mechanism your application uses.

public void SaveJson()
{
    string json = this.Blackboard.ToJson();
    // Save 'json' to your storage
}

Loading from JSON

To load Blackboard data from a JSON string, use the FromJson method. This method deserializes the provided JSON string, updating the Blackboard's state with the data.

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

    this.Blackboard.FromJson(json);
}

Note

Please be aware that these implementations are simple and might not cover all use cases, such as handling GameObjects or interfaces. You may need to extend or modify the provided methods to suit your specific needs.

Last updated