Difference between revisions of "Hooking into the Save State"
From Beerplop
(One intermediate revision by one other user not shown) | |||
Line 3: | Line 3: | ||
The $key must be a unique key for the module. The $saveCallback must return a serializable value. The $loadCallback will either get the deserialized value which was stored before or won't be called if no entry is present for the module in the current save state. | The $key must be a unique key for the module. The $saveCallback must return a serializable value. The $loadCallback will either get the deserialized value which was stored before or won't be called if no entry is present for the module in the current save state. | ||
− | An example integration may look like:<syntaxhighlight lang="javascript" line=" | + | An example integration may look like:<syntaxhighlight lang="javascript" line="1000"> |
(new Beerplop.GamePersistor()).registerModule( | (new Beerplop.GamePersistor()).registerModule( | ||
'BeerFactory__Trader', | 'BeerFactory__Trader', | ||
Line 9: | Line 9: | ||
(loadedState) => this.state = $.extend(true, {}, this.initialState, loadedState), | (loadedState) => this.state = $.extend(true, {}, this.initialState, loadedState), | ||
); | ); | ||
− | </syntaxhighlight><br /> | + | </syntaxhighlight> |
+ | |||
+ | <br />Make sure to load the loadedState over an initialState and not over the current state. Otherwise the loadedState may mix up with the currently active state before loading. |
Latest revision as of 01:16, 18 September 2020
To create an entry in the save state a GamePersistor object is required. Using the method registerModule($key, $saveCallback, $loadCallback) of the save state a new entry can be added.
The $key must be a unique key for the module. The $saveCallback must return a serializable value. The $loadCallback will either get the deserialized value which was stored before or won't be called if no entry is present for the module in the current save state.
An example integration may look like:
1 (new Beerplop.GamePersistor()).registerModule(
2 'BeerFactory__Trader',
3 () => return this.state,
4 (loadedState) => this.state = $.extend(true, {}, this.initialState, loadedState),
5 );
Make sure to load the loadedState over an initialState and not over the current state. Otherwise the loadedState may mix up with the currently active state before loading.