Retrieving custom properties from objects

General discussion about the R.U.B.E editor
Post Reply
garfi43
Posts: 6
Joined: Mon Jan 21, 2013 11:00 am

Retrieving custom properties from objects

Post by garfi43 »

Keys used to map custom properties are memory addresses of objects (b2Body,b2Fixture...etc).
And Box2D uses his own allocation method to be efficient (reuse allocated memory).

If the above is true, then what happens when an objectA (memory 0x01), which has a custom property, gets deleted and, objectB (memory 0x01), which does not have one, is created with the same memory as objectA.
It looks like it is going to give the wrong output if its called.

Possible solutions:
- hash the key
- implementing removeCustomPropertiesForItem(void *item)
- set to default value after use

Maybe the best would be to reset values to default, avoiding multiple creation and deletion of custom property objects. As stated above, Box2D is going to reuse memory anyway.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Retrieving custom properties from objects

Post by iforce2d »

I'm not quite following you... all properties in the JSON should only be accessed at the point of loading, so they will not be affected by any memory management that Box2D does.

The b2dJson builds a structure from which you can get information, but if you change anything about the world after loading then all bets are off regarding the validity of the the information, so all the b2dJson functions like getBodyByName and so on will only work immediately after loading the scene.

The intended practice is that you would have some data structure of your own, typically to put in the user data of a body or fixture, and you would copy the necessary things from the b2dJson info into your own structure. There is a new example in the sampleLoaders folder now, to show a basic sample of how this might work.
Post Reply