Traction Point swaps Zig for C3, full builds drop to about 3s

A developer writing on the Madrigal Games blog says that over the past few months they have been rewriting the game Traction Point in C3. All of the game's Zig code has now been replaced, and the modding SDK will follow when the next playtest version is released.

The motive was build speed. Hot-reloading game code while the game runs has become integral to the author's workflow, whether for tweaking a UI element, lengthening an animation's ease-in or adding a temporary print to track down a bug. Zig builds on Windows were very slow. Zig now has a self-hosted backend that compiles relatively quickly, at least in debug mode, but the author says it is not yet available on Windows. Zig's form of incremental compilation does not remove the long LLVM linking times. At a little over 100k lines of Zig, a full build of the debug configuration took around 24s, and rebuilding after a one-line change with a warm cache took over 15s. C3 compiles the full game without caching in about 3s, and the author has seen it go as low as 2.6s.

The author wanted a near drop-in replacement for Zig: the same memory model (no garbage collector, no RAII), compilation to WASM for mod support, fast runtime and fast compilation. Ease of pickup was a preference too. Though fond of Zig syntax, the author thinks a C-derived language makes sense for attracting modders. Odin, Rust, Jai or other new languages might in some cases have fit as well, but the choice was C3.

The two languages were similar enough for an almost mechanical translation. C3 supports defer like Zig, so much of the lifetime management and scoping was copied straight over. It has built-in temporary memory pools, operator overloading for things like math types, and annotations: tagging a struct with @Component makes the engine treat it as a game object component, and @Exposed fields appear in the level editor inspector.

Earlier experiments with compiling mods to WASM had already given the project basic C3 support. The author first expanded the C3 modding SDK far enough to build and run the flying scout example mod, then ported almost all engine APIs while the Zig version kept running. The game code is a dll loaded by a C++ engine, so the whole dll interface had to be replicated in C3 or the engine would reject it. After that, systems moved over one at a time, some of them live on stream. Game data had to stay untouched and work with both versions until the port proved itself. The biggest issues came from JSON parsing: how the Zig and C3 standard libraries handled default values for missing data.

The author then built SL3 (Scripting Language on C3), for code that needs to update faster than a multi-second compile allows, as in the node-based particle editor where each graph node contributes a piece of C3 code. SL3 is a strict subset of C3. It compiles either to bytecode for a register-based virtual machine, for fast iteration, or to a native dll through the official C3 compiler, for fast runtime logic, with the same interface to the host application. So far it is used only for particle effects. On the virtual machine, all effects made for the game to date run at 60fps with room to spare.

Key facts

  • All Zig code in Traction Point has been replaced with C3; the modding SDK follows with the next playtest version.
  • A full Zig debug build, at a little over 100k lines, took around 24s on Windows, and a one-line change took over 15s to rebuild with a warm cache.
  • C3 compiles the full game without caching in about 3s, with 2.6s the fastest the author has seen.
  • The port was an almost mechanical translation, done system by system; the biggest issues involved JSON parsing defaults for missing data.
  • SL3, the author's strict subset of C3, runs as virtual machine bytecode or as a native dll and currently powers the particle effects.

Why it matters

Compile time shapes how a developer can work. Here hot-reload had become central to building the game, and a wait of over 15s after a one-line change cut against it. C3 is not as widely known as other modern systems languages, so a game with a little over 100k lines of Zig moving to it is a concrete account of what such a switch involved.

The second part goes further than the port. SL3 aims to give very fast hot-reloading during development and native performance in the shipped game from the same source. The author compares the native step to blueprint nativization in Unreal Engine 4.

Who it affects

Traction Point modders are directly affected: the modding SDK is due to follow in C3 with the next playtest version, and the author sees a C-derived language as a good way to attract them. Developers who build Zig projects on Windows and rely on quick iteration will recognise the complaint. It is also relevant to anyone weighing C3 for a game or another systems project, especially one that wants no garbage collector, no RAII and a WASM target.

How to use it

The porting sequence can be borrowed. Start with the modding or plugin interface, port APIs while the old version keeps running, and test new functionality in a mod. Move the application code only after the hardest binding problems are solved, then go one system at a time. The author also set a hard rule that game data must work with both versions until the port was proven. Check how each standard library's JSON parser fills in missing values, since that caused the biggest trouble here.

The SL3 pattern is to run bytecode while iterating and compile to a native dll for runtime speed. Its compiler can bundle multiple bytecode modules into a single dll, which avoids loading hundreds of dlls. Functions tagged @HostCall can be called from the host application.

How solid is it

This is a first-person account from the person who did the port, published on the Madrigal Games blog, and the author says they wrote it themselves. The timings are the author's own measurements on the project, and the hardware used for them is not specified. The Zig figure refers to a full build of the debug configuration, while the C3 figure is described as compiling the full game without caching. No runtime performance comparison between the Zig and C3 versions of the game is given. For SL3, the author reports that on a fairly modest development machine the demo mission with all effects on the virtual machine is barely distinguishable from the native version, with no numeric speed comparison.

Risks and caveats

The author expects C3 builds will probably get a bit slower as more code is added before the game is finished. The smooth port depended on how close the two languages were for this project's purposes, and the author calls the codebase about as well suited to such a rewrite as could be imagined, so other projects may find it harder.

SL3 is at an early stage with a long to-do list, and the author says the virtual machine could be faster. Some advanced C3 features, including contracts and semantic macros, are not supported yet, and memory management and access are limited to sandbox SL3 code from the rest of the app. A general-purpose scripting role and GPU particle simulation through generated shader code are only future possibilities.

“I could do an almost mechanical translation of the code, which I think was the only way this port was ever going to happen.”

— Traction Point developer, Madrigal Games blog