Welcome to the revival of RoguelikeDev’s FAQ Friday, now on the Fediverse!

The purpose for this event is for traditional roguelike developers to discuss design philosophy and share their technical insights and methods as well as field questions related to the topic.


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.


Original FAQ's on Reddit

all 2 comments

sorted by: hot top controversial new old
[–] 4 points 2 days ago

My game loop is single-threaded. I figure with small 2D maps and optimized native code, I should never need multi-threading. Plus, I target wasm, so I can't depend on parallelism for performance.

Ignoring UI and menu modes, there are two cases in the game loop:

  1. An animation is running: tick it
  2. Else: do regular state updates

Animation frames are represented as a simple list of (point, glyph) deltas to apply to the map. We render the map as normal, and then render the deltas as an overlay. Even there, there are a few wrinkles. I test each of the deltas for visibility before rendering them. If all of an animation frame's deltas are skipped, then we skip to the next frame during that rendering. In fact, if none of an animation's frames are visible, it won't any time at all, and we'll fall through to case 2. This check is actually relevant to gameplay! It means that players have no way of telling when an animated event occurs unless they can see (at least a portion) of the animation.

Another wrinkle in the animation system is that there may be callbacks that update game state queued between two frames. Adding in these callbacks was essential. For instance, for a fire attack, a callback might light a tile on fire only when the flame moves through it. For all future frames, that cell will be lit in the underlying map.

Compared to animations, case 2 - regular state updates - are trivial! I run the following logic:

  • Find the actor with the smallest time-to-next-turn
  • Advance time to that turn
  • Run plan() to choose an action, and act() to apply it
  • If the actor is the player, plan() can return "blockedOnInput" - that's when we break!
  • Else, repeat

Easy!

  • source
  • [–] 3 points 1 day ago

    In my first game Boohu, my game loop was messy, with game logic interleaved with drawing, but my later games Harmonist and Shamogu use Gruid's Update/Draw loop attached to a model (containing state), inspired from Elm's architecture and bubbletea's, but a bit less "functional-style" to adapt it to a grid-based roguelike needs.

    The Update method is called with an input message each time there's an incoming one. It updates the state of the game, and it may return one or more Effect values. Effects are functions that are run asynchronously on a goroutine, and may send one or more custom messages to the Update game loop. This is useful for extra messages beyond the basic ones for input events from the backend (SDL, terminal or js/wasm). For example, I use it for interruptable animations, to send "next animation frame" messages at appropriate times, with a number identifier that ties them to a particular animation, so that they may be ignored if that animation has been interrupted (by a keypress or some other action).

    The Draw method is called after every Update and returns a grid of cells with color, letter (possibly represented by a tile), and attributes (possibly custom ones). The grid is typically cached, though a new one can be returned in a kinda functional way. Gruid then sends the grid to the backend for screen drawing and to the replay recorder, but it only sends the grid changes from previous frame, not the whole grid (unless screen was exposed). That system means that, following the spirit of typical terminal applications, when the game is idle with no animations going on, no drawing happens and the cpu is idle. This is nice, because typically animations are pretty short, so having a drawing function getting called all the time even when nothing changed would've been wasteful.

    Both Update and Draw dispatch to different code paths depending on the model's state (typically to distinguish between map mode, inventory mode, new game screen, and so on).

  • source