Way to choose gamestate?
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. */
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
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
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).
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 ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
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!
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!
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.
Always remember, you''''re unique. Just like everyone else.
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
(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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement