NAL · Native Agent Language

The NAL language

NAL is the language Resonance games are written in. It's deliberately small. You can read a system and know what it does, and an AI agent can produce it without much guesswork. It's also strict, because the bugs we've found hardest to catch are the ones where nothing happens and nothing tells you why.

The basic pieces

A NAL world is built from four kinds of thing. Components are plain data. Systems run every tick, in priority order, for each entity that has the components in their query. Templates describe things you can spawn, and entities are what exists when the game starts.

There's no inheritance and nothing runs behind your back. To find out what can change an entity, look at the systems whose queries include its components.

Every sample on this page passes nal-validate, and we've run each one in the native player.

coin_run.nalNAL
world CoinRun {
    config {
        title: "Coin Run";
        width: 800;
        height: 600;
        background_color: "#14111b";
    }

    component Position { x: float = 0.0; y: float = 0.0; }
    component Velocity { dx: float = 0.0; dy: float = 0.0; }
    component Player { speed: float = 240.0; coins: int = 0; }
    component Coin { value: int = 1; }

    template CoinT {
        Position { x: 0.0, y: 0.0 }
        Coin { value: 1 }
        Sprite { width: 12.0, height: 12.0, color: "#f4a43a" }
    }

    system PlayerInput {
        query: [Player, Velocity];
        priority: 0;
        transform {
            Velocity.dx = 0.0;
            if (Input.left) { Velocity.dx = -Player.speed; }
            if (Input.right) { Velocity.dx = Player.speed; }
        }
    }

    system Movement {
        query: [Position, Velocity];
        priority: 10;
        transform {
            Position.x += Velocity.dx * DeltaTime;
            Position.y += Velocity.dy * DeltaTime;
        }
    }

    system Scatter {
        query: [Player];
        on_start {
            for i in 0..8 {
                Entity.spawn_at("CoinT", 80.0 + i * 90.0, 300.0);
            }
        }
    }

    entity Hero [tags = ["player"]] {
        Position { x: 400.0, y: 300.0 }
        Velocity { dx: 0.0, dy: 0.0 }
        Player { speed: 240.0 }
        Sprite { width: 24.0, height: 24.0, color: "#a78bfa" }
    }
}

Behaviour, events and UI

A state machine attaches to every entity with the listed components. Each state can have enter, transform and transition blocks.

Events have typed fields. When you emit one, the matching on handlers run on the next frame, so you always know when they'll fire.

Functions take typed parameters. Calling one with the wrong number of arguments is an error, and so is recursing deeper than 64 calls. Arrays hold up to 256 items and get copied when you assign them. A for loop walks a snapshot, so changing the array inside the loop is safe.

A ui block describes an interface. Its expressions are re-evaluated every tick. Button actions run on the following tick, which keeps the UI from changing the world halfway through drawing it.

In this example the guard switches to Chase once its alert level reaches 1. Entering that state emits Spotted, the handler adds a line to a string array, and the HUD lists it.

features.nalNAL
world Features {
    config { width: 800; height: 600; }

    component Guard { alert: float = 0.0; seen: int = 0; }
    component Score { points: int = 0; log: array[string] = []; }

    event Spotted { by: int; }

    fn clamp01(x: float) -> float {
        return clamp(x, 0.0, 1.0);
    }

    state_machine GuardBrain for [Guard] {
        default: Patrol;
        state Patrol {
            transform { Guard.alert = clamp01(Guard.alert - DeltaTime * 0.2); }
            transition {
                if (Guard.alert >= 1.0) -> Chase;
            }
        }
        state Chase {
            enter { emit Spotted { by: Entity.id() }; }
            transition {
                if (Guard.alert <= 0.0) -> Patrol;
            }
        }
    }

    system Tally {
        query: [Score];
        on Spotted {
            Score.points += 1;
            Score.log.push("spotted by " + to_string(Spotted.by));
        }
        transform { }
    }

    ui Hud [context = Score, layer = 10] {
        window "Status" [anchor = "top-left", x = 12.0, y = 12.0] {
            heading("Points " + to_string(Score.points));
            for line in Score.log {
                label(line) [size = 11];
            }
            button("Reset") { Score.points = 0; }
        }
    }

    entity G { Guard { alert: 0.0 } }
    entity S { Score { points: 0 } }
}

