World custom properties

Report problems here (or use the built-in feedback dialog in the editor)
Post Reply
Craig Mitchell
Posts: 3
Joined: Sun Feb 16, 2014 3:24 am

World custom properties

Post by Craig Mitchell » Sun Feb 16, 2014 3:36 am

Hi! I think I've found a bug in the way world custom properties are output in the json file...
eg you get:-

Code: Select all

	"customProperties" : 
	[
		
		{
			"float" : 0.1,
			"name" : "myCustomProperty"
		}
	],
instead of

Code: Select all

	"customProperties" : 
	[
		
		{
			"name" : "myCustomProperty",
			"float" : 0.1
		}
	],
This causes a crash in my json reader... I am using v1.5.4 in windows.
If you confirm this is a RUBE bug, how soon can it be fixed??
Many thanks for your excellent editor!

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: World custom properties

Post by iforce2d » Mon Feb 17, 2014 12:29 am

I don't understand what the bug is... both are perfectly valid JSON, and they are in fact equivalent to each other according to the spec: "An object is an unordered set of name/value pairs" http://www.json.org/

The parser should not crash regardless of any malformed input it is given. If it chokes on this, it needs fixing.

Craig Mitchell
Posts: 3
Joined: Sun Feb 16, 2014 3:24 am

Re: World custom properties

Post by Craig Mitchell » Mon Feb 17, 2014 9:40 am

You are correct, of course... I shouldn't make posts when tired...
I've investigated further and the problem only occurs when I export files with hex representation for floats enabled. It occurs with any float custom property at any scope (world, body etc...)
There is no bug in the RUBE export. Please accept my apologies!
The error is in b2json.cpp from the sample loader. See my comment below...

Code: Select all

#define IMPLEMENT_READ_CUSTOM_PROPERTIES_FROM_JSON(b2Type)\
void b2dJson::readCustomPropertiesFromJson(b2Type* item, Json::Value value)\
{\
    if ( ! item )\
        return;\
    if ( ! value.isMember("customProperties") )\
        return;\
\
    int i = 0;\
    Json::Value propValue = value["customProperties"][i++];\
    while ( !propValue.isNull() ) {\
        string propertyName = propValue.get("name", "").asString();\
\
        if ( propValue.isMember("int") ) {\
            int val = propValue.get("int", 0).asInt();\
            setCustomInt(item, propertyName, val);\
        }\
        if ( propValue.isMember("float") ) {\
//*** This is wrong.
            float val = propValue.get("float", 0).asFloat();\
//***  it should be:-
            float val = jsonTofloat("float", propValue);\
            setCustomFloat(item, propertyName, val);\
        }\
        if ( propValue.isMember("string") ) {\
            string val = propValue.get("string", 0).asString();\
            setCustomString(item, propertyName, val);\
        }\
        if ( propValue.isMember("vec2") ) {\
            b2Vec2 val = jsonToVec("vec2", propValue);\
            setCustomVector(item, propertyName, val);\
        }\
        if ( propValue.isMember("bool") ) {\
            bool val = propValue.get("bool", 0).asBool();\
            setCustomBool(item, propertyName, val);\
        }\
\
        propValue = value["customProperties"][i++];\
    }\
}
Many thanks once more!

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: World custom properties

Post by iforce2d » Tue Feb 18, 2014 12:38 am

You are quite right. This was corrected in the github a while ago but there are so many sample loader downloads that I probably forgot to update one of them. Whereabouts did you get that particular b2dJson.cpp from?

Craig Mitchell
Posts: 3
Joined: Sun Feb 16, 2014 3:24 am

Re: World custom properties

Post by Craig Mitchell » Tue Feb 18, 2014 8:19 am

I think it was the cocos2d-x one from here
https://www.iforce2d.net/rube/loaders/r ... ct-src.zip

Post Reply