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:
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
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 againstEvent, but not the keys of anontable (see the known gap). With the list,stateerrors on anonkey that is not in it, as soon as the state is built, andsendwarns 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,stateerrors on a tag that is not in it, andhasTagwarns once about a tag that is not in it.
const S = Canopy.setup<<Context, Event, Name, Data, nil>>({
events = {
"knockedBack",
"landed",
"moveStarted",
},
tags = {
"canAttack",
"canMove",
},
})| Cause | Message | |
|---|---|---|
| Error | options.events holds something that is not a string | Canopy.setup: 'events' must be a list of the events' names. |
| Error | options.tags holds something that is not a string | Canopy.setup: 'tags' must be a list of the tags' names. |
| Error | A 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. |
| Warning | send 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
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.nilwhen they carry nothing.Record— what a recorded state adds to the history.nilwhen 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
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.
| Cause | Message | |
|---|---|---|
| Error | definition is not a table | Canopy.state: a state is a table. |
| Error | states, on, onStart, onProcess, onEnd or record has the wrong type | Canopy.state: '<field>' must be a <type>. |
| Error | initial is not a string | Canopy.state: 'initial' must be a state's name. |
| Error | duration is negative, or a { min, max } with min > max | Canopy.state: 'duration' must be seconds (0 or more) or { min, max }. |
| Error | onTimeUp is not a name or a function | Canopy.state: 'onTimeUp' must be a state's name or a function. |
| Error | definition sets identifier, memory or cues | Canopy.state: 'identifier', 'memory' and 'cues' are set by Canopy. Use withMemory for a state's memory. |
| Error | An entry of on is not a name mapped to a name or a function | Canopy.state: 'on' maps an event's name to a state's name or a function. |
| Error | A child in states was not made with state | Canopy.state: 'states' maps names to states made with state. |
withMemory Added in 0.1.0
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 thegoToor event that entered the state.
It returns the memory, of any type.
Returns a MemoryBuilder, whose state method builds the state.
| Cause | Message | |
|---|---|---|
| Error | factory is not a function | Canopy.withMemory: the memory factory must be a function. |
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
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
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 withstateorwithMemory.
Returns the checked Tree.
| Cause | Message | |
|---|---|---|
| Error | definition is not a table with a states table | Canopy.tree: a tree is { initial, states }. |
| Error | An outermost state was not made with state | Canopy.tree: 'states' maps names to states made with state. |
| Error | A name is used twice anywhere in the tree | Canopy.tree: the state '<name>' is used twice. A name must be used once in the whole tree. |
| Error | initial is not one of the outermost states | Canopy.tree: 'initial' must be one of the outermost states. |
| Error | A state with children has no initial, or one that is not a child | Canopy.tree: the state '<name>' holds other states and needs an 'initial' that is one of them. |
| Error | A state has an initial but no children | Canopy.tree: the state '<name>' has an 'initial' but holds no states. |
| Error | A handler in on or onTimeUp names a state the tree does not have | Canopy.tree: the state '<name>' goes to '<target>' on <event>, which is not in the tree. |
goTo Added in 0.1.0
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.
| Cause | Message | |
|---|---|---|
| Error | target is not a string | Canopy.goTo: the target is a state's name. |
stay Added in 0.1.0
S.stay(): StayMakes 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.
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
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.
| Cause | Message | |
|---|---|---|
| Error | tree was not made with tree | Canopy.new: the tree must be made with tree. |
| Error | options.maxMoves is less than 1 | Canopy.new: 'maxMoves' must be 1 or more. |
| Error | options.history was not made with history | Canopy.new: 'history' must be made with history. |
| Error | options.timeScale is not more than 0 | Canopy.new: 'timeScale' must be more than 0. |
| Error | options.trace is less than 1 | Canopy.new: 'trace' must be 1 or more. |
restore Added in 0.1.0
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.
| Cause | Message | |
|---|---|---|
| Error | snapshot is not a table with runs | Canopy.restore: the snapshot must be made by getSnapshot. |
| Error | snapshot.runs is empty | Canopy.restore: the snapshot has no states, which a stopped machine gives. |
| Error | A run's state is not at that depth, or not under the run before it | Canopy.restore: '<name>' is not where the snapshot puts it in the tree. |
| Error | A run's cue is not a cue of its state's timeline, or its cueAt is not a number | Canopy.restore: the run of '<name>' has a cue its state's timeline does not have. |
| Error | The last run's state has children | Canopy.restore: the snapshot ends at '<name>', which holds other states. |
| Error | A cooldown or remembered child names a state the tree does not hold there | Canopy.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.