Archived

This topic is now archived and is closed to further replies.

BauerGL

Way to choose gamestate?

Recommended Posts

BauerGL    467
Hi, which way whould be the "best" to switch between different gamestates, to switch between initialize, menu, gameloop, single player game, multiplayer game, etc etc. I need something to split my game up in pieces. I''ve thought of making a variable which changes value (duh), like "Gamestate = Menu, Gamestate = SingleGame" etc, and then use a switch(Gamestate). Although that doesn''t sound too good :]. So if anyone got an idea feel free to post it =). Thanks. /* The only difference between a genius and a madman is the success. */

Share this post


Link to post
Share on other sites
JohnBSmall    881
I don't know about the "best" way, but you could try having a function variable (probably not the official name), basically a variable which just stores a pointer to a function. This would be your render function, then each time through the main loop, you use this pointer to call the right function. When you want to change states, change which function the variable points to.
Unfortunately, I don't know how you would do this in C/C++, as I use Delphi, but it should be possible.

John B

Edited by - JohnBSmall on January 30, 2002 4:59:36 PM

Share this post


Link to post
Share on other sites
amish1234    104
I''ve done it your way before (making a enum and a variable that can equal render, init, etc.). Probably not the best for performance. Now, I call my Init functions in the beginning of winmain. After the createwindow crap, and the init functions, it enters the message loop, which calls GameMain(). GameMain then calls all in-game functions such as rendering map, rendering models. (thats all I have in my engine).

Share this post


Link to post
Share on other sites
Kylotan    9860
I have objects for each state, derived from a base State class. They all implement some sort of ''Run'' function, which contains their main loop. It then returns the state to replace it. I need my Windows message pump in a separate thread that way however.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Oluseyi    2103
Could people please refrain from posting so much code? If it significantly exceeds a single page, post a link to it.

Furthermore, posting that much code doesn''t usually help as its easier to derive structure from abstractions. Did we really need all the function definitions? This is a pet peeve of mine - ie, I''m pissed!

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Greven    100
I''ve been wrestling with the problem myself for quite some time... What I''ve ended up doing is the whole enum{MENU,INGAME,GAMEOVER} thing. Someone mentioned that it is slow. Why would a switch/case be slow? It''s one If/Then call each frame to call the right function for what state you are in. Would it really slow it down that much? Personally I find it works rather well. For each function I call I will usually have a separate h/cpp file for clean code. Makes very organized. h/cpp file for menu code, startup code, in game code. Only real problem is that I end up with hundreds of global variables... But what is the performance problem with doing this?

Always remember, you''''re unique. Just like everyone else.

Share this post


Link to post
Share on other sites
Ruval    122
There''s not really any performance issues (it is just one switch statement per frame after all), but there are tidier ways of doing it.

I usually have something like
  
struct GAMESTATE
{
short stateID;
void (*Draw)();
void (*Update)();
bool (*Init)();
void (*Free)();
void (*Input)();
};


(That''s how you do C function pointers btw)

Then a function that assigns functions to those pointers for
each enumerated game state. Essentially it''s just moving the switch into a one-time function, but I find it encourages a bit more structure in my state-changing code.

Share this post


Link to post
Share on other sites
granat    122
Just use switch/case...Slow ??? Hell no...I mean..you only do 1 pr. frame...That''s nothing..Come on...Computers are fast these days....

Share this post


Link to post
Share on other sites
BauerGL    467
Wow! So many replies, I never thought I would get this feedback in so short time, thank you!

What I''ve made out of this is, since I haven''t done anything with function pointers yet, I''ll try to wait with that. I''m going to try the "enum gamestate" way. So please correct this if I''m wrong:

enum gamestate { Init, Menu, Game, Exit, etc etc }

switch(gamestate)
{
case Init: Init_Funtion();
case Menu: Menu_Function();
}

and something like this:

if(key[VK_ESCAPE])
gamestate = Exit;


Did I get that right(except for the syntax)?

Thank you VERY much again!


/* The only difference between a genius and a madman is the success. */

Share this post


Link to post
Share on other sites
BaShildy    122
I posted it all of that because software engineering is really tough to implement. I thought if i just gave him only a brief overview of what to do that he may still make mistakes that I''ve done in the past, so i just showed a rudimentary 3 module, 3 state game strucutre. Sorry to piss you guys off If anyone wants the original code feel free to email me at kking@digipen.edu .

- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com

Share this post


Link to post
Share on other sites
Oluseyi    2103
quote:
Original post by BaShildy
I posted it all of that because software engineering is really tough to implement. I thought if i just gave him only a brief overview of what to do that he may still make mistakes that I''ve done in the past, so i just showed a rudimentary 3 module, 3 state game strucutre. Sorry to piss you guys off If anyone wants the original code feel free to email me at kking@digipen.edu .

It''s not that serious. I just feel (very strongly) that it''s sometimes easier to communicate by saying less.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites