Skip to content

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.

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.

A new function, its own route, and the route bar 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.

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, default 300
  • Outputs: point, a Vector2
  • Branches: two, near and far
Declaring max distance, point, and the near and far branches.

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.

Is Near on the Run cell, with max distance at the top of the bind list.

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.

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
A Finish on each cell, choosing near and far in the Result picker.

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 Pick Target call on Aim, with its branch cells and point output.

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:

  1. Set Value writing into target, with a wire dragged from the call’s point dot into its Value.
  2. Transition, with its To branch pointing at Walk, named walking there.
The near branch: Set Value fed by the point wire, then Transition to Walk.

On far, the same two steps: a Set Position with point wired into its Position, then a Transition pointing at Idle, named jumping there.

The far branch: Set Position fed by the same wire, then Transition to Idle.

Compile and play: click near and it slides, click far and it jumps.

Clicking near slides, clicking far jumps.