Signal
Lua-side duplication of the API of events on Roblox objects.
Signals are needed for to ensure that for local events objects are passed by reference rather than by value where possible, as the BindableEvent objects always pass signal arguments by value, meaning tables will be deep copied. Roblox's deep copy method parses to a non-lua table compatable format.
This class is designed to work both in deferred mode and in regular mode. It follows whatever mode is set.
local signal = Signal.new()
local arg = {}
signal:Connect(function(value)
assert(arg == value, "Tables are preserved when firing a Signal")
end)
signal:Fire(arg)
info
Why this over a direct BindableEvent? Well, in this case, the signal prevents Roblox from trying to serialize and desialize each table reference fired through the BindableEvent.
Functions
new
Constructs a new signal.
isSignal
Signal.
isSignal
(
value:
any
) →
boolean
Returns whether a class is a signal
Destroy
Signal.
Destroy
(
) →
(
)
Alias for [DisconnectAll]
isSignal
Signal.
isSignal
(
value:
any
) →
boolean
Returns whether a class is a signal
new
Constructs a new signal.
Connect
Signal:
Connect
(
fn:
(
...
T
)
→
(
)
--
Function handler called when :Fire(...)
is called
) →
RBXScriptConnection
Connect a new handler to the event. Returns a connection object that can be disconnected.
DisconnectAll
Signal:
DisconnectAll
(
) →
(
)
Disconnects all connected events to the signal.
info
Disconnect all handlers. Since we use a linked list it suffices to clear the reference to the head handler.
Fire
Signal:
Fire
(
...:
T
--
Variable arguments to pass to handler
) →
(
)
Fire the event with the given arguments. All handlers will be invoked. Handlers follow
::: info Signal:Fire(...) is implemented by running the handler functions on the coRunnerThread, and any time the resulting thread yielded without returning to us, that means that it yielded to the Roblox scheduler and has been taken over by Roblox scheduling, meaning we have to make a new coroutine runner. :::
Wait
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsSignal:
Wait
(
) →
T
Wait for fire to be called, and return the arguments it was given.
::: info Signal:Wait() is implemented in terms of a temporary connection using a Signal:Connect() which disconnects itself. :::
Once
Signal:
Once
(
fn:
(
...
T
)
→
(
)
--
One-time function handler called when :Fire(...)
is called
) →
RBXScriptConnection
Connect a new, one-time handler to the event. Returns a connection object that can be disconnected.
::: info -- Implement Signal:Once() in terms of a connection which disconnects -- itself before running the handler. :::
Fire
Signal:
Fire
(
...:
T
--
Variable arguments to pass to handler
) →
(
)
Fire the event with the given arguments. All handlers will be invoked. Handlers follow
Connect
Signal:
Connect
(
handler:
(
...
T
)
→
(
)
--
Function handler called when :Fire(...)
is called
) →
RBXScriptConnection
Connect a new handler to the event. Returns a connection object that can be disconnected.
Once
Signal:
Once
(
handler:
(
...
T
)
→
(
)
--
One-time function handler called when :Fire(...)
is called
) →
RBXScriptConnection
Connect a new, one-time handler to the event. Returns a connection object that can be disconnected.
Wait
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsSignal:
Wait
(
) →
T
Wait for fire to be called, and return the arguments it was given.
Destroy
Signal:
Destroy
(
) →
(
)
Disconnects all connected events to the signal. Voids the signal as unusable. Sets the metatable to nil.