Skip to content

Canopy ​

Canopy is a hierarchical state machine for Roblox, written in Luau. A state can hold child states, and a parent state is called a superstate. knockedBack can be the superstate of flying and groundImpact, so an event that flying does not handle, such as being knocked back again, is handled once by knockedBack for all of its children.

In development

Canopy is in early development. Its API may still change.

How it works ​

  1. Name your types. setup takes the types of the shared context, the events and the state names, so the analyzer can check everything built from it.
  2. Build a tree. Describe the states once: their children, durations, memory, and the events they handle. The tree is checked and frozen, so every machine can share it.
  3. Make a machine. Each owner, such as a character or an enemy, gets its own machine from the tree, with its own context.
  4. Send events and process. Send events as things happen, and call process with the current time to run timed states and each state's onProcess.

What you get ​

  • Nested states. A state's children inherit its event handlers, so behaviour shared by a group of states is written once, on their superstate.
  • Types in your editor. The analyzer reports a misspelt state, an unknown event, and a wrong field in the context or in a state's memory. See Typing.
  • Memory for each state. withMemory gives a state data of its own, made on each entry and dropped when it leaves, typed from the function that makes it.
  • Timed states. A state with a duration runs onTimeUp once it has lasted that long. A { min, max } duration is rolled on each entry.
  • History. Record the states you choose, with the data they were given, and read the sequence back to react to it, such as a combo that turns the next skill into its ultimate form.
  • Clear moves. A move leaves only the states the current and target paths don't share, and enters down to the target and its initial children, with a hook for each step. See How a machine moves.
  • Checked definitions. A tree raises an error for a name used twice, a parent with no initial child, or a handler that leads to a state the tree does not have. Each object Canopy builds carries a private identifier, so a hand-written table cannot pass as one.
  • No clock and no side effects. A machine reads no clock and writes nothing outside itself. You pass the time, so a test can step it to any moment.

Performance ​

Measured in Roblox Studio over 1000 machines, as the fastest round of each benchmark:

WhatCost
process on a machine with nothing due0.05 µs, however deep its states are
A machine in a group that is only waiting0.01 µs per group:process
A move by send or goTo0.6 to 1 µs
isIn, hasTag, getLeaf0.04 to 0.06 µs
One new machine with two flat statesabout 1.3 KB

A machine keeps the time its next duration ends, so a machine that is waiting costs one comparison per process. A group goes further and only runs the machines that have something to do. See Performance for every measurement and how to run them yourself.

Continue with the Getting started guide, or read Why Canopy for the reasons behind it.