Hello! Welcome once again to the WalkScape dev blog.

For the past week or so, following our most recent update, Iā€™ve been laying the groundwork for our Quality of Life (QoL) improvements. Before introducing any new QoL features or changes (with Gear Sets being the first on our list) I decided to start by optimizing and overhauling the game engine itself. This has been in the planning stages for quite some time and will likely take a couple more weeks to complete.

I know we have many programmers and game developers in our community, so this dev blog will feature some technical details especially geared towards you. I hope youā€™ll find it interesting! But before we dive into the tech stuff, Iā€™ve got some teasers and general info for those who prefer the less technical side of things!

Teasers and general info

Alright ā€“ letā€™s start with the teasers! We have two new skills coming to the game, hopefully in the next major content update. Hereā€™s some art to help you figure out what they might be:

Two new skills coming to the game!

The design and art process for these new skills, related activities, and items is already underway. However, it will take at least a few months to complete, as much of the designed content depends on the Quality of Life (QoL) updates.

In general news, if you missed it, we released a minor update and hotfixes to the game last week:

Reddit post

Portal post

Lemmy post

These fixes addressed many of the remaining issues, though a few minor ones still persist. Weā€™ll tackle those in future updates.

Lastly, myzozoz has been hard at work creating and designing the ā€œParty system.ā€ Progress on this front has been great!

Why the engine overhaul is needed?

There are four major reasons for this overhaul:

  • To improve game performance, reducing loading times and eliminating UI lag.
  • To enable processing the game loop for notifications, on the server and potential future companion apps (such as for smartwatches) more easily.
  • To enhance stability and introduce elements that increase the gameā€™s scalability and flexibility for planned features and content.
  • To address the issue of uncounted steps by implementing a game state rollback feature when step loading is interrupted (e.g., by closing the game or losing internet connection).

Currently, the main cause of lag (especially when loading a bunch of steps) is that the game processes all heavy calculations on the same CPU thread that renders the game (Flutter Isolates provides more information). This approach worked well for a long time, as the game loopā€™s calculations were simple enough to avoid lag. However, as the gameā€™s complexity has grown, particularly when loading thousands of steps, it can now cause several seconds of ā€œfreezing.ā€

The solution? Run the processing on a separate thread while optimizing the code as much as possible.

Game loading time

Iā€™ve begun this overhaul by reworking how the game loads, which will likely be the most significant ā€œvisible changeā€ for our players upon release.

By implementing multi-threading, Iā€™ve reduced the gameā€™s loading time from a 5+ seconds to be around 500ms (excluding server calls, which Iā€™ll optimize soon). Previously, the gameā€™s data had to be processed synchronously (loading one file at a time). Now, using multi-threading, or ā€œisolatesā€ in Flutter, we can load all game data simultaneously. This approach doesnā€™t compete with the UI thread for resources, making it even faster in practice. Our goal is to reduce the loading time so the game is ready before the Not a Cult logo fades away, as long as thereā€™s no update to download and you have solid connection.

The main limitation of using isolates in Flutter is that they donā€™t share memory. This means the game needs to load in the isolate, which saves the data to its memory and then sends the necessary parts back to the UI thread for rendering. This brings us to our next point: decoupling!

Decoupling game logic from the application

What is decoupling?

Previously, the application and the game processing logic were tightly coupled. This is common in many games, as decoupling isnā€™t always necessary, although it is a good practice. For WalkScape, several factors have necessitated decoupling the logic from the game into an entirely independent module.

The primary reason for decoupling is to enable notifications. We can then run the game loop quickly in the background to process steps and provide accurate information about your progress. This can also be used in things like home screen widgets, smartwatch companion apps, and whatnot. Additionally, we can move some game processing to the server side where necessary. Since WalkScape server also uses Dart (the programming language for Flutter), sharing the code is seamless. This approach allows us to process everything locally for the single-player version of WalkScape as well.

To achieve this, Iā€™ve created a separate module (known as packages in Flutter development) for the game. This self-contained module includes all the gameā€™s classes, logic, and messaging between the main thread and the ā€œgame processingā€ isolate threads. We can add this package to any application, effectively sharing it between the game, development tools, server, and potential future companion apps.

