Saving world state using b2dJson

General discussion about the R.U.B.E editor
Post Reply
Eddy8555
Posts: 42
Joined: Sat Apr 27, 2013 3:01 pm

Saving world state using b2dJson

Post by Eddy8555 »

I'm trying to save my game's state using the json tools in TouchJSON, which are used in RUBELayer.

I'm attempting to save my world to a file (now that's how poetry should react to a changing culture!) by using this code, basically taken from BasicRubeLayer but using the write instead of the read:

NSString* filename = @"newWorld.json";
NSString* fullpath = [CCFileUtils fullPathFromRelativePath:filename];
b2dJson json;
bool wroteFile = json.writeToFile(_world, [fullpath UTF8String]);
printf("wrote %i \n", wroteFile);

The wroteFile returns good, and I've followed the code all the way to jsoncpp.cpp's StyledStreamWriter::writeValue( const Value &value )
and I'm seeing what looks like values from the world seemingly accessed and written. But my newWorld.json file stays empty.

Any ideas why is that? I added the file to the project, and it's in the same directory from where my rube scene's json is being read for loading my subclass of RubeLayer. I also chmod'd newWorld.json to have write permissions. So I'm not sure what am I missing here.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Saving world state using b2dJson

Post by iforce2d »

Reading the documentation for the functions you are using is always a good start. fullPathFromRelativePath gives you a path to a file in your app bundle, so you can't write to that location.

iOS File System Basics

What is the documents directory
Eddy8555
Posts: 42
Joined: Sat Apr 27, 2013 3:01 pm

Re: Saving world state using b2dJson

Post by Eddy8555 »

Thanks.

So I'm writing to a file and it comes out with alphanumeric values instead of the actual ones. Something like this:

"fixture" :
[
{
"density" : "446DD5D5",
"friction" : "3E4CCCCD",
"name" : "fixture0",
"polygon" :
{
"vertices" :
{
"x" : [ "3D9B0520", "BD29BF9C", "BDE80480" ],
"y" : [ "BFBC6F48", "BF685030", "BFBD1F47" ]
}
}
},

Why is this happening? Is the json.writeToFile method useful in this context?
Eddy8555
Posts: 42
Joined: Sat Apr 27, 2013 3:01 pm

Re: Saving world state using b2dJson

Post by Eddy8555 »

OK got it, need to set the readHumanFloats boolean in b2dJson to true.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Saving world state using b2dJson

Post by iforce2d »

If you are using the b2dJson to load it, those hex values are ok because it can load either. If you want to manually edit the .json file though, you would want to have them human-readable :)
Eddy8555
Posts: 42
Joined: Sat Apr 27, 2013 3:01 pm

Re: Saving world state using b2dJson

Post by Eddy8555 »

Yeah, I get that. And I get the loading to work, including bodies and joints, created on the fly by the user. Nice.

But I guess I'm living in parallel universes right now. One is my own in which I use the Rube generated C++ code -- and got stuck because some images aren't aligned correctly with their bodies . Ane the second universe, in which I'm loading the rube scene through json, for which I'd need to write some code to hide the initial load, then use each body loaded as template for creating the bodies the user is interested in.

I need to organize my thoughts here :)
Post Reply