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

2014/10/23

calling variable from new World subclass

Alwin_Gerrits Alwin_Gerrits

2014/10/23

#
Hey guys, I kindoff have this problem and I don't know how to fix it. I want to have something like a variable which I can set in a class, then after I switch worlds, I want to be able to check this variable. However, I have multiple subclasses of World (ranging LEVEL1 to LEVEL4) and I think it might just be impossible to set a variable of a class and keep the set value after changing world. Any thought on this? (By the way, I'm trying to make it a boolean value instead of an integer, so if you could keep that in mind that would be great :))
danpost danpost

2014/10/24

#
There are several ways to accomplish that: (1) set the field as 'static' (you need to understand how static fields work, however); (2) pass the value to the constructor of the new world by using an argument/parameter; (3) pass the value to the new world after creating it (and before setting the new world active); There may be more ways, but these are, I believe, the most common ways.
Alwin_Gerrits Alwin_Gerrits

2014/10/24

#
I think I saw an early post of 2011 from you about the same problem, but I didn't get it. I think my knowledge about static only goes as far as the fact that once you declared it it shouldn't be able to change. So that is probably part of the problem. I found it by the way, you posted this:
Level oldLevel = (Level)getWorld();  
Level newLevel = new Level();  
newLevel.totalTime = oldLevel.totalTime+oldLevel.time;  
Greenfoot.setWorld(newLevel);  
Now obviously it's not what i'm trying to do seen this is adding up 2 scores. But seen the idea is the same I thought I would just try using it for my code. It turned out like this but obviously it doesn't work:
LEVEL1 oldLevel = (LEVEL1)getWorld();  
LEVEL2 newLevel = new LEVEL2();  
newLevel.Fire= oldLevel.Fire;
Greenfoot.setWorld(new Level);  
Now I think I should be using 'public static void Fire()' instead of 'public void Fire()'
public void Fire()
{
   if (canFire)
   {/*do something*/}
}
But I don't get what my code should look like if I'm just trying to get a simple boolean value from world A to world B. Is it just that I'm trying something rediculous here or is this just something that's a bit hard to get? Just calling me inexperienced is fine to :). Anyway, some explanation would be fine, but I'll react later because it's 3 in the morning here XD.
Alwin_Gerrits Alwin_Gerrits

2014/10/24

#
K, just a quick add then. SPower posted this, Maybe this will help you create a example? I don't know just trying to make it a bit easier to explain to a stubborn person like myself :).
// you can put this in any class  
private static int currentScore;  
  
public static int getCurrentScore() {  
return currentScore;  
}  
  
public static void setCurrentScore(int s) {  
currentScore = s;  
}  
and then when you change the score, you say:
view plaincopy to clipboardprint?
<nameOfClassWhereScoreIsStored>.setScore(100);  
// without the <> signs  
danpost danpost

2014/10/24

#
First off, the code you say I posted in 2011 does not appear to deal with static fields. You did not supply a link or any reference to that discussion thread, so I am at a lose as far as that goes. I can tell, however, that the code is used to pass some information from the old world to the new one. 'totalTime' and 'time are field names, whereas 'Fire' appears to be the name of a method whose return type is void. As far as the 'Fire' method: I cannot see that being anything but a non-static method as some object must be doing the firing. Furthermore, I cannot see why you would call the method during a change in worlds. Also, I do not see what that method has to do with the boolean you want to pass to the new world. Maybe not enough context was given in that regard. As far as the code provided by SPower: the suggestion looks ok; but, like I said before, you should know what you are doing when using static fields/methods.
davmac davmac

2014/10/24

#
Alwin_Gerrits wrote...
I think my knowledge about static only goes as far as the fact that once you declared it it shouldn't be able to change.
I think you have mixed up static with final. You can change the value of a static variable (unless it is also final). A static variable is a variable which belongs to a class instead of to instances of the class. So, each instance of the class will see the same value, instead of having their own "copy" of the variable with a distinct value.
Alwin_Gerrits Alwin_Gerrits

2014/10/24

#
davmac wrote...
A static variable is a variable which belongs to a class instead of to instances of the class.
So then does that mean it's like variables you declare as a global variable? I think there has to be a difference, but it's vacation here so I can't really bother my teacher about it and class has been going to slow to get to this subject yet -_-. I guess regardless of my lacking knowledge, I'm not beïng very clear about what i'm trying to do. So here's the scenario: I'm making a platform game where the player can pick up certain items to get a powerup. What I want to get done is that when the player has a powerup and is moving to the second level he doesn't lose his powerup. My method 'Fire' up until not contained nothing but the boolean canFire which would determine whether the player picked up the powerup that lets him fire. I guess that's all i'm trying to do right now... i'll try looking into static and final myself, but plz your vision on how to take down this problem i'm having.
Alwin_Gerrits Alwin_Gerrits

2014/10/24

#
Wow, seems like I got the answer to my question allready. I simply changed canFire to a public static boolean. Now when going to the next level it remembers whether it picked up a weapon that makes it possible to fire. Ty guys that realy helped. Especially the part about static. Looking into that solved my problem in a heartbeat :)
Super_Hippo Super_Hippo

2014/10/24

#
As you use it as a static variable, you should make sure to set it to 'false' when starting the game. Static variables are only created when the scenario is compiled (not when it is just reset).
Alwin_Gerrits Alwin_Gerrits

2014/10/24

#
I started out setting it to false because the player would start without any powers. However, it works just fine the way it is now (for now at least) and I allso think i get static now. It's prety much that the value of a static is kept even if the class isn't created. So if you create a copy of a class. Then delete it or make another copy it will have the same value for the variable as the last time you changed it in the first class. In other words: it's like it remembers the value of the variable even when switching worlds
Super_Hippo Super_Hippo

2014/10/24

#
To give an example. In a scenario, you have enemies which are moving around. After a certain point of the game, you want the enemies to become faster. If the enemy class has the speed as a static variable, you can just increase this value and all enemies will be faster then. If you don't set the variable speed to the first value when starting the game, the enemies will still be faster when resetting the scenario. It is not enough to have it like 'public static int value = 5'. Instead, you need to reset the fields manually from the constructor of the world in which you start the game. Static variables are not bounded to an object, so you can set the value before any object is placed into the world.
davmac davmac

2014/10/24

#
So then does that mean it's like variables you declare as a global variable?
There aren't global variables in Java; there are only class variables (aka static variables), instance variables, and method-local variables. Static variables have (almost) global lifetime, and only a single instance, so in that sense they are like global variables in other languages. However, there is no global namespace for variables.
Alwin_Gerrits Alwin_Gerrits

2014/10/24

#
I see, thanks guys!
danpost danpost

2014/10/24

#
Maybe, instead of creating a new main actor in the new level, you should be using the same main actor. That way any and all states for that actor are not lost. The code could look something like this (although, it would depend on what class you are changing worlds in)::
Level1 oldWorld = (Level1)getWorld();
Level2 nextWorld = new Level2();
nextWorld.setMainActor(oldWorld.mainActor);
Greenfoot.setWorld(nextWorld);
This particular code would require that you had a method in your Level2 classes defined as follows (however, it could be defined more specifically as far as the class is concerned and named more appropriately):
 public void setMainActor(Actor mainActor)
This method should add the main actor into your Level2 world instead of the constructor of the class doing it.
You need to login to post a reply.