Skip to content

Setup ​

setup returns the builders for one kind of machine. Give it the machine's types between << and >>, and every state, tree, goTo and machine built from it is checked against them.

The examples on this page start like this:

luau
const Canopy = require("@Canopy")

type Context = { model: Model, target: Model? }
type Event = "spotted" | "lost" | "hit"
type Name = "idle" | "fighting" | "chasing" | "attacking" | "stunned"

const S = Canopy.setup<<Context, Event, Name, Model, nil>>()

setup Added in 0.1.0 ​

luau
Canopy.setup<<Context, Event, Name, Data, Record>>(
	options: SetupOptions<Event>? -- Every event and tag, to check the keys of each `on` table and each state's tags
): Setup<Context, Event, Name, Data, Record>

Returns the builders below, typed with the five types. Give all five, with nil for Data or Record when you don't use them: a type left out is not checked.

  • options — a table with:
    • events: { Event } — every event the machine can be sent. The type checker checks this list against Event, but not the keys of an on table (see the known gap). With the list, state errors on an on key that is not in it, as soon as the state is built, and send warns once about an event that is not in it and that no state handled.
    • tags: { string }? — every tag a state may have. With the list, state errors on a tag that is not in it, and hasTag warns once about a tag that is not in it.
luau
const S = Canopy.setup<<Context, Event, Name, Data, nil>>({
	events = {
		"knockedBack",
		"landed",
		"moveStarted",
	},
	tags = {
		"canAttack",
		"canMove",
	},
})
CauseMessage
Erroroptions.events holds something that is not a stringCanopy.setup: 'events' must be a list of the events' names.
Erroroptions.tags holds something that is not a stringCanopy.setup: 'tags' must be a list of the tags' names.
ErrorA state's on has a key not in options.events (from state and withMemory)Canopy.state: '<event>' is not one of the events given to setup.
Warningsend was given an event not in options.events that no state handled (once per tree and event)Canopy: '<event>' is not one of the events given to setup.

Setup type Added in 0.1.0 ​

luau
type Setup<Context, Event, Name, Data, Record = nil> = {
	state: (definition: StateDefinition<Context, Event, Name, Data, nil, Record>) -> State<Context, Event, Name, Data, Record>,
	withMemory: <Memory>(factory: (context: Context, data: Data?) -> Memory) -> MemoryBuilder<Context, Event, Name, Data, Memory, Record>,
	tree: (definition: TreeDefinition<Context, Event, Name, Data, Record>) -> Tree<Context, Event, Name, Data, Record>,
	goTo: (target: Name, data: Data?) -> GoTo<Name, Data>,
	stay: () -> Stay,
	new: (tree: Tree<Context, Event, Name, Data, Record>, context: Context, options: MachineOptions<Context, Event, Name, Data, Record>?) -> Machine<Context, Event, Name, Data, Record>,
	restore: (tree: Tree<Context, Event, Name, Data, Record>, context: Context, snapshot: Snapshot<Name, Data>, options: MachineOptions<Context, Event, Name, Data, Record>?) -> Machine<Context, Event, Name, Data, Record>,
	group: () -> Group<Context, Event, Name, Data, Record>,
	parallel: (regions: { [string]: Tree<Context, Event, Name, Data, Record> }, context: Context, options: MachineOptions<Context, Event, Name, Data, Record>?) -> Parallel<Context, Event, Name, Data, Record>,
	history: (options: HistoryOptions<Name, Record>?) -> History<Name, Record>,
}
  • Context — the data shared by the whole machine, passed to every state function.
  • Event — the names of the events the machine can be sent, usually a union of strings.
  • Name — the names of the states, a union of strings. Each name is used once in the whole tree.
  • Data — what events and goTos carry, such as a skill's name or a target. nil when they carry nothing.
  • Record — what a recorded state adds to the history. nil when nothing is recorded.

history is listed on the History page, group on the Groups page, and parallel on the Parallel regions page.

state Added in 0.1.0 ​

luau
S.state(definition: StateDefinition<Context, Event, Name, Data, nil, Record>): State<Context, Event, Name, Data, Record>

Checks a state with no memory and freezes it. The fields of definition are listed on the States page.

  • definition: StateDefinition — the state's children, duration, handlers and functions.

Returns the checked State.

CauseMessage
Errordefinition is not a tableCanopy.state: a state is a table.
Errorstates, on, onStart, onProcess, onEnd or record has the wrong typeCanopy.state: '<field>' must be a <type>.
Errorinitial is not a stringCanopy.state: 'initial' must be a state's name.
Errorduration is negative, or a { min, max } with min > maxCanopy.state: 'duration' must be seconds (0 or more) or { min, max }.
ErroronTimeUp is not a name or a functionCanopy.state: 'onTimeUp' must be a state's name or a function.
Errordefinition sets identifier, memory or cuesCanopy.state: 'identifier', 'memory' and 'cues' are set by Canopy. Use withMemory for a state's memory.
ErrorAn entry of on is not a name mapped to a name or a functionCanopy.state: 'on' maps an event's name to a state's name or a function.
ErrorA child in states was not made with stateCanopy.state: 'states' maps names to states made with state.

withMemory Added in 0.1.0 ​

luau
S.withMemory<Memory>(
	factory: (context: Context, data: Data?) -> Memory -- Makes the state's memory on each entry
): MemoryBuilder<Context, Event, Name, Data, Memory, Record>

Gives the next state data of its own. factory runs each time the state is entered, before onStart, and what it returns becomes run.memory until the state is left. The memory type is inferred from what factory returns, so run.memory is typed inside that state's own functions.

  • factory — called with:

    • context: Context — the machine's context.
    • data: Data? — the data of the goTo or event that entered the state.

    It returns the memory, of any type.

