Calling signal:Fire(...) yields the thread until all connected functions completed.
Be careful with functions you connect, you might accidentally lock the calling thread.
Tables you pass as arguments to signal:Fire(...) might be changed by connected handlers.
You can use it to retrieve a result of calling several handlers one by one.
Custom Signal can be added as a dependency in Wally project:
[dependencies]
CustomSignal = "bogotolec/custom-signal@0.1.0"- Go to releases page
- Download CustomSignal.rbxm of preferred version
- Insert the file into project of Roblox Studio
Signals are created simply by requiring the library and calling CustomSignal.new():
local CustomSignal = require("path/to/custom-signal")
local signal = CustomSignal.new()There are 2 supported types of connections: permanent and once.
| Type | Permanent connection | Once connection |
|---|---|---|
| Creation | signal:Connect(func) |
signal:Once(func) |
| # of invokes | ∞ | 1 |
| Order of call | First | Second |
Order of call means that when you fire an event, firstly all the permanent connections will be invoked.
local onceConnection = signal:Once(function(v)
print(`Called once: {v}`)
end)
local permanentConnection = signal:Connect(function(v)
print(`Called permanent: {v}`)
end)
signal:Fire(1)
-- Output:
-- Called permanent: 1
-- Called once: 1
signal:Fire(2)
-- Output:
-- Called permanent: 2Connections preserve order during fire in which they were connected.
local connection1 = signal:Connect(function()
print(`Called 1`)
end)
local connection2 = signal:Connect(function()
print(`Called 2`)
end)
local connection3 = signal:Connect(function()
print(`Called 3`)
end)
signal:Fire()
-- Output:
-- Called 1
-- Called 2
-- Called 3Connections can be disconnected, which stops invoking connected function
local connection1 = signal:Connect(function()
print(`Called 1`)
end)
local connection2 = signal:Connect(function()
print(`Called 2`)
end)
signal:Fire()
-- Output:
-- Called 1
-- Called 2
connection1:Disconnect()
signal:Fire()
-- Output:
-- Called 2If signal:Fire(...) is called before any connection was created, event is stored and will be fired
to invoke the next connected function. This means that function can be invoked during signal:Connect(), yielding the thread.
signal:Fire("Hello world!")
local connection = signal:Connect(function(v)
print(v)
end)
-- Output:
-- Hello world!