Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Handling Game State

Author
Message
Jrock
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Riven
Posted: 3rd Feb 2011 04:53
Hi,

I've hacked up a sort of OO-wrapper on top of DGDK, and I'm curious about how to handle game states. I'd rather not use a switch statement, as I think this my code look like spaghetti. Using a switch statement also makes it impossible to load content on-the-fly, since I cannot declare and initialize pointers in one case and use them in another due to scoping.

Any ideas on how I should handle states?

Thanks.

Practice makes perfect. But if nobody is perfect, why practice?
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 3rd Feb 2011 15:09 Edited at: 3rd Feb 2011 15:38
Using OOP! Make a class such as 'GameState' and make other states that inherit this, such as 'Menu', 'InGame' and whatever else. How you handle the rest really depends on what coding patterns you like, but one method would be to make the base 'GameState' class maintain a pointer to the current game state that's set via a static variable and in its constructor you would check to see if this variable is already set, if so, then delete that. This will ensure that only one 'GameState' instance is active at any time, and it allows you to easily switch between them by simply doing: new Menu(); example(untested):


Or using templates for nicer syntax:


Note: This code is unsafe, in that switching states within a game state's method is effectively the same as 'delete this;', so you wouldn't want to access any class variables after switching states, or you could improve it so the class is only deallocated once the derived functions have returned.

Jrock
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Riven
Posted: 4th Feb 2011 04:00
Thanks for the response. However, I'm a bit confused as to how one might implement this in a GDK main loop. Could you mash up some implementation pseudo-code?

Thanks again.

Practice makes perfect. But if nobody is perfect, why practice?
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Feb 2011 04:32 Edited at: 4th Feb 2011 04:33


This is the entry point and main loop in my example, where 'int main()' is effectively 'void DarkGDK()' and 'for ( ; ; )' is usually 'while ( LoopGDK() )'. You'll also want to replace the 'nullptr' instances with 0 or NULL if you're not using VS2010.

Jrock
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Riven
Posted: 4th Feb 2011 05:24
I see now, thanks.

Finally, I have one more simple structure question (that may slightly diverge from the original topic). Let's assume I've written a wrapper class for handling darkbasic objects called dbObject. In the LoadingGame state, I write something like


How, then, do I use this pointer in another state? If I switch to the LevelOne state, then that pointer goes out of scope.

Ah, let me generalize that question. How do I handle the loading of content with your state manager? I feel like I'm missing a very simple concept.

Thanks!

Practice makes perfect. But if nobody is perfect, why practice?
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Feb 2011 06:14
You'd want to use some type of object manager class, that exists above each state, so switching between them won't be a problem.

In my games I achieve this via an entity system, where each type of entity(player, item, spaceship, etc) is a class and an EntityManager class stores a list of all active entities and calls update() on them every frame. This means the game states just create new entities and don't necessarily store a pointer to them.

Login to post a reply

Server time is: 2024-11-19 14:30:58
Your offset time is: 2024-11-19 14:30:58