Skip to content

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.

luau
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",
		}),
	},
})
luau
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 ​

luau
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.

CauseMessage
Errorregions is empty, or not a map of names to treesCanopy.parallel: 'regions' maps names to trees made with tree.
ErrorTwo regions have a state with the same nameCanopy.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 ​

luau
Canopy.isParallel(value: unknown): boolean

Returns whether value was made by parallel.

Moving the regions ​

send Added in 0.1.0 ​

luau
parallel:send(event: Event, data: Data?, at: number?, options: SendOptions?): boolean

Sends 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 ​

luau
parallel:process(now: number): boolean

Processes every region. Returns whether any moved.

goTo Added in 0.1.0 ​

luau
parallel:goTo(target: Name, data: Data?, at: number?): boolean

Moves 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 ​

luau
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 ​

luau
parallel:stop(): ()

Stops every region.

Reading the regions ​

isIn Added in 0.1.0 ​

luau
parallel:isIn(name: Name): boolean

Returns whether the region that holds name is in it, or in a state inside it.

canSend Added in 0.1.0 ​

luau
parallel:canSend(event: Event, data: Data?, at: number?): boolean

Returns whether any region would take event, as machine:canSend reads it.

hasTag Added in 0.1.0 ​

luau
parallel:hasTag(tag: string): boolean

Returns whether a state on the active path of any region has tag.

getTags Added in 0.1.0 ​

luau
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 ​

luau
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 ​

luau
parallel:getRegionNames(): { string }

Returns the names of the regions, sorted, in a new list.

getSnapshot Added in 0.1.0 ​

luau
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 ​

luau
parallel:isStopped(): boolean

Returns whether every region has stopped.

getContext Added in 0.1.0 ​

luau
parallel:getContext(): Context

Returns the context shared by the regions.