This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zamoht
Zamoht wrote ...

2012/11/17

Greenfoot.setWorld(World world) question

Zamoht Zamoht

2012/11/17

#
Okay so after using this method what happens to the old world? If you're never going to use that world again do you release it somehow. Maybe it's a good time to ask about releasing in general here? I guess World.removeObject(Object object) releases the object as well, but does the Greenfoot.setWorld(World world) release the old world or how does this work?
danpost danpost

2012/11/18

#
If there is no reference to the old world in the new world once the new world act methods start executing, then the old world is flagged for garbage collection. If you send the current world to the new world either as a parameter in the constructor of the new world or setting a static to the current world or call a method in the new world to set a field to the old world (after creating the world, and maybe after using the Greenfoot.setWorld command but before leaving the current 'act' method that is executing). If a reference to the old world is saved in the new world, you can go back to it (with everything just the way you left it) by using Greenfoot.setWorld(referenceNameForOldWorld). See my Crayon Babies scenario, which demonstrates this.
Zamoht Zamoht

2012/11/18

#
Thank you!
danpost wrote...
If a reference to the old world is saved in the new world, you can go back to it (with everything just the way you left it) by using Greenfoot.setWorld(referenceNameForOldWorld).
I'm using this in my Wombat Fighter scenario, which was why I wondered what would happen if you didn't make a reference. Thanks again!
danpost danpost

2012/11/18

#
Actually, you said that you guessed that "World.removeObject(Object object) releases the old object as well"; but, that is not the case. 'removeObject(...)' needs an 'Actor object' according to the World API; and, anyway, how can you remove a world from itself???
Zamoht Zamoht

2012/11/18

#
danpost wrote...
'removeObject(...)' needs an 'Actor object' according to the World API;
Yes okay my bad, but does it release the Actor object then?
danpost wrote...
how can you remove a world from itself???
I thought this would be weird too, but I don't much about the autorelease system works in Java (or is it Greenfoot that has the autoreleasing?).
danpost danpost

2012/11/18

#
Zamoht wrote...
does it release the Actor object then? I think you mean, will this statement cause the Actor object to be flagged for deletion. Not in itself. For example:
addObject(new Food(), 0, 0);
removeObject((Actor) getObjects(Food.class).get(0));
// with no references, the object will be flagged
//
Food food = new Food();
addObject(food, 0, 0);
removeObject(food);
// no flagged until exiting the method
If the second example, if you add 'food' back into the world or save it in an active object or a class field, it will not be flagged when you exit the method.
Zamoht Zamoht

2012/11/18

#
Okay so basically objects get flagged if there is no reference to them?
You need to login to post a reply.