Sign in to follow this  

events over time vs. game loop

This topic is 2847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys I've always been confused about something. I'm fairly competent at coding a game loop: get input -> update -> render -> repeat. It's straightforward, etc., etc. BUT I've never been able to handle sequential events in this framework. For example, how do I go from a Lobby to a server to the in-game using a loop? How do I execute code that has to happen over several steps -- such as following a state machine? How does this fit into a game loop? It almost seems like it would be more at home in a text-based game that waits instead of looping, and uses gotos.

Share this post


Link to post
Share on other sites
getinput->update->render->repeat

So now we want to add a lobby what does it mean to update in the lobby surely this is different than in game. what does it mean to render the lobby surely this is different than what it means to render the game. Basically you need some sort of dynamic dispatch weather it be a polymorphic call on a game state class or a big switch statement on a current location to do what is necessary for the current state of the gameworld.

As far as state machines go say your character has a reload delay, they enter the reload state you start the reload animation and record when that started, and move on. When you come back around in the next update loop you check the difference in begin/now time and see that the character is still reloading so you move on. When you finally check and see that the reload delay is complete transition to the next state.

Share this post


Link to post
Share on other sites
This is often handled by either a proper state machine or just a simple switch block for your main loop


while (1)
{
switch( gameState )
{
case GAME_RUNNING:
updateGame();
updateUI();
render();
break;

case GAME_PAUSED:
updateUI();
render();
break;

case SERVER_LOBBY:
updateUI();
render();
break;
}
}



Or whatever. Just a nonsense example to wrap your head around it. There are infinitely cleaner ways of implementing this. But essentially, you are correct that games have various states and that your main loop needs to change depending on that state. A switch block like above can be easy to implement and often does the job; if you're a glutton for cleanliness, a proper state machine may be required. The latter can be overkill but depending on the frequency of state change and the complexity of your states, it may be the correct solution.

-me

Share this post


Link to post
Share on other sites

This topic is 2847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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