UPDATE:
This module will no longer receive updates as there is a new module released by me with various improvements. Please check it out here:
(Details on the post itself)
Original Post
As a developer, it has been difficult to find a way to create custom, modular, and efficient events in Roblox. The best solution I could find was @Quenty 's Signal class, which unfortunately has no documentation or tutorial here on the DevForum. Also, it used BindableEvents, and was not very easy to make cross-script compatible. That is why I decided to recreate this and call it Signal Class 2.
LINKS:
Download Module
Github (Still relatively new to Github, lemme know if there are issues)
Updates and Releases
What is different from this and the original Signal Class:
- It does not use BindableEvents to make connections to a signal
- It includes more functions that you may need
- It was designed to be cross-script compatible, and is easy to use when making modules of your own (ex. gun module, cash module, etc.)
BENCHMARKS
For a brand new event on a server with one function connected to it, it takes an average of:
<0.0001seconds.
For a brand new event with 10 connections that perform basic mathematical calculations (The one I used was pi/2), it takes an average of <0.0001 seconds.
For a brand new event with 50 connections that perform basic mathematical calculations (The one I used was pi/2), it takes an average of <0.0002seconds.
Firing or connecting cross-script would have the same exact benchmarks, because of the way it is programmed.
I believe the numbers for the first 2 are smaller, but unfortunately there is no way to know, as benchmark numbers only have so many decimal digits.
DOCUMENTATION
Signal.new(name optional)
This creates a new signal which can be accessed cross-script. It has one parameter which is a name, which you will need if you plan on making it cross-script compatible. If you only plan on using it in the script you make it in, it is unnecessary to fill this parameter. This function returns one value, which is a new and blank custom signal.
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.new("example") -- Again, the name is optional, and can only be used when making it cross script-compatible.
Signal.Get(name)
This gets a signal with the given name if one exists. If none exist, it returns nil. This is cross-script compatible, so creating a signal in one script and retrieving it in another will work.
Think of it the same way you would the :FindFirstChild() function
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.Get("example") -- Will attempt to get a signal if one exists. You can use this to get signals from different scripts
Signal.WaitFor(name,timeOut optional)
This is nearly the same as the Get function, except it yields until it finds a signal with the given name or when the timeOut is reached, whichever comes first. It will release a warning if no signal is found in the given timeOut.
The timeOut value is optional, and the default value is 5.
This function is more reliable than the Get function, but should only be used at the beginning of scripts. the Get function should be used when returning nil is intended behavior.
Think of it the same way you would the :WaitForChild() function
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.WaitFor("example",10) -- Yields the thread until it finds a signal or time runs out
Signal:Connect(function)
Connects the given function to the signal the function is ran on. Whenever the signal is fired, the function runs. This is a non-yielding function, similar to the real :Connect function, so it can be called in the middle of a thread. The function that is given as a parameter can accept multiple parameters.
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.new() -- Creates a signal
ExampleSignal:Connect(function()
print("This is an example of the connect function!")
end)
Signal:Fire(extra parameters optional)
Fires the given signal, which runs all functions that are connected to it using the :Connect() function. This fires cross-script.
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.new() -- Creates a signal
ExampleSignal:Connect(function(param)
print(param)
end)
ExampleSignal:Fire("This is an example of passing a paramter")
Signal:Wait(function)
Yields the thread it is called in until the signal that is given is fired. This does yield.
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.new() -- Creates a signal
ExampleSignal:Wait() -- Waits until the signal is called
print("It was called!") -- Of course this runs when the signal is called. This can be cross-script.
Signal:Destroy()
Destroys the signal, which prevents it from being fired and prevents connected functions from running. This does not error, so attempting to fire a destroyed signal will not cause an error.
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.new() -- Creates a signal
wait(5) -- Waits a bit...
ExampleSignal:Destroy() -- Destroys the signal
Connection:Disconnect()
Disconnects the function from the connection from the signal it is attached to. This function can only be run on a connection, so doing Signal:Disconnect() will error.
Example:
local Signal = require(script.Signal) -- Or wherever your module is located
local ExampleSignal = Signal.new() -- Creates a signal
local ExampleConnection = ExampleSignal:Connect(function()
print("This is an example of the connect function!")
end)
wait(5) -- Waits a bit
ExampleConnection:Disconnect() -- Disconnects the function from the ExampleSignal
Please let me know if you have any reccomendations or if you encounter a problem and enjoy the module!