The main challenge in creating such a module is ensuring that it remains agnostic to whatā€™s running it, thus achieving proper decoupling. Moreover, when the server processes game logic, it must be able to handle multiple players concurrently, necessitating a stateless design (for a detailed explanation, see this Stack Overflow comment). The concept that can tie it together is called an Event Queue, which you can read more about here.

While most games implement a similar structure, and WalkScapeā€™s engine already had a comparable system, it wasnā€™t fully decoupled or stateless. So, our next challenge is: how do we achieve that?

Making a stateless Event Queue

Iā€™ve been preparing the engine for this overhaul for months, as such a massive change couldnā€™t be achieved quickly. Most components needed modification to become stateless.

To achieve statelessness, every Event in WalkScape (such as ā€œcomplete action,ā€ ā€œgain item,ā€ ā€œarrive at location,ā€ ā€œopen treasure chest,ā€ or ā€œgain XPā€) is its own, isolated function. These then receive a state (typically the Player Character) and return the updated state. Hereā€™s a simplified example:

  • Player loads 1000 steps.
  • This is sent to the Event Queue with the current Player Character as ā€œLoad Stepsā€ Event.
  • Event Queue processes the ā€œLoad Stepsā€ Event with the received Player Character (currently doing the ā€œCut Birch Treesā€ activity for example) and 1000 steps as inputs.
  • It processes ā€œcomplete actionā€ Events, forwarding the Player Character and returning updated versions as needed.
  • After processing all steps or encountering an interruption (e.g., becoming overencumbered), the Event stops and returns the fully processed Player Character.
  • The updated Player Character is then saved to the server, stored locally, and sent to the UI thread to render the new game state.

This approach pretty much guarantees that as long as a Player Character is given as input itā€™ll work similarly anywhere and allows simultaneous processing of multiple Player Characters.

Other considerations and optimizations

That was a rather technical explanation, and I hope some of you find it interesting!

All of this comes with some considerations and caveats, as seasoned programmers might already know.

  • For many game events, the order of processed events is crucial (avoiding race conditions). To address this, Iā€™ve implemented two event queues: ordered and orderless. The ordered event queue maintains the sequence and links events to other events that theyā€™re dependent on. If an error occurs, all linked events are cancelled, and the system returns to the original state with an error.
  • In WalkScape, events are completed asynchronously (on a separate thread or server), so the UI must account for this. If the response with an updated state takes a significant amount of time, the UI needs to reflect this. Some events, like loading steps, must also be able to block UI interactions during processing.

For players, this overhaul not only makes the game load and run faster but should also fix the remaining issues with step loading. Currently, if you interrupt step loading by closing the game, putting it in the background, or losing internet connection, the steps are often lost entirely. With the new system, if an interruption occurs during processing, the game state wonā€™t change or save, ensuring the steps will load correctly the next time you open the game.

Iā€™ve also implemented some basic but effective optimizations. When I started working on WalkScape, I used many ordered lists as data structures. This was convenient and wasnā€™t problematic when the game had less data to process. However, as the gameā€™s complexity increased, these lists began to contain hundreds of objects, causing performance issues when searching for items (which takes linear time, O(n)).

A simple fix for this is using key-value maps for data structures where weā€™re not searching, but only getting or setting values. This reduces operations to constant time, O(1). For data structures that require searching, using ones that allow for logarithmic time operationsā€”such as binary search trees, for which Dartā€™s closest equivalent is SplayTreeSetā€”makes a huge difference in performance.

Until next time

Phew, that was a lot of technical stuff! I hope you found it interesting and useful. As always, Iā€™m happy to answer any questions or address comments you might have. And even if the tech details arenā€™t your cup of tea, I hope the teasers made it worthwhile!

Happy walking, everyone, and donā€™t forget to stay hydrated! ā¤ļøļø

  • butter
    link
    fedilink
    English
    arrow-up
    2
    Ā·
    14 days ago

    Exciting! I wish more devs spent time optimizing their games.