DeclaredSignal¶
Type
DeclaredSignal is a custom ComponentSignal declared through ComponentOptions.Signals on World.DefineComponent. Scripts can connect globally on the component handle, listen on a single entity with entity:On(signal, fn), or fire it for an entity with Fire.
Connect¶
Connect runs fn every time Fire triggers this signal on any entity, passing the entity followed by the declared arguments, and returns a Connection.
Example
-- Server script.
local Energy = World.DefineComponent("Energy", { Value = 100 }, {
Signals = { Drained = { Field.Arg("amount", 0) } },
})
local drained: DeclaredSignal<number> = (Energy :: any).Drained
local conn = drained:Connect(function(entity: Entity, amount: number)
print(entity.Name, amount)
end)
conn:Disconnect()
Once¶
Once runs fn the next time Fire triggers this signal on any entity, then disconnects it, and returns a Connection.
Example
Wait¶
Wait yields until Fire triggers this signal on any entity or seconds pass, returning true, the entity, and the declared arguments on fire, or false on timeout.
Example
-- Server script.
local Door = World.DefineComponent("Door", { Open = false }, {
Signals = { Opened = { Field.Arg("by", Field.Entity) } },
})
local opened: DeclaredSignal<Entity?> = (Door :: any).Opened
task.spawn(function()
local ok, door, actor = opened:Wait(10)
if ok and door then
print(door.Name, actor)
end
end)
Fire¶
Fire dispatches this declared signal for entity e with the declared payload arguments, waking both world-wide listeners on the component handle and per-entity e:On(signal, fn) listeners.