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

2014/3/22

Struggling with Accessing Variables from other Worlds/Actors

EIGNERT EIGNERT

2014/3/22

#
Hey Guys, So basically I've been struggling for the last 3 weeks or so on accessing a certain variable called 'password' from an actor called 'ChipRider_PasswordSet' into an actor called 'ChipRider_SingleTicketButton'. (Weirdly named variable, I know.) In the 'ChipRider_PasswordSet' actor, I have made a method which returns the variable 'password':
public int getpassword()
    {
        return password;
    }
And I have called it in the actor 'ChipRider_SingleTicketButton' using this code:
ChipRider_PasswordSet passwordset = (ChipRider_PasswordSet) getWorld().getObjects(ChipRider_PasswordSet.class).get(0);
        int password = passwordset.getpassword();
But I keep getting a run-time error stating:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
	at java.util.ArrayList.get(ArrayList.java:382)
	at ChipRider_SingleTicketButton.act(ChipRider_SingleTicketButton.java:28)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
(Sorry, not sure whether I should submit that as code.) Would anyone be able to help me fix this? Thanks, EIGNERT
EIGNERT EIGNERT

2014/3/22

#
Oh and also the part which starts with 'at ChipRider_SingleTicketButton' is highlighted in red and underlined. If clicked, it refers to me to the part where I call it in the actor 'ChipRider_SingleTicketButton' as seen above. Thanks, EIGNERT
danpost danpost

2014/3/22

#
Looks like there is no ChipRider_PasswordSet object in the world; so you cannot 'get(0)' from the empty list returned from 'getObjects(ChipRider_PasswordSet)'.
EIGNERT EIGNERT

2014/3/22

#
Sorry danpost, I'm not very good with the technical terms - just to clarify does object mean actor or variable? ChipRider_PasswordSet is an actor in which 'password' is defined. What does 'get(0)' mean exactly? Would there be something I should put in its place? Thanks.
danpost danpost

2014/3/22

#
I mean 'ChipRider_PasswordSet object' as an actor (since the 'ChipRider_PasswordSet' class extends Actor). 'get(int)' is a method of the List class that retrieves an element from the list at the given index ('0' is the first index as counting, in Java, starts at zero). If the list is empty, however, there is no first element to 'get'. You could check to make sure there is at least one element in the list by using the 'isEmpty()' method on the list. It returns a boolean value indicating whether the list is empty or not.
int password = 0; // or some other number; one that can never be a password, preferably
if (!getWorld().getObjects(ChipRider_PasswordSet.class).isEmpty())
{
    ChipRider_PasswordSet passwordset = getWorld().getObjects(ChipRider_PasswordSet.class).get(0);
    password = passwordset.getpassword();
}
EIGNERT EIGNERT

2014/3/24

#
Thanks danpost. But is there a way to access objects and their methods from other worlds? That's my problem.
danpost danpost

2014/3/24

#
EIGNERT wrote...
Thanks danpost. But is there a way to access objects and their methods from other worlds? That's my problem.
Sure. 'getWorld' just returns a reference to the world that the actor is in. If you use a reference to that world from another world, it would work just as well. Since you did not supply any names, I will call the one ChipWorld and the other OtherWorld. From OtherWorld:
ChipWorld chipWorld = new ChipWorld();
int password = 0;
if (!chipWorld.getObjects(ChipRider_PasswordSet.class).isEmpty())
{
    ChipRider_PasswordSet passwordset = (ChipRider_PasswordSet) chipWorld.getObjects(ChipRider_PasswordSet.class).get(0);
    password = passwordset.getpassword();
}
Oh, and in line 4 of my last post, I missed the typecasting. It should be the same as in line 4 here -- '(ChipRider_PasswordSet)'
You need to login to post a reply.