Skip to content

Groups ​

A group holds many machines and processes them together. Each process runs only:

  • the machines in a state with onProcess, which must run every time, and
  • the machines whose next duration has ended.

A machine that is only waiting, for an event or for a duration that has not ended, costs nothing. The group keeps the end times in a heap, so finding the machines that are due does not look at the others. With a thousand enemies that mostly stand still, one group:process costs a fraction of calling process on each one.

luau
const RunService = game:GetService("RunService")
const EnemyStates = require(ReplicatedStorage.EnemyStates)

const enemies = EnemyStates.S.group()

const function spawnEnemy(model: Model)
	const machine = EnemyStates.S.new(EnemyStates.tree, contextOf(model), {
		at = os.clock(),
	})

	enemies:add(machine)
	model.Destroying:Once(function()
		machine:stop()
	end)
end

RunService.Heartbeat:Connect(function()
	enemies:process(os.clock())
end)

Sending events and calling goTo work as on any machine. The group learns of the new end time each time a machine moves, is paused, resumed or slowed, or has an end changed by its own functions.

Making a group ​

group Added in 0.1.0 ​

luau
S.group(): Group<Context, Event, Name, Data, Record>

Makes an empty group for machines of this setup.

Using a group ​

add Added in 0.1.0 ​

luau
group:add(machine: Machine<Context, Event, Name, Data, Record>): ()

Adds a machine. A machine is in one group at most. Adding it to its own group again, or adding a stopped machine, does nothing.

CauseMessage
Errormachine was not made with new or restoreCanopy: a group holds machines made with new or restore.
Errormachine is in another groupCanopy: the machine is in another group already. Remove it from that group first.

remove Added in 0.1.0 ​

luau
group:remove(machine: Machine<Context, Event, Name, Data, Record>): ()

Takes a machine out of the group. A machine that is stopped leaves its group by itself.

process Added in 0.1.0 ​

luau
group:process(now: number): number

Processes the machines that poll with onProcess and those whose next duration ended at or before now, as machine:process would. Returns how many of them moved.

The machines due are found before any of them runs, so a machine's functions may send events to other machines in the group, add machines or remove them. A machine made due during the call, such as one entering a state with a duration of 0, runs on the next call.

has Added in 0.1.0 ​

luau
group:has(machine: Machine<Context, Event, Name, Data, Record>): boolean

Returns whether machine is in the group.

getSize Added in 0.1.0 ​

luau
group:getSize(): number

Returns how many machines are in the group.

getMachines Added in 0.1.0 ​

luau
group:getMachines(): { Machine<Context, Event, Name, Data, Record> }

Returns a new list of the machines in the group, in no particular order.

isGroup Added in 0.1.0 ​

luau
Canopy.isGroup(value: unknown): boolean

Returns whether value was made by group.