Skip to content

Variables and binds

A click tells you where the mouse was at that instant. To walk there over the next second you have to keep it somewhere, and that somewhere is a variable.

Open the Aim frame, click its Start cell and add Set Value. Click the card’s name to open its panel: every field there has a small +, Create and bind a new variable. Use it on Target, name it target, type Vector2.

Creating target as a Vector2 from the field that needs it.

One click creates the variable, binds the field to it and lists it in the sidebar. On a field that receives a value the button is shorter still, Create a variable named after this output, and it asks nothing. The other way round, New Variable on the Props tab, is there for when you are planning ahead, but the moment you need a variable is usually the moment you are looking at the field that wants it.

Right-click a variable’s row in Props for its type, its default and Is Export, which puts it in Godot’s own inspector: see Properties.

A chip on a card is just the literal case of something with five settings. In the card’s panel, each field has a row of small buttons that switch between them:

SourceHowWhen
LiteralType it into the chipA fixed number, a colour, a piece of text
BindThe circle buttonRead a variable, a property of the node, or a built-in value
ExpressionThe calculator buttonA one-off bit of maths the actions do not cover
Inline actionThe action button, or the input pin on the cardFeed the field with another action’s result
WireDrag from an output dot to an input pinReuse a value a step above already produced

A field holds one source at a time: picking a new one drops the old.

The inline action row has a rule of its own: only pure producers are offered, actions that just compute a value, with no branches and no body. That is why Random Float can go inside a field and Wait cannot, and a Wait you need the result of goes in the chain, storing into a variable the field then binds to.

The source buttons on a field, from literal to inline action.

Still on the Set Value, set Value with the bind button, then Mouse Position.

Binding Value to the built-in Mouse Position.

The bind list is longer than “your variables”. At the top are built-in values, Self, Node path…, Mouse Position, Screen Size, Delta, Random Float, Key pressed… and more, which cost no action at all to read. Under them come your variables, filtered to the types that fit the field, and then the properties of the node itself, whatever the class you extended exposes: position and modulate on a Sprite2D, plus velocity on a CharacterBody2D.

Some fields are where a value lands rather than where it comes from, like the Target you just filled: those take a bind and nothing else, because randf() = 5 is not a thing you can write. They read not set until you give them one, so an unfinished step is visible instead of silently doing nothing.

Click the Aim entry’s Start cell again, or the Add action node after the step, and add Transition, pointing its To branch at Walk and naming it going there.

Transition sending Aim to Walk, named going there.

On Walk’s Every Frame cell, two steps:

  1. Lerp Toward, with Target bound to the node’s position, To bound to the target variable, and Weight 0.1.
  2. Is Near, with A bound to position, B bound to target and Distance 8. Point its Yes cell at Idle and name the transition arrived.
Lerp Toward and Is Near on Walk, with Yes going back to Idle.

Compile and play. The sprite slides to wherever you click, and you can click again mid-slide to change its mind, because the click check is on the parent and keeps running under all of it.

The sprite sliding to each click, redirected mid-slide.

That is a playable thing, built out of six ideas. The next two pages are about not repeating yourself.