RbxUtility (Library)/LuaSignal: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>XLEGOx
Added LuaSignal class documentation.
>JulienDethurens
yay, it work! We can now fix these links that have nothing to do with the actual functions. :)
Line 12: Line 12:
| returns =    [[RbxUtility_(Library)/LuaConnection|LuaConnection]] <var>connection</var>
| returns =    [[RbxUtility_(Library)/LuaConnection|LuaConnection]] <var>connection</var>
| description = Connects this signal to the specified handler, and returns a connection which can be used to disconnect this specific connection in the future
| description = Connects this signal to the specified handler, and returns a connection which can be used to disconnect this specific connection in the future
|nolink = true
}}
}}


Line 19: Line 20:
| returns =    The arguments passed to the last call to fire.
| 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.
| 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.
|nolink = true
}}
}}


Line 26: Line 28:
| returns =    None
| returns =    None
| 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.
| 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.
|nolink = true
}}
}}


Line 33: Line 36:
| returns =    None
| returns =    None
| 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.
| 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.
|nolink = true
}}
}}



Revision as of 06:14, 9 February 2012

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 only differences in behavior of the LuaSignal objects is that they wait and extra frame between being fired and the handler being called, and they have an additional "disconnect" method which is depreciated for the normal RBXScriptSignal objects.

The LuaSignal class is located in the RbxUtility_(Library) library.

API

connect( function handler )
Returns LuaConnection connection
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( None )
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( Any arguments )
Returns None
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( None )
Returns None
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!")