Skip to content

Functions

For creatorsAdd-on 2.2.2+

In this tutorial you rebuild the dungeon’s rules with two tools every node system ends up needing: functions, groups of nodes saved once and used anywhere, and the Expression node, a formula written as text. The game plays exactly as before, from a smaller graph. This builds on tutorial 5, The dungeon.

▶ Play the dungeon, built with functions: the same game and controls as tutorial 5.

A graph can own functions. Each one is a small graph of its own with a signature: named, typed inputs and outputs. Exec pins are allowed, so a function can be a calculation or an action.

Node What it is
Call Function Uses a function. Its pins are the function’s inputs and outputs
Function Input Inside the function: what the call was given, as outputs
Function Output Inside the function: what it hands back, as inputs. An exec here is where the call carries on

Three rules come with functions:

  • A function reads and sets its graph’s variables.
  • A Random Range inside a function rolls its own numbers at each place it is called, so two calls never roll the same. It is still the same every run with the same seed.
  • A function can call another function, but never itself.

When the game runs, every call is replaced by a copy of the function’s nodes, so a function costs nothing extra to run.

  1. Open the graph that will own the function (here, dungeon.rules). In the Node Editor’s sidebar, Simulacrum tab, Graph panel, press + beside Functions and type a Function ID, for example walkable.

  2. The new function opens as its own graph, with a Function Input and a Function Output already in it. While it is open, the Graph panel’s This function box shows its Function ID, a Label and the signature as text.

  3. Edit the signature. walkable takes one number and gives one back:

    {"inputs": [{"id": "v", "type": "number"}], "outputs": [{"id": "yes", "type": "number"}]}

    An action also lists {"id": "exec", "type": "exec"} in both lists.

  4. Press Apply Signature. The Function Input and Function Output nodes rebuild their pins, and so does every Call Function that uses the function. Wires on pins that still exist are kept.

  5. Build the body: an Expression with the formula a == 1 || a == 3 || a == 5 (floor, corridor or stairs), its a fed from Function Input’s v, its result into Function Output’s yes.

  6. Back in dungeon.rules, add Call Function (Add ▸ Simulacrum ▸ Flow) and type walkable in its Function field. Its pins appear; wire a cell’s value in and use yes.

One node for a formula over its inputs a, b, c and d.

You can write Examples
Arithmetic + - * / %
Comparisons (1 or 0) ==, !=, <, >, <=, >=
Logic &&, ||, !, and cond ? x : y
Functions abs min max floor ceil round sqrt sign clamp mod

abs(a - c) + abs(b - d) == 1 asks “is the monster next to me?” in one node, where it took seven Math nodes before. A formula that does not parse reads 0 and reports an error in the log.

The finished dungeon uses five functions:

Function What it does
centre The centre cell of a room
randomCell A random cell inside a room
walkable Whether a cell value is floor, corridor or stairs
whenPlaying Carries on only for a key press while the game is playing
act Your turn in one direction: fight, walk or bump a wall

Expressions replace every chain of Math, Compare and Integer nodes. The rules went from 319 nodes on one canvas to 232, plus 37 inside the five functions, each written once.

  1. Find a group of nodes you repeat, such as the four direction inputs that each fight, walk or bump.

  2. Make it a function with exec in and out (act), with the direction as two number inputs.

  3. Replace each copy with a Call Function, and replace each Math chain with one Expression.

  4. Press Build Cartridge and play: the dungeon behaves exactly as before.

  • You edit a signature as JSON text; there is no drag-a-pin editor yet.
  • What remains large is one Get Variable per variable you read and a pair of nodes for every grid write. Tutorial 10 removes both.
  • A function is a small graph owned by a graph, with a signature, called with Call Function.
  • Expression writes a formula in one node.

Next: Alien invasion runs one graph on fifty-five spawned copies at once.