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

2016/12/6

world

Nosson1459 Nosson1459

2016/12/6

#
How do I get/call a variable from one world to another?
danpost danpost

2016/12/6

#
Nosson1459 wrote...
How do I get/call a variable from one world to another?
Details! Code!
Nosson1459 Nosson1459

2016/12/7

#
danpost wrote...
Details! Code!
What code! I'm asking If it's possible to (and how do i) call/use a variable from one class which extends World to another class that extends World? If you really want code then here it is (below is the first world with a variable that I want to call from the second world):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (Nosson1459) 
 * @version (12/6/2016)
 */
public class MyWorld extends World
{
    public int variable=4;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
    }
    public void act()
    {
        variable++;
    }
}
Below is the world that's calling the variable from the first world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld2 here.
 * 
 * @author (Nosson1459) 
 * @version (12/6/2016)
 */
public class MyWorld2 extends World
{

    /**
     * Constructor for objects of class MyWorld2.
     * 
     */
    public MyWorld2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
    }
    public void act()
    {
        // on this line and any more needed below it, should be the coding for retrieving and using a variable from a different world
    }
}
Here is your "Code!" I'm wondering (as asked above) if it's possible to use one variable for two classes both extending World (not extending another class).
danpost danpost

2016/12/7

#
In the MyWorld class, do this:
// replace line 11 with
public static int variable;
// and insert the following at line 20
variable = 4;
Then, line 23 can be:
int var = MyWorld.variable;
Nosson1459 Nosson1459

2016/12/7

#
Thanks, I don't necessarily need this now but I was just wondering. You wrote then line 23... that means in MyWorld2?
danpost danpost

2016/12/7

#
Nosson1459 wrote...
Thanks, I don't necessarily need this now but I was just wondering. You wrote then line 23... that means in MyWorld2?
Correct.
Nosson1459 Nosson1459

2016/12/7

#
Am I able to change the variable in MyWorld besides for just getting the value?
danpost danpost

2016/12/7

#
Nosson1459 wrote...
Am I able to change the variable in MyWorld besides for just getting the value?
You can change the value from any class with (to zero its value):
MyWorld.variable = 0;
Nosson1459 Nosson1459

2016/12/7

#
So the main part is basically the public static, defining in the constructor, and WorldName.variableName.
danpost danpost

2016/12/7

#
Nosson1459 wrote...
So the main part is basically the public static, defining in the constructor, and WorldName.variableName.
I guess. Be aware that a static variable is not associated with any object, but with the class. Also, its value is not reset when the project is reset. Only compilation will cause it to reset (or by code, like setting its value in the world constructor).
Nosson1459 Nosson1459

2016/12/8

#
Oh thanks. (This has nothing to do with this discussion, but it has to do with the Greenfoot website (and you might know about it): There is a star next to my user image which gets colored when someone replied/commented in the same place as me, on the star is a number with the amount of new replies there are, my question is: If i have more than 8 replies I feel like it still says 8, when I click on the star at any time it usually only has the last 8 replies so by the new replies even if I never visited them it won't count them in, today I think I had more than 8 replies but it only said 8 (this discussion was not one of the 8 in the star but I still haven't visited this discussion.) I counted 14 replies for today (I noticed while looking through the discussions there were replies in discussions that I posted in but it didn't have them in the new reply star (that's how I noticed it said a maximum of eight)).
You need to login to post a reply.