Skip to content

Macros

The sprite should spin for a moment when it lands. That sounds like another function, and it is not.

A function is a list of steps: it runs and it is over. A macro is a piece of state machine: it has states of its own, with all four cells, and it keeps running across frames. A spin lasts a second, so it needs somewhere to live while that second passes, and only a state can do that.

The rule of thumb: if it finishes inside the call, it is a function. If it takes time, it is a macro.

Under Macros in the Props tab, click New Macro. Like a function it opens its own route, and it comes with one state already inside so there is somewhere to run.

Right-click its row for the inspector and set it up:

  • Name: Make Rotation
  • Inputs: speed, a float, default 360, and seconds, a float, default 0.5
  • Branches: one, called done
A new macro, with its two inputs and the done branch.

Rename the state to Spinning, then click its Every Frame cell and add two steps:

  1. Rotate, with Speed bound to the macro’s own speed input, which sits at the top of the bind list just like a function’s arguments.
  2. Wait, with Seconds bound to seconds.
Spinning: Rotate and Wait, both fed by the macro’s inputs.

Open the Wait card’s Finished cell and look at the target list: the branches of the macro are in it, alongside the states. Pick done.

The Wait’s Finished cell taking the done branch.

The macro does not know what comes after the spin, and it must not: that is what makes it reusable. It says “I am done”, and each place that uses it decides what that means.

One step on Spinning’s End cell: Set Rotation with Angle 0.

Set Rotation back to 0 on the End cell.

Back on the script, open State settings on the Playing frame and pick Use macro, then Make Rotation. Do it twice:

Usespeedsecondsdone goes to
Quick Spin7200.35Idle
Slow Spin1201.2Idle
Two uses of Make Rotation on Playing, each with its own numbers.

A use is a sub-state like any other, except its contents are the macro’s machine instead of steps you wrote. Its entry node reads top to bottom as exactly that deal: a pin per input, the four normal cells which belong to the use itself, and a cell per branch, where you pick the state this use goes to.

Its header also gets an extra button, Open the macro this state runs, which is the fast way back into the definition.

  • In Walk, the Yes cell of the Is Near goes to Quick Spin instead of Idle
  • In Aim, the far cell of the Pick Target call goes to Slow Spin instead of Idle
The two landings repointed, and the fast and slow spins running.

One machine, written once, running at two speeds because each use said so. As two hand-written states it would have been two copies to keep in sync, and you would have fixed the End reset in only one of them.