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

2017/2/17

Boolean value from User Class to World class

Jillis Jillis

2017/2/17

#
I am aware that the "accessing variables from another class" thread has been made hundreds of times and there's a tutorial, but I still can't get it to work in the scenario that I am working on. I have a boolean value called "entry" in my User class. And I would like to access it and use it in an if statement in my world subclass called "Room7" (child of the World1 class). Any help would be greatly appreciated.
Nosson1459 Nosson1459

2017/2/17

#
You can use in Room7:
User user = new User(); // fit it to your class name and constructor
// and when you want to access the variable you can do
boolean userEntryBoolean = user.entry;
You can use this code to get the value from the User class but any changes made to either of these two variables won't happen to both variables unless you reset the userEntryBoolean to user.entry after the change has been made (probably right before you want to check the state of the variable).
danpost danpost

2017/2/17

#
Or simply use the following line:
if ( ! getObjects(User.class).isEmpty() && ((User)getObjects(User.class).get(0)).entry)
The first half can be removed if there is a User object in the Room7 world during the entire time that world is active.
Jillis Jillis

2017/2/18

#
Thank you both for your help! I was just wondering if you were able to help me with another problem. The User can walk two routes, and the choice of route will determine an outcome. For example, if the user walks through "door6" then a boolean value called "entry" becomes TRUE. If they walk through "door7" then nothing happens. This is why I am using entry (from the User class) in the Room7 world. As entry determines if something should be spawned or not. Originally I used
if (isTouching(Door6.class))
        {
            entry = true;
            Greenfoot.setWorld(new Room7());
        }
however I believe this resets entry to false once the new world has been set. Is there a way of keeping entry true? Thanks again.
Jillis Jillis

2017/2/18

#
Also dan, how does your line work? I can't just use "entry" as a value after I have put that line in right? The part of the code where I am using it is this:
private void prepare()
    {
        User user = new User();
        if ( ! getObjects(User.class).isEmpty() && ((User)getObjects(User.class).get(0)).entry)
        if (entry == true)
        {
            DoorClosed doorclosed = new DoorClosed();
            doorclosed.turn(270);
            addObject(doorclosed,60,300);
            
            Door door7 = new Door6();
            door7.turn(90);
            addObject(door7,560,300);
            
            addObject(user,100,300);
        }
        else
        {
            DoorClosed doorclosed = new DoorClosed();
            doorclosed.turn(90);
            addObject(doorclosed,560,300);
            
            Door door6 = new Door6();
            door6.turn(270);
            addObject(door6,40,300);
            
            addObject(user,500,300);
        }
    }
Super_Hippo Super_Hippo

2017/2/18

#
Usually line 4 should replace line 5. If you use it in the prepare method before the User is added to the world though, it will never be true. I think you should pass the User object to the new world and add that user to the new world instead of creating a new one. Then you can check if its entry variable is true or false.
You need to login to post a reply.