• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nicholas Kong

Is the game loop suppose to run when the user is in the main menu?

12 posts in this topic

So I created the gameplay for my arcade shooter in Java. Gameplay has been tested to work fine.

 

Main Menu and game controls work fine in isolation.

 

Problems arise when I want to have a main menu and game control showing up before game play actually starts. I feel like I shot myself in the foot multiple times because many hours went by with no progress being made. 

 

The game structure is as follows:

User sees main menu and game controls: no game loop is running.

Users sees gameplay. Game loop is running but I cannot control the ship and certain game mechanics do not show up. Game loop has been tested to work fine if I test it separately from the other mechanics.

 

It seems integrating these features together is the biggest challenge which is strange in itself.

0

Share this post


Link to post
Share on other sites

Usually, the main menu is implemented as a separate state than the game loop.

1

Share this post


Link to post
Share on other sites

Usually, the main menu is implemented as a separate state than the game loop.

I'm pretty sure that is what I have. I created a Game State Manager class which uses enums to display and detect the game state's: MAINMENU,PLAYING,GAMEOVER.

 

So the main menu is not suppose to have a game loop then? Wouldn't the game not run if it is not in a game loop during the time the user sees a main menu screen?

Edited by warnexus
0

Share this post


Link to post
Share on other sites

A pattern that is commonly used for game state management is for each game state's Update method to return a pointer to the game state that should be made current. If null is returned instead, continue with the current state. So, for example, when the game is started the main menu state is created and set as current. When the Play Game option is selected, then a new Game state is created and returned during Update, so the state manager knows to discard the current state and set the new state as current. During gameplay, if Esc is pressed, a new state (one which keeps a reference to the game play state internally so that it can continue to render the view even though that state's updating is paused) is created for the in-game options menu. If Return to Game is selected, the stored Game state is returned by Update, causing the in-game menu to be discarded.

 

Each state implements its own set of Render and Update functions to perform tasks relevant to that state. The outside loop of your game runs the same, regardless of the currently active state; however, when currentstate->Update() is called, different things happen depending on the current state. You shouldn't be updating your game while in Main Menu, for example (since it shouldn't exist yet). You may need to draw the Game state while in Options menu, but if it's single-player you shouldn't be Updating the game state. And so forth.

2

Share this post


Link to post
Share on other sites

User sees main menu and game controls: no game loop is running.

Hi. I think you're mistaking 'game loop' with 'main loop'. Main loop being, the construct which keeps executing game states.

Your 'menu' state would belong to the same state class as your 'level 1', 'level 2' etc. These states themselves don't have loops that halt the program flow; They usually have the 'update' function that processess the current frame. What keeps the game executing is the main loop calling whatever current state is being enacted.

If you want subtleties such as having a pop-up menu screen during gameplay, cut-scenes, message-boxes etc. you can use a state stack. Read this article, it's great: http://gamedevgeek.com/tutorials/managing-game-states-in-c/

 

If null is returned instead, continue with the current state.

I remember someone posting around here that a state can return itself (instead of null) when it's desired to continue with it. It could be more intuitive for some.

1

Share this post


Link to post
Share on other sites

The game loop is nothing more than an infinite loop that prevents the application from closing.  However, during the main menu screen, the game loop should be dominated by the UI system.  There may be other systems at work such as the rendering system, or an animation system.  So yes, there is ALWAYS a game loop, it just isn't always doing the same things in each state.

2

Share this post


Link to post
Share on other sites

The game loop is nothing more than an infinite loop that prevents the application from closing.  However, during the main menu screen, the game loop should be dominated by the UI system.  There may be other systems at work such as the rendering system, or an animation system.  So yes, there is ALWAYS a game loop, it just isn't always doing the same things in each state.

Thanks for the tip.

0

Share this post


Link to post
Share on other sites

Perhaps this is already how you have things set up, but I'll try to explain a way of managing the loops of each state in your game.

 

Each of your states should have its own functions for each part of the loop (input, logic, render). So you have your main menu state which has its own input, logic, and render functions, and your gameplay state also has its own input, logic, and render functions.

Now, your program should have a main loop that always runs until the user quits the program. This loop is likely one of the first things your program executes. This main loop also has input, logic, and render functions. However, all of these functions call the state manager to execute the respective loop function of whichever state is active.

 

It should work something like this:

 

Start of Main Loop

Input-> calls the Input function of the active state

Logic-> calls the Logic function of the active state

Render-> calls the Render function of the active state

End of Main Loop

 

As you can see, if a state is not active its loop functions will not be called. While the main menu state is active, the gameplay state loop functions will not be called, and vice versa. The main loop is the loop that always runs, and it simply calls the loop functions of whichever state is active.

2

Share this post


Link to post
Share on other sites

Perhaps this is already how you have things set up, but I'll try to explain a way of managing the loops of each state in your game.

 

Each of your states should have its own functions for each part of the loop (input, logic, render). So you have your main menu state which has its own input, logic, and render functions, and your gameplay state also has its own input, logic, and render functions.

Now, your program should have a main loop that always runs until the user quits the program. This loop is likely one of the first things your program executes. This main loop also has input, logic, and render functions. However, all of these functions call the state manager to execute the respective loop function of whichever state is active.

 

It should work something like this:

 

Start of Main Loop

Input-> calls the Input function of the active state

Logic-> calls the Logic function of the active state

Render-> calls the Render function of the active state

End of Main Loop

 

As you can see, if a state is not active its loop functions will not be called. While the main menu state is active, the gameplay state loop functions will not be called, and vice versa. The main loop is the loop that always runs, and it simply calls the loop functions of whichever state is active.

So that would mean main menu state and gameplay state are inside the main loop. 

 

This is the pseudo-code i came up with based on your feedback. 

 

while(isRunning)

{

   if(GameStateManager.getEnum() == GameStateManager.GameState.GAMEMENU)

   {

   input();

   logic();

   render():

   }

   else if(GameStateManager.getEnum() == GameStateManager.GameState.PLAYING)

   {

   input();

   logic();

   render();

   }

 

}

0

Share this post


Link to post
Share on other sites

Yes, that is one possible implementation. However, with a bunch of different states in your game, you may find that the code for the main loop will become quite cluttered. A better solution would be to have the loop functions of the main loop point to the loop functions of the active state. In C++ I use a pointer of the state class for this. I'm not sure what the equivalent of this would be in Java. In any case, this way your main loop will look the same no matter how many states you have. You'll just have to make minor adjustments to your state manager when you want to add a new state.

0

Share this post


Link to post
Share on other sites

Yes, that is one possible implementation. However, with a bunch of different states in your game, you may find that the code for the main loop will become quite cluttered. A better solution would be to have the loop functions of the main loop point to the loop functions of the active state. In C++ I use a pointer of the state class for this. I'm not sure what the equivalent of this would be in Java. In any case, this way your main loop will look the same no matter how many states you have. You'll just have to make minor adjustments to your state manager when you want to add a new state.

so you're saying a better implementation is for the main loop to figure which state of the game it is in and perform the following loop functions of that state?

0

Share this post


Link to post
Share on other sites

It would make the code of the main loop much cleaner and the code for the states themselves easier to manage, so yes - even though it's not necessary it is preferable.

0

Share this post


Link to post
Share on other sites

It would make the code of the main loop much cleaner and the code for the states themselves easier to manage, so yes - even though it's not necessary it is preferable.

Thanks, Ludus for a cleaner approach. I always like cleaner approaches.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0