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

2014/11/4

Referencing a variable across worlds

Hersov Hersov

2014/11/4

#
Im doing a program where I need to reference a variable to be accessible in another world. Right now its coming up with an error "java.lang.ClassCastException: main menu cannot be cast to Game". The code it says is wrong is "
1
Game worldref = (Game) getWorld();
Just incase you need anymore information this is defined in another world and that is where the actor is located, any help would be appreciated
davmac davmac

2014/11/4

#
Right now its coming up with an error "java.lang.ClassCastException: main menu cannot be cast to Game"
I'm certain that this is not the exact text of the error message. However, I think I know the problem. It is that the actor that is executing the line of code you posted is not actually within a "Game" world; instead, it is within your menu world. So, when it calls 'getWorld()' it gets a reference to the menu world; you then try to cast this to a Game, but that fails (because it is not a Game, it is a menu). If you need a reference to the Game world, you need to save it in a variable somewhere, most likely.
Hersov Hersov

2014/11/4

#
If I save it as a variable then how would I access it from one world to the other, I have a main menu where people enter two strings then a game where it prints the names. Is this possible by saving the inputed messages from the "main menu" into the other world? Or is it just easier to save as a variable?
davmac davmac

2014/11/4

#
Is this possible by saving the inputed messages from the "main menu" into the other world?
Yes, that should be possible.
Or is it just easier to save as a variable?
You use a variable in either case. You can either save a reference to the world that you need, or to the message within it. The only real difference is the type of the variable.
Hersov Hersov

2014/11/4

#
Can you elaborate because Im very confused, this is the code for my input messages in the world "mainmenu"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void act()
    {
        GreenfootImage textImage = new GreenfootImage ( "Click this to Input player names" ,30, Color.PINK, Color.WHITE);
        setImage(textImage);
 
        if (Greenfoot.mouseClicked(this)) 
        {
            String name1 = JOptionPane.showInputDialog("What is player 1's name?");
 
            Game worldref = (Game) getWorld();
            worldref.setName1(name1);
 
 
            String name2 = JOptionPane.showInputDialog("What is player 2's name?");
 
            Game worldref1 = (Game) getWorld();
            worldref1.setName2(name2);
 
        }
Hersov Hersov

2014/11/4

#
And this is my code for the world, (Just do say the actor of the input is in the "mainmenu", so this might be causing the difficulties)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void setName1 (String z)
{
    Name1=z;       
}
 
public String getName1()
{
    return Name1;
}
 
public void setName2 (String x)
{
    Name2=x;       
}
 
public String getName2()
{
    return Name2;
}
davmac davmac

2014/11/4

#
I think I already explained the problem. Your call to getWorld() returns the menu world, not the game world. You need to save a reference to the game world somewhere (in a variable in either the menu world or the actor within it), so that you can access it. Or, if you haven't yet created the game world, you should save the required data (name1 and name2) somewhere, and set it on the game world just after you create it.
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
The command getWorld will return the world that's currently 'active' or the world the codes resides in. So davmac is just trying to say that seen you're trying to get the variable from the other world you should have a variable that holds a refence to that world. Because using getWorld again will only return the world currently active or the world the code is in. Allso, is it really necessary to make 2 differend worldrefs? I mean you could just use this if I'm right:
1
2
3
Game worldref = new Game();
worldref.setname1(name1);
worldref.setname2(name2);
Making two differend worldrefs really has no use seen you're calling 2 methods from the same world. Seen you're using a world called 'mainmenu' i'm assuming this is allso the first world you want to be build. So that means the world 'Game' has not yet been builld hence the commandline "Game worldref = new Game();" instead of "Game worldref = Game();" which should work if the 'Game' world has been created before.
davmac davmac

2014/11/6

#
The command getWorld will return the world that's currently 'active' or the world the codes resides in.
No - it returns the world that the actor is in, which isn't necessarily the active world. If the actor is not in any world, it returns null (even if there is an active world).
You need to login to post a reply.