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

2016/1/16

One variable that is shared between worlds

Sphem Sphem

2016/1/16

#
I've seen this a few times but I need it explained as basically as you can. I have many worlds in my program and I want 1 variable to be shared by all of them. I want it where the variable can be changed in any world and can be read in any world.
davmac davmac

2016/1/16

#
In one of the worlds, declare a public static variable at the class level:
class MyWorld
{
    public static int myVariable = 4;
}
Now you can access it anywhere as "MyWorld.myVariable". (Note, because the variable is static its value won't automatically get reset when you press the "reset" button in Greenfoot, you should probably initialise it in your first world's constructor).
You need to login to post a reply.