Skip to content

AI guide

How to make and change Ludrel games with the AI tools (MCP server apps/mcp, or the built-in assistant). The tools work on game folders under games/; every edit is validated, and games run headless so a change can be proved before anyone plays it.

  1. Look. list_games, then get_project_tree and read_file for game/game.json, the scene you will change, and the prefabs involved. For levels, read game/levels/legend.json (the map characters).
  2. Change data first. Prefer the specific tools: update_prefab (merge component values), upsert_prefab, upsert_scene, add_entity, build_level (new levels), read_level + edit_level (change existing levels). Use write_file for anything else, including code in src/. An invalid document is refused with exact JSON pointers, and nothing is written; fix and retry.
  3. Validate. validate (add typecheck: true after editing code). Fix every error.
  4. Prove it. run_headless with scripted inputs and expect assertions, e.g. “after 2 s holding right, player.Transform.x > 100” or “event pickup:gem happened”. Results include errors, event counts, named entities (position, velocity, health) and scene state.
  5. Look again (optional, slower). capture_screenshot shows a scene after scripted input.

Start a new game with create_game_from_template (template platformer), which creates games/<name>.

  • game/game.json: display size, input actions (moveX, jump, down, pause, uiUp…), UI theme, assets (images, sprite sheets with animations, bitmap fonts, synth audio), prefab list, scene list, entry scene.
  • game/scenes/<id>.json: level, camera, background, music, systems (code behaviors), hud, menu, rules.
  • game/levels/<id>.json: tilemap. Build new ones with build_level from text rows instead of writing tile arrays. To change one, read_level (layers drawn as text, objects with tile positions, gids per tileset) and then edit_level with operations: ground (auto-tiled terrain by cells or rect), paint/fill/floodFill (gids), place/move/update/remove objects, resize, layer operations. They apply all or nothing and the result is validated. Creators make the same edits in the editor’s visual level view.
  • game/prefabs/<id>.json: entity templates; components by name (see the component reference), tags, optional extends (another prefab whose components are merged under these).
  • game/audio/*.json: synthesized sound effects (kind: "sound") and songs (kind: "song").
  • src/behaviors.ts: code. Every exported function is a behavior that scenes can list in systems.

Built-in components (always active): PlatformerController (run, jump, maxJumps, enabled), Health, ContactDamage (stompable), Patrol (walkers turn at walls and ledges), Hover (flyers: rangeX, rangeY, period), Pickup (emits pickup:<kind>, can heal), Checkpoint, Lifetime, PlatformerAnimator, Trigger, plus physics (Body, Collider) and visuals (Sprite, SpriteAnimation, Rect, Text, ParticleEmitter).

Rules connect events to actions in a scene:

  • Events: scene:start, controller:jump, pickup:<kind>, enemy:stomped, enemy:defeated, <name>:hurt, <name>:died, <name>:respawned, checkpoint:reached, trigger:<event> (from Trigger components or level trigger objects), input:<action> (an input action was pressed), app:hidden (page lost focus), and anything a behavior emits.
  • Actions: goToScene (fades), pushScene, popScene, restartScene, addToCounter (amount or from a binding; scope scene or session), setCounter, playSound, playMusic, stopMusic, cameraShake, emit, respawn, setComponent (change fields of a named entity), banner, toggleSetting, saveGame. Any action takes delayMs.

Bindings read values for HUD labels, menu labels, banners and assertions: game.<key> (scene state, e.g. game.coins, game.coinTotal), session.<key> (kept across scenes), settings.sound, and <entity>.<Component>.<field> for named entities (e.g. player.Health.hp). Labels use {binding}; HUD labels also take {value}.

  • Names: ids are lowercase with dashes (level-2); components are PascalCase; input actions camelCase.
  • The player must be named player (level legend P, or id on the spawn) for cameras, HUD and rules.
  • Spawn positions are the top-left of the entity’s sprite; colliders are offset inside it.
  • A new scene must be listed in game.json (upsert_scene does that) and reachable (goToScene from another scene’s rule or menu).
  • Simulation is deterministic: never use Math.random in behaviors; use the GameRng resource.
  • Use the documented binding paths. If run_headless reports a binding as undefined, the entity is not named or the scene state key was never set.
  • Keep changes small and validate after each one; run_headless costs a fraction of a second.