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

2012/6/12

How do I keep variables after you switch worlds?

steved steved

2012/6/12

#
For a game I'm making I would like to be able to switch worlds and keep your score and other variables. Is there a way to do this?
erdelf erdelf

2012/6/12

#
save the score in the topclass of your worlds, or ask in the constructer of each world for an int.
danpost danpost

2012/6/12

#
There are several ways to make infomation available to worlds you switch to. The usual way is to pass either the information itself, or an object that contains that information, to the new world by way of a parameter in the new world's constructor. The information does not actually need to be contained in the object, just that the object has access to the information (method calls can return the information if you pass a world or actor object). For certain types of information (like the score, for instance, if there is only one instance of it), it might be suitable to make the variable(s) that hold the information 'static', which makes them class variables. Then you do not have to pass it at all. Just get the value with 'ClassName.VariableName'. Be cautious using 'static' variables, as they do not clear when the scenario is reset. If there is only one player, put in its constuctor the code to zero the score value; that way, anytime the player is created, the score is set to zero to start.
steved steved

2012/6/13

#
Thanks guys I will probably put it in the constructor of the new world. That would be easy as most things are based in my world anyways and there are only a few variables.
You need to login to post a reply.