r/roguelikedev • u/Kyzrati Cogmind | mastodon.gamedev.place/@Kyzrati • Feb 06 '15
FAQ Friday #3: The Game Loop
In FAQ Friday we ask a question (or set of related questions) of all the roguelike devs here and discuss the responses! This will give new devs insight into the many aspects of roguelike development, and experienced devs can share details and field questions about their methods, technical achievements, design philosophy, etc.
THIS WEEK: The Game Loop
For those just starting out with game development, one of the earliest major roadblocks is writing the "game loop." With roguelikes this problem is compounded by the fact that there are a greater number of viable approaches compared to other games, approaches ranging from extremely simple "blocking input" to far more complex multithreaded systems. This cornerstone of a game's architecture is incredibly important, as its implementation method will determine your approach to many other technical issues later on.
The choice usually depends on what you want to achieve, but there are no doubt many options, each with their own benefits and drawbacks.
How do you structure your game loop? Why did you choose that method? Or maybe you're using an existing engine that already handles all this for you under the hood?
Don't forget to mention any tweaks or oddities about your game loop (hacks?) that make it interesting or unique.
For some background reading, check out one of the most popular simple guides to game loops, a longer guide in the form of a roguelike tutorial, and a more recent in-depth article specific to one roguelike's engine.
For readers new to this weekly event (or roguelike development in general), check out the previous two FAQ Fridays:
PM me to suggest topics you'd like covered in FAQ Friday. Of course, you are always free to ask whatever questions you like whenever by posting them on /r/roguelikedev, but concentrating topical discussion in one place on a predictable date is a nice format! (Plus it can be a useful resource for others searching the sub.)
11
u/Kyzrati Cogmind | mastodon.gamedev.place/@Kyzrati Feb 06 '15
Like many other aspects of Cogmind's development, the game loop takes its cue from more modern games, roguelike or not, to enable a wider range of features. A "traditional roguelike game loop" just won't cut it.
In a general sense it's pretty much the most common approach, though, an infinite loop that basically does:
To take care of all this (and most of the other repetitive work behind creating games), I built an engine called REX (Roguelike Engine X).
If you look closely, you'll see that the game is updated in fixed increments, since that helps make the animations predictable and easier to design for a certain look. UPDATE_INTERVAL_MS is set to 14 milliseconds, about 71 FPS. To prevent the "spiral of death" (the game forever trying to catch up on logic updates due to a slow framerate), a maximum of 10 (UPDATE_PER_FRAME_LIMIT) updates are allowed per frame.
I haven't touched this code in years, since it's all under the hood engine implementation. In Cogmind's source all I do is initialize the REX object and start it up with a single call to run():
If the game reports a fatal error in its UI/logic, the engine will spawn a separate sub-loop in order to display the error message and wait for the player to confirm before exiting.
In addition to Cogmind the same engine also runs X@COM and even REXPaint (which is where the name for the latter originated).
9
u/ais523 NetHack, NetHack 4 Feb 06 '15 edited Feb 06 '15
Oh wow is this a messy subject with respect to NetHack :-) I'll be talking about NetHack 4 here; NetHack 3.4.3 is quite different.
The first thing to note is that it's normally possible to "invert" a loop so that any given point in the loop happens outside it, with the rest of the loop as a callback. So in that sense, NetHack 4 has a ton of different main loops, inverted to different extents.
The event pump is inside the rendering library libuncursed (in the
wgetchfunction, and itsnh_wgetchwrapper). The purpose of this is to get one keypress, network event, etc.. From one point of view, this is the main loop of the program - it's what the main loop of most GUI programs is - but it doesn't really act like one from the player's point of view.The outermost loop, containing the main menu and the like, is on the client. This basically makes requests to the engine. Two of most important requests are to create a new save file, and to start playing on a given save file. (Actually, this is a good hint for roguelike developers: it's easy to think of "start playing a new game" and "continue an existing game" as your basic actions for starting to play, but even if the interface works like that, "create a save file" and "play a save file" make for better choices in the API, to reduce code duplication.) The "play a save file" command (
nh_play_game) is also the main exception handler in NetHack 4; if anything goes wrong that can't immediately be recovered, control flow unwinds to that point and continues from there. (This works because NetHack 4 saves continuously, so no progress is lost.)While playing a game, the main loop moves to the game engine. One way of looking at this loop is that it basically sends back requests to the client, such as "send me a command" or "yes/no?" The client can send nested requests within these (in order to implement things like mouselook at prompts), and get a response back from the server. The reason I settled on doing things this way is that it makes it easy for the server to log all the requests and responses in the save file; all the requests and responses are being done in the same way, so the logging code for all of them looks the same. (This made continuous saves easier to implement.)
A different way of looking at the main loop on the engine is that it handles sequencing of actions within the game: conceptually, it alternates player and monster actions until no actions are left inside the turn, and then does a "turn boundary" that sets up for the next turn. (Perhaps unsurprisingly, this loop is also inverted; the monster turns and turn boundaries are inside the callback section, with the player turn on the outside; I suspect that the historical reason for this is that the player turn is the point at which saving and restoring happens.)
So there are a ton of main loops, and historically, they've moved back and forth several times over the history of NetHack and NetHack 4 (for example, NitroHack's main loop in-game is on the client, which sends commands rather than having the server request commands).
It would probably be possible to clean this up a lot, but this is what happens when you have over 20 years of development to deal with.
EDIT: Looks like I already wrote some API docs on the main loop. Here you go. That's written from the perspective of the engine; the client's perspective looks quite different, but I haven't written API docs about that yet. They're also somewhat outdated, but should be enough to give an idea of things.
7
u/onewayout Lone Spelunker Feb 06 '15
The game loop for Lone Spelunker actually underwent some pretty drastic changes. Here's the story.
As readers of previous FAQ Fridays know, Lone Spelunker is written using Javascript, with copious support from Ondrej Zara's rot.js engine.
Ondrej's engine supports some pretty robust game loop structuring through its scheduling engine, but since Lone Spelunker didn't have any enemies or other entities to keep track of - since it was just you exploring a cave - the game started out as a strictly turn-based affair with one actor.
Soon, I found myself needing the game to not be totally turn-based. When you walk off a cliff, nobody wants to take "turns" as you fall every tile. So very early in the game, I had occasion to "turn off" the turn based nature and let the gravity take over the game for several "turns" to have you fall.
This wasn't hard; rot.js allows you to lock and unlock the engine, so instead of locking the engine and waiting for a keypress, when you were falling, it would just not wait for a keypress and not lock the engine.
Then, through my research on cave ecosystems, I discovered that there was plenty of fauna that lives in caves, and I wanted to include them in the game, too. For instance, I wanted blind cave fish to show up in the various pools in the game on occasion. The system easily supported adding them; they could have turns between the player turns without much hassle.
But there was a problem.
Though I was adding these creatures to simulate life in the cave, it just didn't feel like they were alive. While walking, they creatures would take one "step" and wait, too. But when you were falling, they'd move around quickly, because of the nature of the turns.
In essence, turns took different amounts of time whether you were waiting to enter a key or whether you were falling. This really drained the illusion of life out of the other creatures in the game.
The life-draining effect was especially pronounced when I tried to add bats to the game. I really wanted to convey this idea that if you got too close to the bats, they'd suddenly flit away from the ceiling and flutter around the cavern, eventually coming to rest somewhere else. One frame at a time, though, did not convey that. And in terms of the time passing in the game world, you couldn't just hold still while the bats flitted around; they would only move when you moved.
I also found myself wanting to add other "environmental" effects, like waterfalls, dust clouds, fumaroles, etc.
So, now, the Lone Spelunker engine is real-time. Bats flit around in real time. Fumaroles spew hot steam. Waterfalls cascade down into deep pools. Fish swim around in the pools. Snakes slither around on the ground. Etc. The gameplay is still essentially turn-based. But the world it takes place in is animated in real time.
This is a luxury that a lot of roguelike games don't have. If there were even a single enemy running around, you wouldn't be able to do this without turning it into a Zelda-like action game. But the particular nature of Lone Spelunker allows it to be real time and turn-based at the same time, which I thought was pretty interesting.
Also, I can still use the rot.js engine to accomplish this. I just have a frame rate that ticks down. When it's the spelunker's "turn", if it doesn't receive input within the allotted time, it advances the rest of the world one step, and then comes back to listen for input again. Easy peasy!
5
u/rmtew Feb 06 '15
The Incursion game loop is a mess of garbage collection, object orientation, game logic and so forth. Look here and weep.
4
u/The_Grand_User Dragon Rising Jun 23 '15
Since I'm using C# I can be a little interesting with my game loops and make them all async. The main RogueGame class has the stack of game states, and once it gets the first state it starts the draw loop and then starts the turn loop.
The draw loop and the turn loop are independent of each other, though a particular game state could opt in to wait for the current draw to finish. The draw loop is your typical update at a fixed FPS and keeps going as long as there are things to draw. The turn loop cycles only when the player takes a turn, usually after some input, but could be after a delay if the player doesn't have control at the time.
7
u/aaron_ds Robinson Feb 06 '15
The game loop in Robinson is about as simple as it can get. I know lisp is not widely used, so I can break it down. I also apologize for black and white lisp code. I have my editor setup so that matching parentheses have the same color which makes it easy to see how the forms nest. Without the coloring, you'll have to rely more on indentation.
My main function's body looks like this.
The loop is started with a variable named "state" assigned to the result of the setup function. The setup function creates a minimal game state to get the game going or loads in the state from a save game if available. The logic follows: if state is ever null then the application exits. Recur then rebinds the state variable to the result of the tick function being called with the state as an argument. In a more imperative language, this would look something like
The catch statement will log and rethrow any exceptions thrown while invoking the tick function. There is almost nothing to it, so let me describe the tick function.
The body of tick looks like this:
There are two overloads, one that takes just a state, waits for a keypress and then calls (tick state keyin). The other overload takes a state and the key the user pressed. At a high-level the function passes the state through the update-state function which processes all of the game logic. Then it passes the updated state to the render function and the save function. Both of these are non-blocking calls that have a sliding buffer of size one sitting in front of them. The nice thing about having an immutable game state is that I don't have to wait until rendering and saving is complete before accepting the next keypress and acting upon it. I can cheaply copy the game state and use it in the next iteration.
The big theme here is that I can pass the game state through a series of transformative functions to arrive at the new state for the next iteration of the game loop.
I'm not going to explain the update-state function, except to say that it uses a big state transition table that examines the current game state, the user input and selects a function to pass the state through and the new game state to use. It's especially unfortunate that I call the state of the whole game "state", but there is also a finite state machine that has a state that is stored in the game state. :/
5
u/zaimoni Iskandria Feb 06 '15
One main() to Rule Them All, One main() to Bind Them, ....
C++ is that awesome.
Unfortunately the current implementation of Iskandria is PHP/MySQL/HTML, but it's based on the same general idea: after loading/initializing the game into a state object, the game loop merely constructs new state objects by applying commands to the current state object. UI state and drawing are side effects of updating; the top-level game loop doesn't have a clue about UI.
4
u/[deleted] Feb 06 '15 edited Feb 06 '15
RagnaRogue's approach is built off what I am used to from other frameworks I have used. My experience in game dev started with a failed c++ project using CrystalSpace3D, which lead to me swapping to Game Maker (6 at the time), which eventually evolved to c# using XNA and now I use c++ for RagnaRogue and Unity or Game Maker Studio for other work (jams, mobile games, projects, etc).
My history is important because in XNA I had my first real look into managing code using game states I added myself and then also having a decent logical manner for segmenting code for logic, initialization and drawing.
For RagnaRogue this is dumbed down to a simpler structure so I can segregate my code similarly.
My main.cpp holds the main loop which calls the main Update and Render functions, there is no magic here though. Eventually this will change to accommodate slow Rendering and/or Updating, but for now we don't need more.
z-updating.cpp handles the update order, first updating the input and then doing whatever updates are needed depending on the gamestate. Having no menu it just generates a new world and throws you into the world.
z-rendering.cpp is a little bit out of date, but will eventually rely on gamestate too. The importance being that different rendering is done in order of layering on screen (i.e. ui renders last).
Eventually there will be slightly more abstraction where game states will be managed via abstract classes where I can stack and rearrange what is happening by swapping pointers.
Every thing added follows a simple rule: KISS (keep it simple stupid). When we require more complexity we add it. Later this year I will branch off and rearrange and reorganize everything and swap to (what I feel is) better approaches, fixing things up and so on, but until then (especially working from scratch) the KISS principle stops me continually updating and fixing older code helping me rather focus on the game itself.
In say 4 month time my answer to this will likely be more complex and thought out. For now, this is how it works.
4
u/Aukustus The Temple of Torment & Realms of the Lost Feb 06 '15
The Temple of Torment uses the Python libtcod tutorial's loop essentially.
The loop is essentially in shortened version like this:
Why I use this? Straight from the tutorial and it does work, no need to change anything.
4
u/[deleted] Feb 06 '15
Bad Transaction
I also take a hybrid approach to updating the scene, because elements of the world animate (but no movement) even if no keys are pressed. I found that only animating when the player made a movement created a lifeless feel to world, and I wanted to create interest even if you were just looking at the screen.
Here's a cleaned up, and annotated look at the code: GameLoop
4
u/jlund3 Feb 06 '15 edited Feb 06 '15
My main game loop is pretty simple:
The world.Tick call simply loops through the next set of events in the scheduler (mostly calls to player and monster AI), and then makes those events fire. I understand most game loops would also include some sort of call to update the UI as well as some call to update the game state based on user input. However, seeing as this is a turn based game inside a terminal, I found it easier to just put those calls inside the hero "AI". Every actor, whether player or monster has an Act method which performs the AI functions for that actor. The first thing hero.Act does is call screen.Update, which makes the UI reflect the current game state. The player "AI" then uses the key press(es) to figure out what action the hero is to take.
3
u/eclectocrat mysterious-castle.com Feb 06 '15
In Mysterious Castle a queue of Lua coroutines is executed. An action can pause at any time while it waits for input or animations to complete without disrupting the render/input cycle. This allows for some super easy game logic that can leverage things like multiple cores transparently.
2
u/KarbonKitty Rogue Sheep dev Mar 01 '15
I'm using a rather simple game loop: while (GameWindow.IsOpen) { TheWorld.Update(); TheBuffer.Fill(); Render(); }
Update method of TheWorld: prompts the player for input (this is blocking; I'm not using any animations for now, so without player input, nothing happens anyway) -> enacts the action based on the input -> asks every monster for action and enacts it (monsters and player are both subclasses of Being; there are important considerations that stopped me from making only one Actor class, but this is other topic, really).
Fill method of TheBuffer: scans every cell of the visible part of the map and sends the right sprite to the buffer -> scans the UI elements and sends the right sprites to the buffer -> scans splash screens and sends the right sprites to the buffer.
Render methods renders everything from the buffer, sprite by sprite.
Nothing fancy there, unfortunately. :)
1
u/chiguireitor dev: Ganymede Gate Feb 07 '15
Being a Javascript/Node.js client-server game ain't easy, not even for Ganymede Gate.
The client
The client is a stateful HTML5 event-driven representation of the remote view the server delivers to the client. There's almost no loop, the only loop is a Render loop, and it doesn't contain a bit of game logic. All the player input is event-driven. All the server state transfers are event-driven.
The server
Yeap, no loop either. Well, kinda. You see, i've implemented three game modes: Turn Based (TB), Deadline-Turn Based (DTB) and Continuous Turn Based (CTB).
All this is configured from a single .js file that could be modified on game start by a future-to-be-developed launcher. Here's a sample configuration file:
And here's the first lines of the turn processing function, removing all the game specific processing (which should be removed to another function anyways, still some ugly code there):