What else is in there

Types

int, float, bool, string, entity_ref, vec2, vec3, enums, array[T], map[string, T] and record types, which nest. Constants can come straight from a JSON file. Integer maths stays integer, and overflow is an error. Division always gives a float, so for whole-number division write floor(a / b).

The world

Use World.find, World.find_nearest and World.all to look things up. World.in_radius lists what's nearby. You can read and write another entity's fields directly, as in target.Health.hp -= 30.0. Nav.path and flow fields find routes around obstacles. Entity.spawn_at and spawn_at_3d hand back a reference straight away, so you can set up the new entity in the same frame.

Time and flow

There's DeltaTime, plus Timer.after, every and cancel, and Game.set_paused. Game.set_time_scale speeds up or slows down timers and physics too. Scene.load switches scenes at the end of the frame. Systems can run on an interval:, and there's while, break, continue and match.

Input

Arrow keys and action buttons, named actions defined in an input_map, mouse buttons (including "pressed this frame"), mouse position and movement, and the scroll wheel.

3D

Transform3D, MeshRenderer (a primitive or a .glb/.gltf path, with tint, highlight and alpha) and Camera3D, plus Camera.screen_to_ground, Camera.world_to_screen and World.pick.

Projects

Split a game into modules with import and pub, then list the scenes and the starting scene in a manifest. The player, validator, MCP server, packager and IDE all load a project the same way.

Our one strict rule

If it parses, it has to do something

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

That sentence is in the language spec because of some painful bugs. In one, a system written below the closing brace of the world block validated fine and never ran. In another, a spawn with a misspelled template name produced nothing and gave no error. Worst of all, Entity.id() returned 0 for every entity. All three are now errors. The devlog has the full list. The validator now also checks names across the whole project, so a misspelled field, state or template fails validation instead of failing quietly at runtime. How we got there.

This matters even more when an agent is writing the code. An agent can fix a problem the validator reports, but it has nothing to go on when the only symptom is that nothing happened.

terminalshell
$ cargo run --bin nal-validate -- coin_run.nal
ok: coin_run.nal — valid (4 components, 3 systems, 1 entities)

$ cargo run --bin nal-validate -- broken.nal
error[parse]: broken.nal:Unexpected token at 42:5:
  expected Semicolon, found RBrace
FAIL: broken.nal — 1 error(s), 0 warning(s)

Projects and scenes

Real games end up in several files. Ember Colony's main scene imports five modules, and its manifest lists four scenes, starting from the menu. Calling Scene.load("src/win.nal") switches scene at the end of the frame.

Interpreter and VM

Games run on an interpreter today. We're also building a bytecode VM, which shares the interpreter's number handling and its interface to the engine. A test harness compares the full game state from both, frame by frame, across whole games. In November we measured it on our biggest game and it was slower than the interpreter, so we froze it as a test backend. How the comparison works, and why we froze it.

Not in the language yet

String interpolation (for now, join strings with + and to_string or to_fixed), and vec4/quat values.

ember_colony/src/game.nalNAL
import "./components.nal";
import "./colonists.nal";
import "./buildings.nal";
import "./threats.nal";
import "./hud.nal";

world EmberColony {
    config {
        title: "Ember Colony";
        width: 1280;
        height: 720;
        fixed_timestep: 0.016667;
        input_map {
            "pan_up":   ["KeyW", "ArrowUp"];
            "pan_down": ["KeyS", "ArrowDown"];
            // …
        }
    }
    // …
}
ember_colony/project.resonance.jsonJSON
{
  "name": "Ember Colony",
  "entry": "src/game.nal",
  "entry_scene": "src/menu.nal",
  "scenes": ["src/menu.nal", "src/game.nal",
             "src/win.nal", "src/lose.nal"],
  "nal_version": "v2"
}