Parallel regions
A character can be jumping and attacking at once. In one tree that needs a state for every pair, such as jumpingAndAttacking, and the count grows with each new state. Parallel regions keep each concern in its own tree: movement knows standing and airborne, combat knows ready and swinging, and both run at once.
S.parallel makes one machine per region, all on the same context. Events, process, pauses and the time scale reach every region. A move or a question about a state goes to the region that holds it, so each state's name is used once across the regions.
const movement = S.tree({
initial = "standing",
states = {
standing = S.state({
tags = { "canAttack" },
on = {
jump = "airborne",
},
}),
airborne = S.state({
on = {
landed = "standing",
},
}),
},
})
const combat = S.tree({
initial = "ready",
states = {
ready = S.state({
on = {
attack = "swinging",
},
}),
swinging = S.state({
-- Asks the other region through the context.
canEnter = function(context)
return context.states:hasTag("canAttack")
end,
duration = 0.5,
onTimeUp = "ready",
}),
},
})const context = {
humanoid = humanoid,
}
context.states = CharacterStates.S.parallel({
movement = CharacterStates.movement,
combat = CharacterStates.combat,
}, context, {
at = os.clock(),
})
context.states:send("jump")
context.states:send("attack") -- Refused: `swinging` needs `canAttack`, which `airborne` does not have.A region's states reach the others through the context, as above: put the parallel in the context, then ask it with isIn or hasTag. Prefer tags to names, so a region does not depend on how another one names its states.
Each region is an ordinary machine. Use getRegion for what is not listed here, such as its trace or its path. To process many parallels together, add each region to a group.
Making a parallel
parallel Added in 0.1.0
S.parallel(
regions: { [string]: Tree<Context, Event, Name, Data, Record> }, -- Each region's name and tree
context: Context, -- Shared by every region
options: MachineOptions<Context, Event, Name, Data, Record>? -- Given to each region's machine
): Parallel<Context, Event, Name, Data, Record>Makes one machine per region with new, each from its own tree. The regions are reached in the order of their names. A history in options is shared, so it records the states of every region in one list.
| Cause | Message | |
|---|---|---|
| Error | regions is empty, or not a map of names to trees | Canopy.parallel: 'regions' maps names to trees made with tree. |
| Error | Two regions have a state with the same name | Canopy.parallel: the state '<name>' is in the regions '<region>' and '<region>'. A name must be used once across the regions. |
isParallel Added in 0.1.0
Canopy.isParallel(value: unknown): booleanReturns whether value was made by parallel.
Moving the regions
send Added in 0.1.0
parallel:send(event: Event, data: Data?, at: number?, options: SendOptions?): booleanSends the event to every region, as machine:send would. Returns whether any region moved. With a buffer, each region that does not take the event keeps it.
process Added in 0.1.0
parallel:process(now: number): booleanProcesses every region. Returns whether any moved.
goTo Added in 0.1.0
parallel:goTo(target: Name, data: Data?, at: number?): booleanMoves the region that holds target, as machine:goTo would. The other regions stay where they are.
pause, resume and setTimeScale Added in 0.1.0
parallel:pause(at: number?): ()
parallel:resume(at: number?): ()
parallel:setTimeScale(scale: number, at: number?): ()Pause, resume or slow every region.
stop Added in 0.1.0
parallel:stop(): ()Stops every region.
Reading the regions
isIn Added in 0.1.0
parallel:isIn(name: Name): booleanReturns whether the region that holds name is in it, or in a state inside it.
canSend Added in 0.1.0
parallel:canSend(event: Event, data: Data?, at: number?): booleanReturns whether any region would take event, as machine:canSend reads it.
hasTag Added in 0.1.0
parallel:hasTag(tag: string): booleanReturns whether a state on the active path of any region has tag.
getTags Added in 0.1.0
parallel:getTags(): { string }Returns the tags of every region's active path, sorted, each once, in a new list.
getRegion Added in 0.1.0
parallel:getRegion(regionName: string): Machine<Context, Event, Name, Data, Record>?Returns the machine of the region named regionName, or nil for a name the parallel does not have.
getRegionNames Added in 0.1.0
parallel:getRegionNames(): { string }Returns the names of the regions, sorted, in a new list.
getSnapshot Added in 0.1.0
parallel:getSnapshot(): { [string]: Snapshot<Name, Data> }Returns a snapshot of each region, by its name. Restore each one with restore and its region's tree.
isStopped Added in 0.1.0
parallel:isStopped(): booleanReturns whether every region has stopped.
getContext Added in 0.1.0
parallel:getContext(): ContextReturns the context shared by the regions.