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

2014/4/17

Access Data from a Non-Actor class through getWorld()

GRIFFIN GRIFFIN

2014/4/17

#
I am making a game where there are different upgrades for certain traits of my character. I have a class called UpgradeBar that switches images after every upgrade. It works fine until you switch worlds and then come back. the data isn't saved! I made a non_actor class called DATA which will store ever variable for the upgrades. I made a new instance of the DATA in my world, but when I call getWorld().data.blahmethod(), it cannot find data. Here is the code where I try to call data:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
 
public class UpgradeBar extends SUPERCLASS
{
    public int upNum;
    public int theNum;
    String[] images = {"Upgrade0", "Upgrade1", "Upgrade2", "Upgrade3", "Upgrade4", "Upgrade5"};
    public UpgradeBar(int partheNum){
        theNum = partheNum;
    }
    public void act()
    {
        setImage();
         
    
    public void setImage(){
        setImage(images[upNum]+".png");
    }
    public void Upgrade(){
        if(upNum < 5){ 
             
        }
    }
    public void chooser(){
       switch(theNum){
           case 1: upNum = getWorld().data.getArmor();
                break;
           case 2: upNum = getWorld().data.getGunLevel();
                break;
           case 3: upNum = getWorld().data.getAmmoCapacity();
                break;
           case 4: upNum = getWorld().data.getMinigun();
                break;
           case 5: upNum = getWorld().data.getShotgun();
                break;
           case 6: upNum = getWorld().data.getCanon();
                break;
           case 7: upNum = getWorld().data.getMissile();
                break;
           case 8: upNum = getWorld().data.getMortar();
                break;
           case 9: upNum = getWorld().data.getRaygun();
                break;
           default: upNum = 0;
                break;
            }
    }
}
I realize that this won't actually do anything yet but I need to be able to access the variable before I do anything else. Thanks for any help!
danpost danpost

2014/4/17

#
You need to return to the same world for the data to be saved. Using the following to go back to a type of world (called 'WorldName')
1
Greenfoot.setWorld(new WorldName());
will create a new world object and set it as active. It will not return you to the original world object. To return to the original world object, the original world would have to be passed to the intermediate world, and the intermediate world would have to save it in an instance field (call it 'previousWorld') so it can return to the original world using:
1
Greenfoot.setWorld(previousWorld);
GRIFFIN GRIFFIN

2014/4/17

#
I will be switching between multiple worlds before coming back to the same one, will this still work? And do I even need the DATA class anymore?
danpost danpost

2014/4/17

#
It will still work and you will not need the DATA class. Just pass the main world from world to world. Take a look at my Crayon Babies: A World Changing Demo.
GRIFFIN GRIFFIN

2014/4/18

#
Thanks a lot! I'll need to look at your code for a little bit to understand it, but this will help a lot!
You need to login to post a reply.