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

2014/1/20

Accessing information from a seperate world

Cross Cross

2014/1/20

#
How would I get an integer from one world and use it in another?
danpost danpost

2014/1/20

#
Explain. Is the integer to be passed when the other world is created or some time after its creation? What are the names of the two world classes and where is the integer within the originating world and where is the integer to go within the destination world and what is the name of the integer field? (I should probably go on).
Cross Cross

2014/1/20

#
I have a score as an integer kept in the game world (myWorld), and I'm trying to access it after the player loses the game to post it to a high scores page (HighScoresPage). The score is just going to be added to a method that saves the score to a file.
danpost danpost

2014/1/20

#
Ok. How is the integer declared in the myWorld class? What is the name of the class that creates and initiates the new HighScoresPage world and what class does that class extend? What method(s) do you have available in the HighScoresPage class to receive and save/use the integer value?
Cross Cross

2014/1/20

#
I have private int score = 0; and I have a public method in myWorld that returns score. I tried:
myWorld m = new myWorld();
        
        score = m.getScore();
but it thinks that score = 0. When the player loses the game, the world changes from myWorld to HighScoresPage.
Cross Cross

2014/1/20

#
Could it be that when i make a new myWorld in HighScoresPage, it finds score at the starting value?
danpost danpost

2014/1/21

#
Cross wrote...
Could it be that when i make a new myWorld in HighScoresPage, it finds score at the starting value?
Yes, but not for the reason you may think. When you make a new myWorld, it gets created complete with the given fields and use of methods that are in the myWorld class. Not only is the myWorld object new, but the fields are new also (each instance gets a field to hold the specified type of data for each non-static field declared, but a specifically named non-static field can hold one value in one world object and a totally different value in another world object. I will presume (since you did not clearly specify) that the change from myWorld to HighScoresPage is coded in the class of the player (an Actor subclass). First, we need a way to get the score from the world. Since the field is declared 'private', we will need a method to return its value (it looks like you may already have this):
// in the myWorld class
public int getScore()
{
    return score;
}
Now, we need a way for the class of the player to access that method. 'getWorld()' alone will not do because that method returns a World object, not a myWorld object (though it may actually be of type myWorld, the system will not know that unless you tell it that it is). So, we cast the type (or typecast) the object with '(myWorld)getWorld()'. With that, you will be able to call 'getScore' on that world with '((myWorld)getWorld()).getScore()'. The player now has access to the score value. Next, we need to pass that value to the new HighScoresPage world either when instantiated or after it has been created. You said you already have a method in the HighScoresPage class that gets a value and saves it to a file. If that is all that needs done with the value of the score in that world at that time, then the following should work (using 'methodName' for that method):
int score = ((myWorld)getWorld()).getScore(); // get score from world
HighScoresPage hsp = new HighScoresPage(); // create high score world
hsp.methodName(score); // call that method in 'hsp' that saves the score
Greenfoot.setWorld(hsp); // make 'hsp' the active world
Cross Cross

2014/1/21

#
myWorld and HighScoresPage is coded in the World subclass. I do have buttons that I can use to change the world, but when I tried to work with the code you provided, but I wasn't able to implement it into the buttons.
danpost danpost

2014/1/21

#
Please show the attempted code at implementing what I provided into the buttons and explain how exactly it did not function correctly.
Cross Cross

2014/1/21

#
private void test()
    {
        int score = ((myWorld)getWorld()).getScore(); // get score from world  
        HighScoresPage hsp = new HighScoresPage(); // create high score world 
        s = Integer.toString(score);
        hsp.getScore(s); // call that method in 'hsp' that saves the score  
        Greenfoot.setWorld(hsp); // make 'hsp' the active world 
    }
That's the code in the button actor class, I added the s = Integer.toString(score) part because the method that saves the score only takes Strings. For now, Im trying to send it to the getScore method,
    public void getScore(String s)
    {
        return s;
    }
I may have done something wrong, but I'm not entirely sure.
Cross Cross

2014/1/21

#
Okay, I think the test() method works, but the getScore() method is not, because the return type is void. I might just have the wrong syntax..
danpost danpost

2014/1/21

#
Cross wrote...
public void getScore(String s)
{
    return s;
}
I may have done something wrong, but I'm not entirely sure.
Well, I will tell you...yes, you sure did. (1) the return type of the method is declared 'void', but you are trying to return a value from the method; (2) the value you are returning appears to be the same value as what is brought in by way of the argument (s); To see if the String value is received by the 'getScore(String)' method, change 'return s;' to 'System.out.println("String received by 'getScore(String)': "+s);'.
Cross Cross

2014/1/21

#
I got a java.lang.ClassCastException: Title cannot be cast to myWorld. Which occured at this line
int score = ((myWorld)getWorld()).getScore(); // get score from world  
I believe this is because the button is in a different world. I tried putting it in a separate class, but at the same line, I got a null pointer exception.
danpost danpost

2014/1/21

#
The code I gave was to be placed at the place where the game is determined to be over (probably in the class of the player).
You need to login to post a reply.