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

2013/5/5

Accessing variables from other actors

mattjames mattjames

2013/5/5

#
Hi all. i was wondering if somebody could explain how I can get a variable I have created in one actor to be used in other actors and also in my world classes. Thanks.
danpost danpost

2013/5/5

#
I will presume that there is only one of this actor type in the world (where the variable you want shared resides). Also, I will call your Actor sub-class where it resides 'Sharing' and your variable 'shared' and give it an 'int' type.
1
2
3
4
5
6
7
8
9
10
// from the world
int sharedValue = ((Sharing) getObjects(Sharing.class).get(0)).getShared();
// or from an actor
int sharedValue = ((Sharing) getWorld().getObjects(Sharing.class).get(0)).getShared();
 
// using this method in the 'Sharing' class
public int getShared()
{
    return shared;
}
You may have to check that 'getObjects(Sharing.class)' does not return an empty list before calling 'getShared' on an element of it.
mattjames mattjames

2013/5/5

#
Thanks danpost
buckeye4life22 buckeye4life22

2013/8/26

#
What is the .get(0)))
Gevater_Tod4711 Gevater_Tod4711

2013/8/26

#
get(int index) is a method in java.util.List. The method getObjects in greenfoot.World returns a List of actors. If you want to access the first actor in this list you need the method get(int index) with the parameter 0. 0 is always the first index of the list like in Arrays.
davmac davmac

2013/8/26

#
There's also a complete tutorial on how to do this.
Mickey09 Mickey09

2014/11/30

#
danpost wrote...
I will presume that there is only one of this actor type in the world (where the variable you want shared resides). Also, I will call your Actor sub-class where it resides 'Sharing' and your variable 'shared' and give it an 'int' type.
1
2
3
4
5
6
7
8
9
10
// from the world
int sharedValue = ((Sharing) getObjects(Sharing.class).get(0)).getShared();
// or from an actor
int sharedValue = ((Sharing) getWorld().getObjects(Sharing.class).get(0)).getShared();
 
// using this method in the 'Sharing' class
public int getShared()
{
    return shared;
}
You may have to check that 'getObjects(Sharing.class)' does not return an empty list before calling 'getShared' on an element of it.
When I place this code in I get the error, Cannot find symbol method getTime(), which is the name of the method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class Road extends World
{
    // Variables Defined
            // PSH = Player Start Height
            // PSH2 = Player 2 Start Height
            // middle = middle of the world's width
            // origin is the center height of the world
            // TSP = Treasure Starting Point
            // D2T = Distance 2 Treasure
             
    int middle = getWidth() /2 - 10 ;//Subtracted 10 to make finish class coincide with grid background
    int origin = getHeight() /2;
    int ran1 = getHeight();
    int ran2 = getWidth();
    int PSH = getHeight() /4 + origin;// added origin to the equation to the current player spawn point
    int P2SH = getHeight() /6;
    int TSP = Greenfoot.getRandomNumber(ran2);
    int D2T = TSP - middle;
    int P2T = ((Actor) getObjects(Player2.class).get(0)).getTime();
     
     
    /**
     * Constructor for objects of class Road.
     *
     */
    public Road()
    {   
        // Create a new world with 2000x200 cells with a cell size of 1x1 pixels.
        super(2000, 200, 1);
         
        FinishLine finishline = new FinishLine();
        addObject(finishline, middle, origin);
         
        Player player = new Player();
        addObject(player, middle, PSH);
         
        Player2 player2 = new Player2();
        addObject(player2, middle, P2SH);
         
        Treasure treasure = new Treasure();
        addObject(treasure, TSP, PSH); 
         
        Treasure treasure2 = new Treasure();
        addObject(treasure2, TSP, P2SH);
 
        background();
    }
    public int treasureDistance()
    {
        return D2T;
    
    public void background()
    {
        GreenfootImage img = getBackground();
        img.setColor(java.awt.Color.white);
        img.fillRect(800,65,450,50);
        img.setColor(java.awt.Color.black);
        img.drawString("vvvv", 100, 50);
        setBackground(img);
     
         
    }
}
This is my Actor Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class Player2 extends Actor
{
    private int limit = 1;
    private int steps = 0;
    public int time = 0;
             
    /**
     *
     */
   public void act()
   {
        move();
   }
   public void move()
   {
      move(1);
      steps = steps + 1;
         if (steps == limit)
         {
          turn (180);
          steps = 0;
          limit = limit*2;
         }
      TouchingTreasure();
      time = time + 1;
      
   }
   public void TouchingTreasure()
   {  
        Actor treasure = getOneObjectAtOffset(0, 0, Treasure.class);
        if (treasure !=null)
        {
            System.out.println("Good Job Player 2! You have found the Treasure after: " + time/60 + " Seconds" +((Road)getWorld()).treasureDistance()+ " Pixels Away");
            getWorld().removeObject (this);
             
             
             
        }
   }
    public int getTime()
   {
       return time;
   }
}
danpost danpost

2014/11/30

#
The fields and any values set to them (lines 11 through 19) are created and assigned their values before your world object is created. You will always get an empty list using 'getObjects' there (line 19) . Also, the error you are getting is saying that the 'getTime' method is not in the Actor class or inherited from any of its superclasses, which it is not. It is located in your Player2 class and any player2 Actor that needs to use that method must be typecast as a player2 object ( by changing '(Actor)' to '(Player2)' ).
danpost danpost

2014/11/30

#
@Mickey09, lines 1 through 4 of my code dated 2013/5/5 are showing examples of declaring a local variable within a method that gets the shared value from another class. They are not declared instance fields.
SnapDraggen SnapDraggen

2016/5/5

#
When I execute the code I get an error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 Does anyone know how to find which place it is in in the index?
SPower SPower

2016/5/5

#
This thread is two years old. If you have a problem, please post a new one.
Communazi Communazi

2016/6/4

#
Yeah i am getting the same error as SnapDraggen. any other threads that seem to have the right idea?
danpost danpost

2016/6/4

#
Communazi wrote...
Yeah i am getting the same error as SnapDraggen. any other threads that seem to have the right idea?
Guys, you should have seen the following above:
danpost wrote...
You may have to check that 'getObjects(Sharing.class)' does not return an empty list before calling 'getShared' on an element of it.
By not following this you may get an IndexOutOfBoundsException run-time error.
You need to login to post a reply.