Returns a MemoryBuilder, whose state method builds the state.

CauseMessage
Errorfactory is not a functionCanopy.withMemory: the memory factory must be a function.
luau
const attacking = S.withMemory(function(context, data)
	return {
		swings = 0,
	}
end):state({
	onProcess = function(context, run, now)
		run.memory.swings += 1

		return if run.memory.swings >= 3 then S.goTo("chasing") else nil
	end,
})

MemoryBuilder Added in 0.1.0 ​

luau
MemoryBuilder:state(definition: StateDefinition<Context, Event, Name, Data, Memory, Record>): State<Context, Event, Name, Data, Record>

Checks and freezes the state, as state does, with the memory factory attached. Every function of definition gets a run whose memory has the type the factory returns. It raises the same errors as state.

tree Added in 0.1.0 ​

luau
S.tree(definition: TreeDefinition<Context, Event, Name, Data, Record>): Tree<Context, Event, Name, Data, Record>

Checks the whole tree, indexes every state by name, and freezes it. Build it once and share it between every machine.

  • definition — a table with:
    • initial: Name — the outermost state a machine starts in.
    • states: { [Name]: State } — the outermost states, made with state or withMemory.

Returns the checked Tree.

CauseMessage
Errordefinition is not a table with a states tableCanopy.tree: a tree is { initial, states }.
ErrorAn outermost state was not made with stateCanopy.tree: 'states' maps names to states made with state.
ErrorA name is used twice anywhere in the treeCanopy.tree: the state '<name>' is used twice. A name must be used once in the whole tree.
Errorinitial is not one of the outermost statesCanopy.tree: 'initial' must be one of the outermost states.
ErrorA state with children has no initial, or one that is not a childCanopy.tree: the state '<name>' holds other states and needs an 'initial' that is one of them.
ErrorA state has an initial but no childrenCanopy.tree: the state '<name>' has an 'initial' but holds no states.
ErrorA handler in on or onTimeUp names a state the tree does not haveCanopy.tree: the state '<name>' goes to '<target>' on <event>, which is not in the tree.

goTo Added in 0.1.0 ​

luau
S.goTo(
	target: Name, -- The state to move to
	data: Data? -- Reaches every state entered, as run.data
): GoTo<Name, Data>

Makes a GoTo, the value that onProcess and handler functions return to move the machine. Return stay to take an event without moving, or nil to pass it on to the outer states.

CauseMessage
Errortarget is not a stringCanopy.goTo: the target is a state's name.

stay Added in 0.1.0 ​

luau
S.stay(): Stay

Makes a Stay, which a handler function returns to take an event, or a time-up, without moving. The event stops at that state: the outer states never see it, as when a handler returns a goTo. Returning nil instead passes it on.

From onProcess, stay does the same as nil: the machine does not move, and the next state's onProcess runs.

luau
on = {
	-- Blocking takes the hit, so the outer `fighting` state does not flinch.
	hit = function(context, run, attacker)
		context.blockedHits += 1

		return S.stay()
	end,
},

new Added in 0.1.0 ​

luau
S.new(
	tree: Tree<Context, Event, Name, Data, Record>, -- The tree to run
	context: Context, -- The data shared by every state of this machine
	options: MachineOptions<Context, Event, Name, Data, Record>? -- The start time, hooks, random source, move limit and history
): Machine<Context, Event, Name, Data, Record>

Makes a machine and starts it: it enters the tree's initial state and its initial children at once, at options.at. The fields of options are listed under MachineOptions.

Returns the started Machine.

CauseMessage
Errortree was not made with treeCanopy.new: the tree must be made with tree.
Erroroptions.maxMoves is less than 1Canopy.new: 'maxMoves' must be 1 or more.
Erroroptions.history was not made with historyCanopy.new: 'history' must be made with history.
Erroroptions.timeScale is not more than 0Canopy.new: 'timeScale' must be more than 0.
Erroroptions.trace is less than 1Canopy.new: 'trace' must be 1 or more.

restore Added in 0.1.0 ​

luau
S.restore(
	tree: Tree<Context, Event, Name, Data, Record>, -- The tree the snapshot was taken from
	context: Context, -- The data shared by every state of this machine
	snapshot: Snapshot<Name, Data>, -- What getSnapshot returned
	options: MachineOptions<Context, Event, Name, Data, Record>? -- As for new; the snapshot's time and time scale win
): Machine<Context, Event, Name, Data, Record>

Makes a machine that stands where getSnapshot was called: in the same states, with the same runs, time, pause, time scale, cooldowns and remembered children. It resumes those states rather than entering them, so no onStart, record or hook runs. Each state's memory is made again by its factory, from the context and the run's data.

The snapshot is checked against the tree first. See Snapshots for sending one to clients.

CauseMessage
Errorsnapshot is not a table with runsCanopy.restore: the snapshot must be made by getSnapshot.
Errorsnapshot.runs is emptyCanopy.restore: the snapshot has no states, which a stopped machine gives.
ErrorA run's state is not at that depth, or not under the run before itCanopy.restore: '<name>' is not where the snapshot puts it in the tree.
ErrorA run's cue is not a cue of its state's timeline, or its cueAt is not a numberCanopy.restore: the run of '<name>' has a cue its state's timeline does not have.
ErrorThe last run's state has childrenCanopy.restore: the snapshot ends at '<name>', which holds other states.
ErrorA cooldown or remembered child names a state the tree does not hold thereCanopy.restore: the snapshot has a cooldown for '<name>', which is not in the tree.

The errors of new apply to tree and options as well, with Canopy.restore in place of Canopy.new.