Signal
DownloadSignal is an open-sourced module used to make custom RBXScriptSignal events. It replicates the behavior while being as light-weight as possible. This makes use of BindableEvents, and also is coded so arguments passed when firing a Signal does not get deep copied when being sent through the BindableEvents.
Where can I download it?
You can download and view the module with the following links:What about documentation?
For complete understanding of the module, it is highly recommended you view the following documentation:Signal.new
Used to create a new Signal object to use :Fire, :Connect, etc functions on.Usage:
Signal.new()
Parameters:
nil
Returns:
Signal
Signal:Connect
Connects a function to whenever the Signal firesUsage:
Signal:Connect(handler)
Parameters:
handler -- The function you wish to connect to the Signal
Returns:
Connection -- The connection between the function and Signal
Aliases:
Signal:connect(handler)
Signal:Wait
Yields until the :Fire method is called on the Signal.Usage:
Signal:Wait()
Parameters:
nil
Returns:
... -- All parameters passed by the :Fire() function that triggered the :Wait() function
Aliases:
Signal:wait()
Signal:Fire
Fires the Signal and triggers any functions connected with :Connect() and stops yielding any current :Wait()s.Usage:
Signal:Fire(...)
Parameters:
... -- Any amount of parameters
Returns:
nil
Aliases:
Signal:fire()
Example of use:
local Signal = require(script.Signal) -- In this example, it assumes that the module is located under the script. Change this to wherever your module is located.
local Clicked = Signal.new() -- I created a signal for whenever the player clicks
Clicked:Connect(function(Count)
print("Click detected! Click #"..Count)
end)
local ClickCount = 0
game:GetService("UserInputService").InputBegan:Connect(function(Input) -- This uses UIS to detect whenever input begins
if Input.UserInputType == Enum.UserInputType.MouseButton1 then -- Checks if the input is a left click
ClickCount += 1
Clicked:Fire(ClickCount) -- I pass the amount of clicks here when firing
end
end)
Source Code
-- I did not include debug traceback to make it as light-weight as possible
local Signal = {}
Signal.__index = Signal
Signal.ClassName = "Signal"
-- Constructor
function Signal.new()
return setmetatable({
_bindable = Instance.new("BindableEvent"),
_args = nil,
_argCount = nil, -- To stay true to _args, even when some indexes are nil
},Signal)
end
function Signal:Fire(...)
-- I use this method of arguments because when passing it in a bindable event, it creates a deep copy which makes it slower
self._args = {...}
self._argCount = select("#", ...)
self._bindable:Fire()
end
function Signal:fire(...)
return self:Fire(...)
end
function Signal:Connect(handler)
if not (type(handler) == "function") then
error(("connect(%s)"):format(typeof(handler)), 2)
end
return self._bindable.Event:Connect(function()
handler(unpack(self._args,1,self._argCount))
end)
end
function Signal:connect(...)
return self:Connect(...)
end
function Signal:Wait()
self._bindableEvent.Event:Wait()
assert(self._argData, "Missing argument data, likely due to :TweenSize/Position corrupting.")
return unpack(self._args, 1, self._argCount)
end
function Signal:wait()
return self:Wait()
end
function Signal:Destroy()
if self._bindable then
self._bindable:Destroy()
self._bindable = nil
end
self._args = nil
self._argCount = nil
setmetatable(self, nil)
end
function Signal:destroy()
return self:Destroy()
end
return Signal