# 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:

```csharp
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:

```csharp
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

```csharp
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.

```csharp
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.

```csharp
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.

```csharp
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.

```csharp
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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blackboard.crashkonijn.com/concepts/saving_loading.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
