How to keep the state of entity on state change (load a new level)

91 views
Skip to first unread message

mrco...@gmail.com

unread,
Nov 19, 2013, 12:59:03 PM11/19/13
to mel...@googlegroups.com
Hello everybody,

I tried to search for this in this community and on Google in general, but I couldn't really find an answer to this...

My question is, how can i keep the state of an entity a screen (level) when I leave the level and come back there?
With "state" i mean: pos, name, and any property that was set to a certain value when I left the level (state change)...
I know all this info can be found in the me.game.children array, but of course this gets cleaned up to be filled again with the entities that populates the next level I am loading...

Is there any way to keep the entities of a level, "saved" between scenes or do I have to code a system to keep track of that somewhere/somehow?
Example: I leave a room, after moving a white crate over a place that makes it become red. When, after a while, I come back in the same room, the crate is where I left it, and it's red...

Thanks in advance and keep up the good work guys!
Marco

Jason Oster

unread,
Nov 19, 2013, 1:22:44 PM11/19/13
to mel...@googlegroups.com, mel...@googlegroups.com
Hi Marco,

I completely dropped this idea on the floor as I was building my first game. The trouble with it is that you need to iterate all objects and save the state of only the interesting parts (position, chest open, etc).

I recommend adding `saveState` and `loadState` methods to every class that needs it. Before switching levels, iterate the entities in the game world and call the saveState() method if it exists. In your me.game.onLevelLoaded callback, do the same iteration, but call loadState() instead. You can use me.save to persist data : http://melonjs.github.io/docs/me.save.html

The tricky part here is to uniquely identify these entities. So for example, a chest always knows its unique id is abc123, so it can save/load its state using that key. I suppose you could add an id property manually to each object in Tiled. MelonJS does create a uuid property on some entities, but I don't know if they are deterministic.

Second, you can deterministically generate unique ids by hashing the current map name with the input arguments to the entity constructor. I will probably add that to melonJS, since it would be useful for other things.
--
You received this message because you are subscribed to the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to melonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jay Oster

unread,
Nov 20, 2013, 5:50:44 PM11/20/13
to mel...@googlegroups.com
I just checked melonJS, and it does consistently and deterministically create GUIDs for all entities extending me.ObjectEntity. So you would key the state on `this.GUID`.

The only remaining trouble is that the state will be bound to maps. So if you have a "PlayerEntity" on all of your maps, its state will be saved differently for each map. In other words, the state cannot cross the map "boundaries". This is not a problem for saving state that is local to the maps, like position. But for saving global state, you should use me.save directly, and key on some global identifier. Even something simple like "inventory" would be a good global ID for saving collected inventory in an RPG!

Reply all
Reply to author
Forward
0 new messages