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

2014/8/29

Universal Array

KenCo KenCo

2014/8/29

#
Please can any help? I have an array " grid which is defined in the world. How can I make this array available to Actors. What I hope can be defined is a universal array available across all classes. Thank you.
erdelf erdelf

2014/8/29

#
define it as public and then just call it from the actors with getWorld();
danpost danpost

2014/8/29

#
If the array remain unchanged throughout the running of the scenario, then you can make the array a 'public static final' array which can be referenced from any class with 'WorldClassName.arrayName'. If the array elements change, then just make it 'public static'.
K_wow K_wow

2014/8/30

#
If you want to get an array from your world, you'll need to have a line in your actor's code like this:
WorldName variableName = (WorldName)getWorld();
Replacing "WorldName" with the name of your world class and "variableName" with whatever you want to call the variable. This is necessary because you need to cast your world before you can access anything in it that is not in the default world class. By the way danpost, do "public static" variables keep their values when the world is reset?
danpost danpost

2014/8/30

#
K_wow wrote...
By the way danpost, do "public static" variables keep their values when the world is reset?
All 'static' fields retain their values and are only re-initialized when the project is re-compiled -- resetting only creates a new world instance to start the project with and does not re-compile the project. Because of this, all 'static' fields that are not declared 'final' should be programmatically initialized in your main world constructor or some other appropriate location during start-up.
K_wow K_wow

2014/8/30

#
danpost wrote...
K_wow wrote...
By the way danpost, do "public static" variables keep their values when the world is reset?
All 'static' fields retain their values and are only re-initialized when the project is re-compiled -- resetting only creates a new world instance to start the project with and does not re-compile the project. Because of this, all 'static' fields that are not declared 'final' should be programmatically initialized in your main world constructor or some other appropriate location during start-up.
So, that could allow for something like a record score in a game that would stay the same when the player resets the game?
danpost danpost

2014/8/30

#
K_wow wrote...
< Quote Omitted > So, that could allow for something like a record score in a game that would stay the same when the player resets the game?
Like a session high score -- yes.
K_wow K_wow

2014/8/31

#
danpost wrote...
K_wow wrote...
< Quote Omitted > So, that could allow for something like a record score in a game that would stay the same when the player resets the game?
Like a session high score -- yes.
Cool, thanks!
KenCo KenCo

2014/9/1

#
Thank you helpers, you input aided me to see what I really wanted to achieve and have now solved my problem.
You need to login to post a reply.