DEV Community

Art
Art

Posted on

The First Game Built with Forge4D


A few years ago I wrote a small Android game called CancerHealing. It started from a game I liked, Tower Wars, with one change: I didn't want a game about war. So instead of towers shooting at each other, you heal. Cells send energy along lines to each other, and a cell of your colour that receives enough energy turns the ones next to it. You win when no enemy cells are left.

It was written with Godot 3 and GDScript. This week it became the first game built with Forge4D.

What "built with Forge4D" means here

Forge4D is the platform I build for native apps. Two languages sit at its centre:

  • SML describes what is on screen: a tree of elements with properties.
  • SMS is the scripting language. You run it through an interpreter while you develop, and you compile it ahead of time (AOT) via C++ for release. Same source, same behaviour.

For games I added a Game { } element. Inside it you can declare Godot nodes (a Sprite2D, a Label, a CollisionShape2D) as SML. Godot does what it is good at: rendering, physics, input, audio. SMS scripts the rules.

So the split in CancerHealing looks like this:

File What it is Size
world.sml the window: buttons, labels, the Game node 81 lines
cell.sml + levels/Level1…10.sml the cells and walls, one file per level generated
cancer.sms the entire game logic about 880 lines
world.sms wires buttons, mouse and the frame loop to the logic 25 lines

No GDScript. Godot is the host and doesn't know anything about the game.

How the port went

The game had ten levels with hand-placed cells and walls in Godot scenes. Retyping them was out of the question, so I wrote a small converter (tools/convert_tscn.py). It reads the Godot scene files and writes SML. Cell instances are expanded in place, and the wall tilemap turns into plain StaticBody2D nodes.

The logic I translated by hand from GDScript. It is the interesting part: lines between cells, gates that open and close depending on a cell's count, energy dots that travel along the lines, collisions between opposing energy, and an enemy that activates after a delay and picks its own targets.

To check the translation I wrote a headless test. A script plays level 2 with scripted mouse moves in fixed time steps and expects a win. It runs in both modes, AOT and interpreter, and both pass the same ten checks.

Things that went wrong

I'd rather list these than pretend it went smoothly.

  • The numbers were unreadable. Godot 4 draws label outlines much thinner than Godot 3, so the white counts on the cells disappeared. Fixing the outline size and font made them readable again.
  • The tutorial hands were missing. Levels 1 and 5 have a hand that shows the first move. In the original that lives in small scripts attached to the level scenes. The converter doesn't carry scripts over, so the hand was simply gone until I wrote it into the game logic.
  • A warning at the end of the game. After the last real level the game loaded a congratulation screen and the engine logged Unterminated string literal. The cause was my converter: it cut a label text at the first line break, so the SML string had no closing quote. The error message was no help, because it carried no text. I changed the engine so that parse errors in game.spawn and loadScene now include the text that failed, which would have saved me an hour.
  • The grey dot that wasn't a bug. I spent a while hunting a grey dot under the counts of enemy cells. It is a closed gate, exactly as in the original. When a cell has used up its channels, the white open gate disappears and the grey one shows. Sometimes the right fix is to stop fixing.

What it shows about the engine

A game with ten levels, a moving enemy and a few thousand sprites over a play session is a small test. It still shows three things:

  1. The split works. The scene is data, the rules are a script, and the host knows neither. I could change levels without touching the logic and the logic without touching the levels.
  2. Interpreter and AOT agree. I develop against the interpreter and release compiled. The same test passes in both.
  3. The bridge is where the cost is. Every property access between SMS and Godot crosses a bridge. Over the last days I made that path much faster, but access is still a few times slower than GDScript's direct calls. The game logic itself, with lists, maps and distance maths, is faster compiled than the GDScript equivalent. For a game like this one neither matters. For a game with thousands of entities moved from the script every frame, it will.

I'll write the numbers up separately, including the ones where we lose.

Try it

The game is part of the Forge4D repository under samples/cancerhealing. The Godot 4.6 port of the original, which I used as the base, lives at CrowdWare/CancerHealing on Codeberg. Forge4D itself is open source.

Next I want to see how far the engine goes: a stress test with many energies in parallel, and a second game that is built for Forge4D from the start instead of ported.

If you make games and have opinions on what an SML + script setup should be able to do, tell me. That is the part I'm least sure of.

Top comments (0)