Functions
A function is a list of steps with a name. You call it from any state, it takes values in, hands values back, and tells you which way it ended.
Reach for one when the same handful of steps shows up in two places, or when a decision is worth a name of its own. It compiles to a real GDScript method, so calling it three times writes the body once.
Create it
Section titled “Create it”Under Functions in the Props tab, click New Function. The canvas switches to the function’s own body, and the route bar at the bottom says where you are: click the script’s name there to get back out.
Look at the entry node. It has one cell, Run, not the four you know. That is the first thing about a function: it has no lifecycle. Its steps run top to bottom whenever somebody calls it, and the phase of the action that called it decides when that happens.
Declare its shape
Section titled “Declare its shape”Right-click the function’s row for its inspector, which holds a Name, and three lists, Inputs, Outputs and Branches, each with an Add … button. Set it up:
- Name:
Pick Target - Inputs:
max distance, a float, default300 - Outputs:
point, a Vector2 - Branches: two,
nearandfar
Write the body
Section titled “Write the body”Click the Run cell and add Is Near: A bound to Mouse Position, B
bound to the node’s position, and for Distance, open the bind list and pick max distance,
sitting right at the top.
That is the trick worth keeping: inside a function, its own arguments come first in the bind list. An input reads exactly like a variable, so every field of every action can be fed by an argument with no extra ceremony.
End it on a Finish
Section titled “End it on a Finish”On each of the Is Near cells, add the Finish action, which only appears in the search inside a
function. It has one field per output plus a Result picker listing the branches:
- On Yes:
point= bind Mouse Position, Result =near - On No: the same, Result =
far
Call it
Section titled “Call it”Go back to Aim and delete the Set Value and the Transition. Click the Start cell and add
Pick Target, which is in the search like any other action, with max distance left at 300.
The card carries two cells of its own, the branches you declared, and a point output dot on
its right. Each branch needs two steps: keep the value, then leave.
On near, use the list + twice:
- Set Value writing into
target, with a wire dragged from the call’spointdot into its Value. - Transition, with its To branch pointing at
Walk, namedwalking there.
On far, the same two steps: a Set Position with point wired into its
Position, then a Transition pointing at Idle, named jumping there.
Compile and play: click near and it slides, click far and it jumps.