Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 28 May 2013 - 10:05 AM

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.



Sponsor:

#2 FLeBlanc   Crossbones+   -  Reputation: 3125

Like
1Likes
Like

Posted 28 May 2013 - 10:31 AM

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



#3 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 28 May 2013 - 11:12 AM

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, 28 May 2013 - 11:36 AM.


#4 FLeBlanc   Crossbones+   -  Reputation: 3125

Like
2Likes
Like

Posted 28 May 2013 - 12:34 PM

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.



#5 Kryzon   Prime Members   -  Reputation: 3314

Like
1Likes
Like

Posted 28 May 2013 - 01:01 PM

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.



#6 metsfan   Members   -  Reputation: 654

Like
2Likes
Like

Posted 29 May 2013 - 11:30 AM

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.



#7 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 29 May 2013 - 12:26 PM

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.



#8 Ludus   Members   -  Reputation: 970

Like
2Likes
Like

Posted 30 May 2013 - 02:29 AM

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.



#9 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 30 May 2013 - 07:53 AM

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();

   }

 

}



#10 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 30 May 2013 - 02:06 PM

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.



#11 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 30 May 2013 - 06:37 PM

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?



#12 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 30 May 2013 - 11:02 PM

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.



#13 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 30 May 2013 - 11:20 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS