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.
Create it
Section titled “Create it”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, default360, andseconds, a float, default0.5 - Branches: one, called
done
Build the machine inside
Section titled “Build the machine inside”Rename the state to Spinning, then click its Every Frame cell and add two steps:
- Rotate, with Speed bound to the macro’s own
speedinput, which sits at the top of the bind list just like a function’s arguments. - Wait, with Seconds bound to
seconds.
Take the branch as the way out
Section titled “Take the branch as the way out”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 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.
Clean up on End
Section titled “Clean up on End”One step on Spinning’s End cell: Set Rotation with Angle 0.
Use it twice
Section titled “Use it twice”Back on the script, open State settings on the Playing frame and pick Use macro, then
Make Rotation. Do it twice:
| Use | speed | seconds | done goes to |
|---|---|---|---|
Quick Spin | 720 | 0.35 | Idle |
Slow Spin | 120 | 1.2 | Idle |
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.
Send the landings to them
Section titled “Send the landings to them”- In
Walk, the Yes cell of theIs Neargoes toQuick Spininstead ofIdle - In
Aim, the far cell of thePick Targetcall goes toSlow Spininstead ofIdle
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.