Skip to content

Trees ​

A tree is built once with S.tree and shared by every machine made from it. It is frozen, and its methods answer questions about its states without a machine.

The examples on this page use the tree from Getting started, where knockedBack holds flying and groundImpact.

Tree methods ​

hasState Added in 0.1.0 ​

luau
tree:hasState(name: string): boolean

Returns whether the tree has a state named name, at any depth. name is any string, so use it to check a name that comes from data.

getPath Added in 0.1.0 ​

luau
tree:getPath(name: Name): { Name }?

Returns the names from the outermost state down to name, or nil when the tree has no such state. tree:getPath("flying") is { "knockedBack", "flying" }. The table is frozen.

getParent Added in 0.1.0 ​

luau
tree:getParent(name: Name): Name?

Returns the state that holds name, or nil for an outermost state or a name the tree does not have.

isWithin Added in 0.1.0 ​

luau
tree:isWithin(name: Name, withinName: Name): boolean

Returns whether name is withinName or a state inside it. tree:isWithin("flying", "knockedBack") is true.

Checking objects ​

Each object Canopy builds carries a private identifier, compared by identity, so a hand-written table with the same fields is never taken for one. S.tree and S.new use these checks to refuse anything Canopy did not build.

isState Added in 0.1.0 ​

luau
Canopy.isState(value: unknown): boolean

Returns whether value was made by state or withMemory.

isTree Added in 0.1.0 ​

luau
Canopy.isTree(value: unknown): boolean

Returns whether value was made by tree.

isGoTo Added in 0.1.0 ​

luau
Canopy.isGoTo(value: unknown): boolean

Returns whether value was made by goTo.

isStay Added in 0.1.0 ​

luau
Canopy.isStay(value: unknown): boolean

Returns whether value was made by stay.

isMachine Added in 0.1.0 ​

luau
Canopy.isMachine(value: unknown): boolean

Returns whether value was made by new.

isHistory Added in 0.1.0 ​

luau
Canopy.isHistory(value: unknown): boolean

Returns whether value was made by history.

isValidDuration Added in 0.1.0 ​

luau
Canopy.isValidDuration(duration: Duration): boolean

Returns whether duration is seconds (0 or more), or { min, max } with 0 <= min <= max. Use it to check a duration that comes from data before building a state with it.