9 years ago by Jesse
View persistence-impact on Github
Using this plugin quickly enables ImpactJS to efficiently save and restore level states. This code works with the entities of ig.Game and exposes two key functions that a developer can use. The module defines a single instance of a Persistence class at ig.game.persistence although this is not required for the plugin to work properly.
getLevelState ()
Finds all persistable entities and returns an Array with their state objects. In order for an ig.Entity to be persistable, it must define a property called persist as true. This way you can choose which entities to save and restore later.
loadLevelState (Array levelState = null)
Loads an Array of state objects produced by getLevelState. Note that the parameter levelState is optional and for a good reason. If you call this method without a levelState, it simply prepares all persistable objects for later. It's a good idea to call this function right after the level is finished loading and your code does any post-loading initialization. You should check to see if a state is available, but call the function even if there isn't
Tip: Call the loadLevelState function before you move any entities with code, because it uses entity positions to identify entities.
View the full documentation on persistence-impact at Github
Using this plugin quickly enables ImpactJS to efficiently save and restore level states. This code works with the entities of ig.Game and exposes two key functions that a developer can use. The module defines a single instance of a Persistence class at ig.game.persistence although this is not required for the plugin to work properly.
getLevelState ()
Finds all persistable entities and returns an Array with their state objects. In order for an ig.Entity to be persistable, it must define a property called persist as true. This way you can choose which entities to save and restore later.
//Call when the level ends, but before the next one is loaded this.savedState = this.persistence.getLevelState();
loadLevelState (Array levelState = null)
Loads an Array of state objects produced by getLevelState. Note that the parameter levelState is optional and for a good reason. If you call this method without a levelState, it simply prepares all persistable objects for later. It's a good idea to call this function right after the level is finished loading and your code does any post-loading initialization. You should check to see if a state is available, but call the function even if there isn't
loadLevel: function(data) { this.parent(data); // Any level loading code goes here // savedState may be undefined, but that's OK var currentLevelState = this.savedState; this.persistence.loadLevelState(currentLevelState); }
Tip: Call the loadLevelState function before you move any entities with code, because it uses entity positions to identify entities.
View the full documentation on persistence-impact at Github