RbxUtility (Library)/LuaSignal: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens No edit summary |
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">" |
||
(3 intermediate revisions by the same user not shown) | |||
Line 39: | Line 39: | ||
== Example == | == Example == | ||
< | <syntaxhighlight lang="lua"> | ||
--first, import the constructor from the library | --first, import the constructor from the library | ||
CreateSignal = LoadLibrary('RbxUtility').CreateSignal | CreateSignal = LoadLibrary('RbxUtility').CreateSignal | ||
Line 66: | Line 66: | ||
wait(1) | wait(1) | ||
sig:fire("Hello, World, again!") | sig:fire("Hello, World, again!") | ||
</ | </syntaxhighlight> |
Latest revision as of 06:08, 27 April 2023
The LuaSignal class provides a mechanism to create "custom events" which work much like the real RBXScriptSignal objects, which ROBLOX events use, but for use in Lua-side data structures, rather than Instances. The main differences in behavior of the LuaSignal objects is that they wait one extra frame between being fired and the handler being called, and they have an additional "disconnect" method which is deprecated for the normal RBXScriptSignal objects.
The LuaSignal class is located in the RbxUtility_(Library) library.
API
connect( function handler ) | |
Returns | LuaConnection |
Description: | Connects this signal to the specified handler, and returns a connection which can be used to disconnect this specific connection in the future |
wait( ) | |
Returns | The arguments passed to the last call to fire. |
Description: | This call does not return until a call to fire has been made, at which point it will return with the arguments that were passed to that call. |
fire( ... ) | |
Returns | nil |
Description: | Calls any handlers connected to this function with the given arguments, and also notifies any code waiting on this signal to resume. The handlers are called *asynchronously* so this call will not block until the handlers return. |
disconnect( ) | |
Returns | nil |
Description: | Disconnects all connected handlers from this Signal. Note that this does *not* make code waiting on this Signal resume, the code will only resume when fire is called. |
Example
--first, import the constructor from the library
CreateSignal = LoadLibrary('RbxUtility').CreateSignal
--create a signal object
local sig = CreateSignal()
--connect it to something
local connection = sig:connect(function(msg)
print("Message: "..tostring(msg))
end)
--fire it
sig:fire("Hello, World!")
--disconnect it
connection:disconnect()
--wait on it:
Spawn(function()
local msg = sig:wait()
print("Waited for message: "..tostring(msg))
end)
--fire it after a bit, at which point the previous wait will resume and the message will be printed
wait(1)
sig:fire("Hello, World, again!")