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

![Example 04 Saving editor](/files/ebi2fvREX5YAvALt3MRE)

## SavingBlackboard.cs

```csharp
﻿using CrashKonijn.RabbitBlackboard.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

```csharp
﻿using CrashKonijn.BlackboardPro.Blackboards.Examples;
using CrashKonijn.RabbitBlackboard.Contracts;
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);
        }
    }
}

```


---

# 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/examples/04_saving.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.
