idemandsky/canopy
A hierarchical state machine for Roblox (luau): nested states, timed states, and events that pass outwards from the innermost state.
Canopy
A hierarchical state machine for Roblox. Nested states, timed states, and events that pass outwards from the innermost state.
Documentation · Getting started · Why Canopy
Warning
Canopy is in early development. Its API may still change.
Features
- Nested states. A state can hold child states. Its parent, the superstate, handles what its children leave to it, so shared behaviour is written once.
- Events from the inside out. An event goes to the innermost active state first. A state that does not handle it passes it to its parent.
- Typed from end to end. Name the context, events and states once. The analyzer then reports a misspelt state, an unknown event, or a wrong field in the context or a state's memory.
- Timed states and timelines. Give a state a duration, fixed or a random range, and the machine moves on when it runs out. A timeline runs functions at set seconds into a state, such as the frames of an attack.
- Memory for each state. A state can keep data of its own, made fresh on each entry and dropped when it leaves.
- Cooldowns, entry conditions and tags. Keep a state from being entered again too soon or when a check fails, and
ask about tags such as
canAttackinstead of listing states. - Input buffering. An event sent with a buffer is kept and tried again until a state takes it or the buffer runs out.
- History. Record the states you choose with the data they were given, and check the sequence, such as the skills of a combo before an ultimate.
- Parallel regions. Run several trees side by side on one context, such as movement and combat.
- One tree, many machines. Build a tree once and share it. Each owner gets its own machine, which reads no clock
and is driven by the time you pass it. A machine that is waiting costs about 0.05 µs per
process.
Install
Canopy is one package, on Ember, pesde and Wally.
embr add idemandsky/canopy
pesde add idemandsky/canopy
# wally.toml
[dependencies]
Canopy = "idemandsky/[email protected]"
Example
A character that can be knocked back. knockedBack handles being knocked back again for both of its children, so
neither flying nor groundImpact needs its own handler.
const Canopy = require("@Canopy")
export type Context = { health: number }
export type Event = "knockedBack" | "landed"
export type Name = "free" | "knockedBack" | "flying" | "groundImpact"
const S = Canopy.setup<<Context, Event, Name, nil, nil>>({
events = {
"knockedBack",
"landed",
},
})
const tree = S.tree({
initial = "free",
states = {
free = S.state({
on = {
knockedBack = "knockedBack",
},
}),
knockedBack = S.state({
initial = "flying",
on = {
knockedBack = "knockedBack",
},
states = {
flying = S.state({
on = {
landed = "groundImpact",
},
}),
groundImpact = S.state({
duration = 0.8,
onTimeUp = "free",
}),
},
}),
},
})
const machine = S.new(tree, {
health = 100,
}, {
at = os.clock(),
})
machine:send("knockedBack") -- free > knockedBack > flying
machine:send("landed") -- knockedBack > groundImpact
game:GetService("RunService").Heartbeat:Connect(function()
machine:process(os.clock()) -- back to free once 0.8 seconds have passed
end)
Note
The new type solver does not report a misspelt event key inside on = { ... }. Pass the list of events to setup,
as above, and S.state errors on any on key that is not in it. See
Typing.