Sponsored By

Rolling Pixels: Looping Fiasco

Adding a game turn loop and refactorings

Jahmal Brown, Blogger

December 18, 2023

3 Min Read



This ‘sprint’ was a good one! This branch of work was about separating a turn-based loop from the game’s render loop. I could see early on that trying to operate the game's turn-based rules from the render loop had problems. A quick Google search revealed agreement but no leads on solutions. 🤷🏾‍♂️ I might have been lazy about this; I found a solid article by Bob Nystrom late in my search after I made some decisions. I have the player selecting, clicking, and dragging, adding to a turn queue until “End Turn” is selected. That gets the non-rendering Turn Loop going. Game objects that can act push their turns to the action turn queue (a priority queue), and then the turn loop runs through the queue of commands executing them. Currently, they just make each game object fire off an animation, all at the same time!! LMAO! I needed a “finished my turn” event to pace the commands. There’s still one call dependent on the render update loop, but I feel like I got 95% there. Refactoring may provide another look at why or making it better.

Right now, I got:
1. The player sends actions to an ActionHandler queue.
2. The player ends the turn, and other game objects that can act send actions to the ActionHandler.
3. The ActionHandler currently sorts the queue by an int field called actionOrder in the commands. This is a random number for now.
4. It runs through the queue executing game object action/commands, waiting for an ActionFinished event before moving to the next command.

Commands have logic to process the action and signal the game object’s animation for the action. The animation event relies on the traditional render update loop to kick it off. I think this is OK, where it belongs.

Having Nystrom’s article and code to review is useful in seeing another approach to turn-based loops. Thanks, Bob!!

next turn

IMGui is installed; I may need it later for debugging. I’m using VSCode and haven’t gotten the debugger going yet. It won’t be the first time I’ve worked without a debugger! I cut my coding chops on VMS and hex dumps, baby!!

I’m looking at art and have a Pinterest board for this project. I’m weighing hiring a pixel artist for better demo sprites vs. taking a swing at it myself, but I have time; I am not sure if I’m going for a more Darkest Dungeons format where the adventurers are on one side and the opponents the other and cinematic animations display the results of actions. Or do I go with a 2.5ish view and move the sprites like RTS games with animations? I don’t know yet. That might be answered by playing with what I’ve got in this prototyping phase.

As I mentioned above, I have not found a lot of design talk about turn-based loop design on the social webs. If you have some references, leads, or thoughts, drop them in the comments –I’d appreciate it! I think what I have works..for now. I highly expect it to change as I adventure on!


About the Author(s)

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like