Slick Forums

Discuss the Slick 2D Library
It is currently Tue May 21, 2013 3:59 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Jun 18, 2012 10:52 am 
Offline

Joined: Mon Jun 18, 2012 10:31 am
Posts: 7
Is there any reason why I shouldn't store the StateBasedGame parameter passed to init() method of BasicGameState, and use it afterwards? And if I store it in some base class, and use it in subclasses, will I run into problems later on? I'm using some methods which need access to StateBasedGame, and don't want to send it as parameter every time. Everything seems to be working for now, but I'd rather not rely only on that.

And similarly for Input; can I get it only in base class' update() method, store it in a property, and access from subclasses?

Example:
Code:
public class GameState extends BasicGameState {

   private int stateID = -1;
   private static Stack<Integer> statesStack = new Stack<Integer>();

   protected Input input = null;   
   private StateBasedGame game = null;
   
   public GameState(int stateID){
      this.stateID = stateID;
   }
   
   @Override
   public int getID(){
      return this.stateID;
   }
   
   @Override
   public void init(GameContainer container, StateBasedGame game)   throws SlickException {
      this.game = game;
   }



   @Override
   public void update(GameContainer container, StateBasedGame game, int delta)   throws SlickException {
      input = container.getInput();      
   }
   
   
   protected void enterSubmenuState(int nextStateID) {
      statesStack.push(this.stateID);
      this.game.enterState(nextStateID);
   }
   
   protected void enterPreviousSubmenu() {
      if (! statesStack.empty() ) {
         this.game.enterState(statesStack.pop() );
      }
   }
   
   protected void clearSubmenuStack(){
      statesStack.clear();
   }

}


Code:
public class MenuState extends GameState{
   
   public MenuState(int stateID){
      super(stateID);
   }

   @Override
   public void init(GameContainer container, StateBasedGame game) throws SlickException {
      super.init(container, game);
   }

   @Override
   public void update(GameContainer container, StateBasedGame game, int delta)   throws SlickException {
      super.update(container, game, delta);
      
      if (input.isKeyPressed(Input.KEY_ESCAPE)) {
         enterPreviousSubmenu(game);
      }
   }   
}


The main reason why I'm asking is because if this approach is OK, then passing StateBasedGame to update() and render() seems pointless; couldn't it have been done in such a way that game is passed to constructor or only to init() and stored internally? Or is this the only way to make sure that those methods actually DO get the game, as somebody could just override the constructor/init() and not set the game parameter...

Anyway, many thanks, and sorry if I complicated it a bit :|


Top
 Profile  
 
PostPosted: Thu Jun 21, 2012 8:33 pm 
Offline
Regular

Joined: Thu Sep 22, 2011 4:39 pm
Posts: 165
Location: Belgium
i'm not sure if i get it right but the sbg is passed by reference so there is no cost
i would rather advise you to change the statebasedgame class with the methods needed instead of keeping the reference in your class locally
cause then you miss the framework

example:
in my game i had a need for a resume, pause, start and exit method, then you can implement it on your state extending the BasicGameState

Code:
public StateBasedGame(String name) {
        this.title = name;

        currentState = new BasicGameState() {

            public D getID() {
                return D.NULL;
            }

            public void init(GameContainer container, StateBasedGame game) throws SlickException {
            }

            public void start(StateBasedGame game) throws SlickException {
            }

            public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
            }

            public void resume(D ZONE) throws SlickException {
            }

            public void pause(D ZONE) throws SlickException {
            }

            public void exit(GameContainer gc, StateBasedGame sbg) {
            }

            public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
            }
        };
    }


Top
 Profile  
 
PostPosted: Mon Jun 25, 2012 1:33 pm 
Offline

Joined: Mon Jun 18, 2012 10:31 am
Posts: 7
Thanks for the reply!

Moving the methods into the StateBasedGame is indeed better concept, I can't believe it didn't occur to me.

However, my main point still stands (this was just an example usage, to keep it minimal): I made a class extending BasicGameState, which all my other states extend. I've split the logic in the update() method - right now it's only calling processKeyboardInput(), but there will be more methods in the future (processMouseInput(), calculateSomething() ). In order to avoid having the method as procesKeyboardInput(StateBasedGame sbg), I store the sbg (and container) locally in the init() method. Is there a reason why this would be bad?

Quote:
(...) instead of keeping the reference in your class locally cause then you miss the framework

I'm not sure I follow you, could you please elaborate?


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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