Make some bodies unload

General discussion about the R.U.B.E editor
Post Reply
welivehere
Posts: 6
Joined: Thu May 23, 2013 12:24 am

Make some bodies unload

Post by welivehere »

Hi,

I need to unload some bodies in my game. For example, a player got some coins, items and so on when he played and then next time when I load this map again then, there shouldn't be coins or items.
Is there any way to do this? Sorry for my poor English..

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

Re: Make some bodies unload

Post by iforce2d »

Good question. Rather than changing the map file, it would be better to keep track of which things were picked up, and then remove all of them after loading the next time. But... to keep track of which things have been picked up, would require some kind of identifying information in the pickups. You could set a custom property (eg. unique integer id number) on the pickups, and add that to a list when they are picked up. The id number could be made unique by a script like:

Code: Select all

body[] bs = getAllBodies();
int id = 0;
for (uint i = 0; i < bs.length; i++) {
    body b = bs[i];
    if ( b.hasCustomProperty("pickup") )
        b.setCustomInt( "id", id++ );
}
But that would potentially give different ids to each pickup every time, so it would not not be suitable for cases where you want to update the map and let users continue with their existing save game. You could improve it by only giving the pickups an id if they didn't already have one, but the script would be a bit more complicated. Let me know if you think this would help, I can make the script for you.
welivehere
Posts: 6
Joined: Thu May 23, 2013 12:24 am

Re: Make some bodies unload

Post by welivehere »

Thanks for reply!!
I will try the way that you wrote. But still it would be helpful for me if you make a script.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Make some bodies unload

Post by iforce2d »

Well, I'll need some more info for that...

- What item type will you be giving the ids to... I guess bodies?

- Presumably you don't need to give the ids to every item in the scene, so what criteria should be used to decide if an item needs the id? For example in my post above, the script only give ids to bodies that have a custom property called 'pickup' (it might have been better to check if the item has a custom string property called 'category', and that category value is 'pickup').

- The method I am thinking of would leave holes in the id sequence. For example, if you have three items and you give them ids, they would be 1, 2, 3. Then if you delete item 2, and make another item, and run the script again, the new item would given id 4, so you would then have 1, 3, 4 and the id 2 would never be used again. If you need the ids to have no holes, that can be done but the script will run slower.
welivehere
Posts: 6
Joined: Thu May 23, 2013 12:24 am

Re: Make some bodies unload

Post by welivehere »

I think I can handle this!! And also checked custom property tutorial!!

More question, if I want to save changed property on C++ then do I just use like 'setCustomBool' method?
If I can check an item picked, then how to unload this in a method like 'afterLoadProcessing(cocos2d method)'?

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

Re: Make some bodies unload

Post by iforce2d »

When managing game levels the b2dJson class is best used as read-only info. Usually, the instance of a b2dJson object would only exist for a temporary time while you load it (eg. afterLoadProcessing) and take all the necessary information from it into other data structures in your game. Writing back to the b2dJson is not a good way to do save games - that's why I said you would need some kind of list to keep track of what has been picked up.

If I was doing it, I would probably use a std::set or std::vector of integers to hold the ids of things that have been picked up. That would be the only thing you need to write to a file when saving the game. When loading the level the next time, you would load all the RUBE data as normal (same every time) and then load the 'save game' (which is the list of integers) and go through the list to remove all bodies with the ids in the list.

The b2dJson class has a function getBodyByCustomXXX (where the XXX is Int, Float, String etc) which you could use to find the bodies to remove. If you're using cocos2d, there is a method in RUBELayer called removeBodyFromWorld which you might find useful.
Post Reply