views:

47

answers:

2

I have not done any large game development projects, only messed around with little toy projects. However, I never found an intuitive answer to a specific design question. Namely, how are different types/states of UI modeled in games? E.g. how is a menu represented? How is it different from a "game world" state (let's use an FPS as an example). How is an overlaid menu on top of a "game world" modeled?

Let's imagine the main loop of a game. Where do the game states come into play? It it a simple case-by-case approach?

if (menu.IsEnabled) menu.Process(elapsedTime);
if (world.IsEnabled) world.Process(elapsedTime);

if (menu.IsVisible) menu.Draw();
if (world.IsVisible) world.Draw();

Or are menu and world represented somewhere in a different logic layer and not represented at this level? (E.g. a menu is just another high-level entity like e.g. player input or enemy manager, equal to all others)

foreach (var entity in game.HighLevelEntities) entity.Process(elapsedTime);
foreach (var entity in game.HighLevelEntities) entity.Draw(elapsedTime);

Are there well-known design patterns for this? Come to think of it, I don't know any game-specific design patterns - I assume there are others, as well? Please tell me about them.

A: 

Most GUI frameworks are event driven, that is: Menu elements (as well as buttons and other "input" widgets) are associated with a window and with a listener object that you provide. The framework takes care of calling the onAction (or something like that) method on your listener object whenever the user clicks on the menu/button.

You, as a programmer, does not need to write code that explicitly checks these menu elements.

Itay
Your answer does not have anything at all to do with traditional game programming, which does not necessarily use input widgets or listeners or even frameworks and windows. How is this relevant in game programming? I obviously do not mean Win32/Windows Forms game programming (e.g. Minesweeper), in which case you would use common GUI elements.
Sander
Even when not using a standard framework, a game program will typically roll out its own framework built around the same principles: objects that get input from the user have a listener object. The engine gets input events (mouse/keyboard/joysticks) finds which object they pertain to (by comparing the mouse location against the location of each object) and then propagating this events to the corresponding object (which, in-turn, will fire its listener)
Itay
+3  A: 

A menu in a game is pretty much the same as a menu in any application - it's a collection of GUI elements with behaviours attached to them. This applies whether you have a distinct menu state or not.

There are several reasons why games tend to have distinct menu states. One is because game menus date back to before we had usable GUI libraries for games and most of these menus would just be collections of sprites and some hard-coded routines to handle the mouse clicks. Another is because games often require very different input handling during the game to what is needed during the menu and therefore it's often easier to have totally separate routines handling these.

But really, there's little need for a distinct menu game state. Your game typically has a GUI, with some elements visible, some invisible. To bring up a menu you simply create or display the elements you need. You may also need to pause the game at that point. (Or not, if it's networked play.) If there's no game in progress yet, no matter. Typically, your system will be a much less simplified version of this:

// update
world.update()
gui.update()

// render
world.render3D()
world.render2D_overlays()
gui.render()

The last 2 stages could even be combined, if your GUI system is adequate.

Although I don't always follow my own advice, I think there's rarely a good reason to have a distinct top-level state machine for a game. There are a small number of objects which exist in a small number of permutations and you just have to update and render them all.

Kylotan
Yep I pretty much agree on this. You are restricted somewhat on console dev due to TRC/TCR so you have to handle a lot more states in a standard way such as 'user removed controller' or 'user removed mem stick' so I have seen some FSM implementations of this but to be honest, they are usually an overkill
zebrabox