Give Page Feedback | API

Persistent State Managers - How To Create Custom Persistent State Managers

If the Binary Serializer Persistent State Manager does not meet your saving/loading needs, you have the option to create a custom Persistent State Manager, or to modify an existing class to implement the IPersistentData interface.

To get started, create a new class or modify an existing one to implement IPersistentStateManager. Note that while it's convenient to make your class a MonoBehaviour (for easy editing of settings in the inspector), this is not a requirement. You can create a regular C# non-MonoBehaviour class if you wish!

You should place your class inside of the DeepSpaceLabs.SAM namespace, which will make the IPersistentStateManager interface available and also help avoid naming conflicts with 3rd party assets or your own code.

--Special Note--
An easy way to start with your custom class is to hover over your the interface and choose the option Show potential fixes -> Implement interface. This will provide default implementations for all interface members (note, because you are implementing an interface, all members need to have implementations! There are no optional implementations like when overriding a base abstract class).

We also recommend

If you would like to save and load other data using the Binary Persistent State Manager, that is perfectly fine! However, you must ensure that the order in which your data is loaded is the same as the order in which your data is saved. Additionally, you will likely need to override the following two Property Getters to return true.

Disable Auto Invocation Of Before And After Save Methods (Property)

If you override this property getter to return false, when the Component Manager's Save method is called, it will invoke the OnBeforeDataSaved method before it begins the save process (the save process involves calling SaveInt, SaveFloat, etc.), and then OnAfterDataSaved once it finishes the save process.

In situations where the only data to save is SAM's persistent data, this strategy is optimal, as it automates thing which generally makes things easier for you (the developer).

On the other hand, if you have data unrelated to SAM that you wish to save using the state manager, it may be more efficient to have OnBeforeDataSaved called before the Component Manager's Save method, and OnAfterDataSaved after. In these situations, you can override the property getter to return true.

Note, however, that you will need to manually call these methods yourself, before the Component Manager's Save method is called (in the case of OnBeforeDataSaved), and after (in the case of OnAfterDataSaved). Otherwise the saving process will not work correctly.

Disable Auto Invocation Of Before And After Load Methods (Property)

If you override this property getter to return false, when the Component Manager's Load method is called, it will invoke the OnBeforeDataLoad method before it begins the load process (the load process involves calling LoadInt, LoadFloat, etc.), and then OnAfterDataSLoad once it finishes the load process.

In situations where the only data to load is SAM's persistent data, this strategy is optimal, as it automates thing which generally makes things easier for you (the developer).

On the other hand, if you have data unrelated to SAM that you wish to load using the state manager, it may be more efficient to have OnBeforeDataLoad called before the Component Manager's Load method, and OnAfterDataLoad after. In these situations, you can override this property getter to return true.

Note, however, that you will need to manually call these methods yourself, before the Component Manager's Load method is called (in the case of OnBeforeDataLoad), and after (in the case of OnAfterDataLoad). Otherwise the loading process will not work correctly.

OnBeforeDataSaved (Method)

Should perform any setup necessary for the Save methods (SaveInt, SaveFloat, etc.) to function correctly.

OnAfterDataSaved (Method)

Should perform any cleanup of resources created/opened in order to perform the save.

SaveInt (Method)

Should save a single integer value using your save logic.

SaveBool (Method)

Should save a single boolean value using your save logic.

SaveFloat (Method)

Should save a single float point value using your save logic.

SaveDouble (Method)

Should save a single double value using your save logic.

SaveString (Method)

Should save a single string using your save logic.

OnBeforeDataLoaded (Method)

Should perform any setup necessary for the Load methods (LoadInt, LoadFloat, etc.) to function correctly.

OnAfterDataLoaded (Method)

Should perform any cleanup of resources created/opened in order to perform the load.

LoadInt (Method)

Should load and return a single integer value using your load logic.

LoadBool (Method)

Should load and return a single boolean value using your load logic.

LoadFloat (Method)

Should load and return a single float point value using your load logic.

LoadDouble (Method)

Should load and return a single double value using your load logic.

LoadString (Method)

Should load and return a single string using your load logic.