Skip to content

How To Save and Load Engine State

Vadim Gromov edited this page Jan 10, 2021 · 11 revisions

All _gc references in these examples are instances of a IGameController

More about state management here

To save engine state (including the entire inventory [more on this]) and get the state contract object, call this method:

_savedState = ZaraEngine.EngineState.GetState(_gc);

This object is ready to be serialized. If you are using serialization that require special attributes to be applied (like XML serialization), change contract objects to your liking in this folder: /Zara/Essentials/StateManaging/Contracts
As long as you do not remove or rename any of the existing fields, nothing will break. All contract data is presented by fields, but you can change them to properties for example if you need so, and add some attributes to them.

To load (restore) engine state, call

ZaraEngine.EngineState.RestoreState(_gc, _savedState, MethodToRestoreWorldTime);
...
private void MethodToRestoreWorldTime(DateTime savedWorldTime){
    _gc.SetWorldTime(savedWorldTime);
}

RestoreState method requires you to restore the world time in order to work correctly. The world time that RestoreState gives you must be passed to the IGameController object to update the WorldTime property.


Important: if you are restoring Zara state asyncronously, be sure not to call .Check() methods on _gc.Health and _gc.Body during the RestoreState method execution.
It is not recommended to save or load during the sleep.


Clone this wiki locally