GameState Architecture Questions

Started by
2 comments, last by DjMaSh 12 years, 2 months ago
Hey guys, a long time reader, first time poster. Currently I plan on starting up a fairly large scale project with a few friends and we're looking for a bit of feedback on the gamestate system we plan on implementing. The game is being written in C++, SDL, and OpenGL.

So I've read through old posts like http://www.gamedev.n...-the-gamestate/, and we've developed a bit of a hybrid system between several systems mentioned there. The application would have both a StateManager and a TaskManager, as we plan on eventually multithreading the program. The StateManager would call handleEvent(), update(), and render() on the current active state, as well as be able to set the new active state when the current state dictates. States would be broken up into things like an introState, MenuState, GameState, etc (fairly common).

Now within each state things will be broken up intro several tasks. For example the menuState will have a 3D scene being rendered in the background as one task while the menus are another. The gameState will have the "game" being one task (or a series of tasks in itself), and any inventory, in game menu, etc will be separate tasks. I've seen people talk about making these separate states, and even talk about using a stack so you can layer them on top of one another, making overlapping renders easy, but conceptually an entire state for a 3D game and then a state just for each small menu seems wrong. The taskManager would manage the tasks within each state, maintaining a priority list. As I mentioned before this will allow us to eventually implement multithreading much easier as individual tasks can do their work separately, causing minimal conflicts (obviously there are still a few concurrency concerns but we all have decent experience with multithreaded programs, I'm sure we can handle it smile.png)

So my question is what do you think? Are we missing anything? Does this seem like a workable framework for a large scale game project? As an aside, I've seen many sources talk about using singletons for managers and/or states and tasks since you'll never have two gameStates running, or your inventory open twice, but singletons make us nervous. I personally have always been taught to use them when they are the ONLY alternative (like a logging system). Also, with our currently described framework, will the overlaying of tasks (menus overlapping with a rendered game scene) be difficult without a stack?
Advertisement
The separation of tasks that you describe doesn't sound like a good candidate for multithreading to me. In general, separating persistent things like UI logic, game logic, rendering, etc ends up not gaining much from multithreading and makes coding/debugging it much more difficult. Threads are more useful (and manageable) for things like asynchronous resource loading or short term parallel computation (updating unrelated entity components simultaneously or a parallel algorithm for frustum culling, etc).

In my opinion the concept of tasks is a bit redundant since you will naturally separate things like entity logic, inventory management, and interface logic into separate modules anyway. To formalize them into tasks would not give you anything you didn't already have.

The separation of tasks that you describe doesn't sound like a good candidate for multithreading to me. In general, separating persistent things like UI logic, game logic, rendering, etc ends up not gaining much from multithreading and makes coding/debugging it much more difficult. Threads are more useful (and manageable) for things like asynchronous resource loading or short term parallel computation (updating unrelated entity components simultaneously or a parallel algorithm for frustum culling, etc).

In my opinion the concept of tasks is a bit redundant since you will naturally separate things like entity logic, inventory management, and interface logic into separate modules anyway. To formalize them into tasks would not give you anything you didn't already have.


Fair points. I think the decision to break down states to tasks may have been more motivated by our reluctance to classify menus and inventories as states, and end up with a huge laundry list of them. We intend to have several menus and interfaces that overlay the game world that can be opened and closed, so the ideal of concurrent objects seemed initially appealing but I suppose unnecessary. So if we wanted to have these interfaces, as well as maintain and render the states beneath them I imagine a state "stack" would be the ideal solution, with the top most states determining if the state beneath it should handle events, update, or render. Would the ideal of breaking large states (menu & game for instance) into a small subset of internal states that manage their own smaller stacks reduce clutter or just be more hassle than its worth? We're trying to get most of the large design decisions made before we start working on things to prevent having to re-code large portions just to add features X and Y.
To me it sounds like just a layer of fuzz that doesnt really give you much, and makes everything much more indirect.

Whats wrong with managing your threads manually rather than building some crazy formalised task system. i.e, if you do decide you want to split your ui and world onto diff threads just do this inside your game state

task1 = RunTask(world.update())
task2 = RunTask(ui.render())

task1.wait()
task2.wait()

Seems like this gives you way more flexibility, and allows you to modify which things are tasks easily

This topic is closed to new replies.

Advertisement