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:
The source generator will generate the following code:
Saving to Data
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.
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.
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.
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.
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