Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 10:39 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Aug 24, 2012 7:49 am 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
How should I go about doing this?
I tried making a global variable in my main class, but it seems to resort back to it's initial value when switching states.
How do I go about doing this?


Top
 Profile  
 
PostPosted: Fri Aug 24, 2012 9:32 am 
Offline
User avatar

Joined: Sat Jan 14, 2012 1:05 pm
Posts: 28
Location: Leipzig, Germany
In the process of making your game you'll probably end up with some basic core classes like for instance a GameLogic.class or a World.class containing all your entities, EntityManager, NetworkSubsystem, etc..
See what other variables you may need in the future and if they have something in common. Then you can put them all in one class and either a) extend your gamestate, so that you can pass a reference to this class in the constructor, b) set the reference via getter/setter upon creation of the gamestate or c) make the class a singleton.

I usually have a singleton instance of World.class where all units, equipment, positions, etc. resist. This class can be accessed from everywhere via it's static methods.
Same goes for a ResourceManager which contains all needed animations, graphics, etc..

_________________
team red


Top
 Profile  
 
PostPosted: Sat Aug 25, 2012 6:05 am 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
Monkeychow wrote:
In the process of making your game you'll probably end up with some basic core classes like for instance a GameLogic.class or a World.class containing all your entities, EntityManager, NetworkSubsystem, etc..
See what other variables you may need in the future and if they have something in common. Then you can put them all in one class and either a) extend your gamestate, so that you can pass a reference to this class in the constructor, b) set the reference via getter/setter upon creation of the gamestate or c) make the class a singleton.

I usually have a singleton instance of World.class where all units, equipment, positions, etc. resist. This class can be accessed from everywhere via it's static methods.
Same goes for a ResourceManager which contains all needed animations, graphics, etc..


So why doesn't it work when I set a global variable in my main state that has the list of other states, etc.


Top
 Profile  
 
PostPosted: Tue Aug 28, 2012 7:23 am 
Offline
User avatar

Joined: Sat Jan 14, 2012 1:05 pm
Posts: 28
Location: Leipzig, Germany
Mmh, this should actually work. What exactly do you mean by global variable?

Usually you should have your game object defined static in your main class anyway. So why don't just use this?
Code:

public class MyGame extends StateBasedGame {

   public static StateBasedGame game;
   public static ArrayList<BasicTWLGameState> states = new ArrayList<BasicTWLGameState>();

   public static void main(String[] args) {
           game = new MyGame();
   }
   
}

and then in some state:
Code:
public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        String s = ((MyMainMenustate)MyGame.game.getState(1)).test;
        g.drawString(10f, 20f, s, col);
}
public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {
   MyGame.states.get(0).somePublicVar = 12;
}

Both methods should work to get the desired effect but look rather ugly :)

_________________
team red


Top
 Profile  
 
PostPosted: Tue Aug 28, 2012 7:56 am 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
Monkeychow wrote:
Mmh, this should actually work. What exactly do you mean by global variable?

Usually you should have your game object defined static in your main class anyway. So why don't just use this?

Both methods should work to get the desired effect but look rather ugly :)



I'm trying to make a game where if you walk up off screen, you appear at the coordinate 0 on the next screen, with the same x coord

To do this I set a variable like so:
Code:
public class Game extends StateBasedGame {
public static float Xcord;
//states
public static final int menu = 0;
public static final int play = 1;
public static final int options = 2;
public static final int thanks = 3;
public static final int opening = 4;   
public static final int level2A = 5;
public static final int level2B = 6;
}


and then setting that variable in one state in the update method
and then saying the character starts at or is rendered at (Xcord,0);
however, when the game transitions between states, it sets the Xcord back the value it is set innitialls appose to what I set in the playing state before it.

I'm just wondering how I go about stopping this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group