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
