» Edit Game Info

2 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 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) {
    // Any level loading code goes here

    // savedState may be undefined, but that's OK
    var currentLevelState = this.savedState;

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

2 years ago by FelipeBudinich

This looks fantastic I'll give it a try later :D

2 years ago by stahlmanDesign

@Jesse Awesome, I created my own solution much like this, but not as a plugin. I remember many of the same things you are talking about, like using the position to ID the entity, making sure they are restored before things start to move, etc. I also had it optional because some entities you don't care about saving, like particle effects for example.

I'll take a look at this plugin cause it's great when you can make the code modular and reusable in other games.
Page 1 of 1
« first « previous next › last »

Post Reply

Please login or register to reply.