Skip to main content
gamedev.net
🔒 Locked

[java] switching gamestates - ugly?

Started by morebeer 5 replies 1.5k views
Original Post
morebeer
morebeer
for handling different gamestates in my applet, i have something like this:
interface GameState
{
 public boolean onKeyDown ();
 public boolean onKeyUp ();
 public void run ( long curTime );
 public void draw ( Graphics g );
};
Several gamestate classes are derived from this interface, and the applet stores an "GameState ActiveState;" variable somewhere, and just delegates all events to it. my question is, would it be a bad coding technique to switch gamestates like in the following example, where i just switch the "global" ActiveState inside of another gamestate class:
public void run ( long curTime )
{
 if ( Keys[KeyEvent.VK_ESCAPE] )
 { //back to the mainmenu...

   ActiveState = new MenuGameState ();
   return;
 }
}
(in C++, ActiveState (the ''this'' pointer actually) would have to be deallocated first - a bit ugly but it would work.) i hope you get the idea..
CaptainJester
CaptainJester
No. You might have something like this.


public class State {
public static GameState currentState;
}

public class SomeThreadClass ... {
public void run ( long curTime ){
if ( Keys[KeyEvent.VK_ESCAPE] ) { //back to the mainmenu...

State.currentState = new MenuGameState ();
return;
}
}
}




First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
tortoise
tortoise
I''d suggest trying to get the GameStates to do their job and their job only. Things like esc and such can be handled at the top level, then the active state can handle the input that it''s interested in. It doesn''t make sense that the states are aware of when switches need to be made, they''re not controlling the game the overall master class above them is. Something like


class MyApplet
{
...
private GameState activeState;

public void play()
{
for(;;)
{
if(keys[KeyEvent.VK_ESCAPE])
{
activeState = new MenuGameState();
}
// other outer level "meta" input handling


activeState.run(ticks);
}
}
}



Then the GameStates can just do their job and not worry about the rest of the game. The master class will have to know how the game flows and switch states accordingly.

In some cases a state is aware of state changes, maybe a level has ended and the active state knows a summary screen needs to pop up. Then you could have run return a value to indicate that.
Ditto Brother Rat
Ditto Brother Rat
My one suggestion would be to create a final static instance of each state instead of using new all the time.

public static final GameState MENU_STATE = new MenuGameState();

Then you could just say ActiveState = MENU_STATE and not be creating any garbage.

Also, I think that the states should handle state transitions. I think that''s what the state pattern is for. And I don''t think he needs to screw around with threads. run(long curTime) looks like it''s called once per game loop. Maybe he should call it update(long) to differentiate it from run() in Runnable.
morebeer
morebeer
this is a good suggestion, i will make them final i guess.

yup, no threads here, run(long curTime) is called once per main loop.


how would one handle state transitions properly? i always did some simple hacks for screen-fading in/out to another gamestate. i''d be interested in such a system.

Ditto Brother Rat
Ditto Brother Rat
I think you just have to keep track of when the state changes


if(activeState != previousState) {
doTransition();
previousState = activeState;
}
Arild Fines
Arild Fines
Why not have the run method of the state return another GameState? That makes state transitions one of the responsibilities of the state itself.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.