Logic & UI

Scripting best practices

How to write visual scripts that work with the engine instead of against it, with examples from Meadow Quest.

A visual script runs once per frame, inside a pipeline of engine systems. In a 2D scene the order is:

ScriptRun  →  MoveByVelocity2D  →  Physics2DStep  →  Sprite2DRendering
(your scripts)  (applies velocity)   (bodies, collisions)  (draws)

Your script decides what should happen; the systems after it do the moving, colliding and drawing. The practices below follow from that.

Move things by setting velocity

Set SetVelocity2D (or SetEntityVelocity2D) to a speed in units per second, and let the engine integrate it.

Set velocityWrite position every frame
frame-rate independentyes, the engine multiplies by the frame timeonly if you multiply by DeltaTime yourself
collides with wallsyes, the physics step stops a body at a wallno, it teleports through
other systems see the motionyes: physics and anything else that reads velocityno, they see a jump
stops cleanlyset velocity 0must stop writing

In Meadow Quest the hero, the dog and the slimes all move by velocity. A sprite needs a velocity to have one: Add Component > Velocity x,y, or Physics Body 2D, which brings a velocity and a collider together.

Writing the position is right for: a camera that follows exactly, HUD elements pinned to the camera, and one-off jumps (a coin moving away when collected, the hero respawning).

Let physics own physical things

  • Anything that must be blocked by walls or push other things gets a Physics Body 2D, and is moved only by velocity.
  • Things that never move and only block (walls) get a Collider 2D without a body: they're static and cost almost nothing.
  • Top-down games: Gravity scale 0 on every body, Fixed rotation on characters.

Keep state in one place

KindUse forMeadow Quest example
Global (GetGlobal / SetGlobal)game state shared by many scriptscoins, hp, hasSword, started
Variable (GetVar / SetVar)memory belonging to one entitythe sword's face and swingAt
Component valuesanything the engine also readsposition, velocity, tint

Scripts that display state (hearts, inventory icons, speech bubbles) read it every frame and place their sprites to match; they never keep their own copy. That way the picture can't drift out of step with the game.

Globals and variables reset when you press Stop. Use OnStart to set starting values that aren't 0 (like 3 hearts).

Make every frame's decision complete

A script runs every frame, so give each Branch an else that undoes what then did: show the bubble or park it, chase or stop. A branch without an else leaves the last frame's result behind.

For one-off events (a key press, a hit), use ActionPressed (true for one frame only), or store a time and compare with Time, as the sword swing and the hit cooldown do.

One script, many entities

Write a script for a kind of thing, not for one object: all coins share Coin, both slimes share Slime. GetPosition2D, SetVelocity2D and the other nodes without an entity input act on the entity running the script. Use FindEntity only for named, unique things (the hero, the camera).

Split by job, not by object

Health and Inventory are separate scripts even though both draw HUD: each has one job and a graph you can read. When a graph no longer fits on the screen after Frame, split it.

Read input as actions

Read ActionVector2 / ActionPressed (Move, Jump, Fire) rather than KeyDown where you can: actions cover keyboard and gamepad, and their bindings can be changed in Windows > Input Actions without editing scripts. KeyDown is fine for a debug key or a one-off like the potion's Q.

Order matters in the scene's system list

Keep ScriptRun before MoveByVelocity2D and Physics2DStep in the scene's update: list (the order Meadow Quest's scene uses), so a velocity set this frame is applied this frame. This matters if you edit a .tgscene by hand or with an AI agent.

Use events for things that happen once

OnTriggerEnter (a sensor collider was entered), OnCollisionEnter (a solid hit; tick Reports collisions on one of the Physics Body 2Ds), OnMessage and OnUiEvent run only when something happens. Prefer them to checking a condition every frame: a pickup on a trigger is exact and costs nothing between contacts. Compare ContactOther with the entity you expect, since anything with a body can enter a trigger.

Side effects such as PlayAudio, SetText and LoadScene belong on those events, or behind a flag that makes them run once (the Meadow Quest gate's open variable). Called every frame, a sound restarts every frame.

Things that aren't possible yet

Knowing the limits saves hours:

  • spawning or destroying entities (park them far away);
  • loading a different scene from a script while in the editor (it works in a published game; in the editor LoadScene only restarts the open scene).

See What's next for the full list.