← Devlog

Bugs that return zero

Crashes are the easy bugs. You get a stack trace and a line number. The ones that cost us the most time this year didn't crash at all. The code was accepted, nothing printed an error, and the game simply did a bit less than it should have, sometimes for weeks before anyone noticed.

That kind of bug is even worse when an AI agent is writing the NAL. An agent works by reading error messages and fixing whatever they point at. When there's no message, it has nothing to work from.

These are the ones we found, starting with the worst.

Entity.id() was always 0

NAL systems run once per entity, and Entity.id() is supposed to give you the entity the code is currently running for. Ember Colony leaned on it heavily, for example to read each colonist's own state-machine state with World.get_field(Entity.id(), "ColonistBrainState", "current").

The interpreter reaches the engine through a context trait, and that trait's default entity_id() returned 0. The native context never overrode it. So every call returned entity 0. Reading through it gave back the "missing" value, 0.0, and writing through it did nothing, because entity 0 didn't have that component.

The gap log puts it bluntly: "the F2a forced transitions never worked at all: colonists never slept, never ate." We hadn't noticed because no test checked whether colonists slept, and our 60-second test run finished before anyone had time to starve.

The fix was a single method. Afterwards the starvation test started failing, because colonists were eating now. To get a colonist to starve again, the test has to switch off food gathering.

A typo in a template name

We had Entity.spawn_at("Arrow", …), but the template was called ArrowT. The spawn gave back None and carried on. Nothing appeared and nothing complained, so the only clue was that nothing happened, and it took a while to track down. Spawning an unknown template now logs an error that names it.

Code below the world block

A system that ended up after the closing brace of world { } parsed and validated without complaint. nal-validate said "valid … 1 systems", counting only the system inside the block, and the stray one never ran. That's now a parse error pointing at the stray code. While we were at it, we checked every NAL file in the repository: 72 files, no errors.

A function that always said no

World.line_of_sight(x1, y1, x2, y2) existed and type-checked, and it always returned false. That's a perfectly believable answer, and you could easily ship an enemy that never sees the player because of it. Until there's a real implementation behind it, calling it now gives an "unsupported operation" error.

Errors caught and thrown away

For a while, if a system's transform hit an error, NativeWorld caught it and dropped it. The system would stop partway through, every frame, without a word. Now those errors are logged and counted, and the tests and the parity harness both check the count. A test can't pass just because both sides failed the same way.

A click that never registered

Input.mouse_left_pressed is supposed to be true for one frame when you press the button. Through GameSession it was never true. The "just pressed" check lived in one particular input function, and the session set the button state directly without going through it. In the meantime the NAL code tracked the previous button state and worked out the press itself. The session now works it out properly, and that workaround is gone.

Still open: disabled buttons

One of these is still open. A ui button with [enabled = false] greys out, but a click sent in through the session still runs its action. Ember Colony's research screen gets around it by checking the condition again inside the action. The real fix, ignoring clicks on disabled buttons, is on the list.

What they have in common

Look at what these bugs returned: 0, 0.0, false, None. All of them are normal values. They just happen to also mean "nothing happened", and code further along has no way of telling the difference. In our experience, defaults that look like real data are the most dangerous thing a runtime can have.

We haven't got rid of all of them, and some we've kept on purpose. World.get_field still returns 0.0 for an entity or field that doesn't exist, because games depend on that. "No entity found" is still u32::MAX. Those are documented now, so at least they're deliberate.

All of this led to one sentence in the NAL spec, which every feature has to satisfy before the public alpha:

A declaration or expression must not parse successfully and then silently do nothing.

That leaves each feature three options. It can run correctly on the interpreter, or run the same on both the interpreter and the VM under the parity tests, or be rejected by the validator with a clear error. "It parses, but we haven't got to it yet" is no longer allowed.

A few habits have helped. We write tests that check behaviour: "a tired colonist is in Sleeping after nightfall" would have caught the Entity.id() bug, where "runs 600 frames without an error" didn't. We log every gap the same way, and the "what actually happened" part of that write-up is where a lot of these turn up. And we've learned to distrust any trait default that returns a value when it could return an error.