A way to save the current state of the world.

Discuss issues specific to miscellaneous ports of Box2D
COmmanderKeith
Posts: 19
Joined: Thu Oct 04, 2007 5:46 am

Re: A way to save the current state of the world.

Postby COmmanderKeith » Fri Dec 07, 2007 9:23 pm

ewjordan wrote:CommanderKeith is working out some of this in Java, but I think his solution is extremely Java specific.


Hi,

My way of saving state just invloves making a copy of the World by saving every single field in World and all its field's fields etc. The method I'm using is automatic - uses reflection to read and copy every object's fields. Isn't there something similar in other languages?

Erin gave me the idea to do this back when I asked if negative time updates were possible to go backwards in time and he said no, but that if you saved game state like 'prince of persia' then you can go back to the saved world and update forward to where you want to be. That's pretty much what I did (of course there are complications and I haven't quite got it to work with JBox2D yet, but it works with more simple things).

Cheers,
Keith

PS: I think XML is the wrong way to go, it would be too slow and with all of the parsing it would only be a solution for saving and loading games, not really for sync'ing server and client worlds by sending the whole game world to clients many times per second.

darkzerox
Posts: 241
Joined: Fri Dec 07, 2007 3:09 pm

Re: A way to save the current state of the world.

Postby darkzerox » Fri Dec 21, 2007 2:02 am

So...

In the meantime, while we're going about figuring out the best way to save the game state... the general consensus about saving and loading maps/levels is that we should keep a copy of the BodyDefs? Can't seem to restore the proper information from a b2Body.

I think I'm going to use XML for this in case Erin adds (and probably) will add more variables to the bodies..the binary data would become corrupt. This way I can at least see what's missing and use default values. Might also be useful to hand-tweak if my editor isn't sufficient.

Ygryggil
Posts: 106
Joined: Tue Dec 11, 2007 3:05 am

Re: A way to save the current state of the world.

Postby Ygryggil » Fri Dec 21, 2007 3:06 am

Maybe a b2Body.saveData() and loadData() function in the core would be useful. ;)

darkzerox
Posts: 241
Joined: Fri Dec 07, 2007 3:09 pm

Re: A way to save the current state of the world.

Postby darkzerox » Sun Dec 23, 2007 7:47 pm

Yeah...it would be... but the question is, is that b2's responsibility?

The way I'm doing it now with my editor...

Every time I create a body, I push the bodydef onto an std::vector
When I save I just pull the data from the bodydefs.
When I delete an object however... I need to correlate the b2Body with with the b2BodyDef. So I'm storing the index of the b2BodyDef in the vector in the userData. Then when I delete a body, I just need to retrieve the index and delete the b2BodyDef too. Problem is...soon as you delete something, the std::vector updates all the indices, which screws everything else up. So now I either need to decrement all the indices stored in userData every time I delete something, or I need to switch over to an array of b2BodyDef's and take a bit of a memory hit.

Thoughts?
Last edited by darkzerox on Sun Dec 23, 2007 10:52 pm, edited 1 time in total.

Erin Catto
Site Admin
Posts: 2948
Joined: Thu Sep 06, 2007 12:34 am

Re: A way to save the current state of the world.

Postby Erin Catto » Sun Dec 23, 2007 9:23 pm

In my experience user data objects are best kept in a pool. You'll need to figure out the pool size ahead of time and use a free list.

If your user data object storage must be of variable size then you can store a GUID in the body user data and use a hashtable to go from GUID to user data object. The GUID can just be the body creation number (e.g. the 23rd body created since app launch).

rraallvv
Posts: 3
Joined: Tue Apr 03, 2012 11:55 am

Re: A way to save the current state of the world.

Postby rraallvv » Mon Aug 06, 2012 5:49 pm

sorry for bringing this old post up, but have you came up with a complete solution?
some kind of library extension or patch to box2d
Regards


Return to “Miscellaneous”



Who is online

Users browsing this forum: No registered users and 1 guest