GenericSignal:
ARCHIVED: New Version
Information:
GenericSignal is a custom wrapper class for RBXScriptSignals. GenericSignal objects have all the functionality of an RBXScriptSignal with the bonus of being entirely script-based and very performant.
[details = “All About RBXScriptSignal Wrappers:”]
RBXScriptSignal wrappers are made to provide a faster alternative to BindableEvents, which are inherently slower due to their integration with the Roblox engine and the use of its Event system. Unlike BindableEvents, which are Instances, custom signal modules use pure Lua, which eliminates some unnecessary overhead and reduces the risk of memory leaks. Wrapper classes are also more convenient and offer greater flexibility and control over your events.
[/details]
[details = Why GenericSignal]
GenericSignal is slightly different from most other signal implementations and uses a more advanced coroutine caching system along with linked lists (yield-safe) to help deal with more operations. When compared to GoodSignal, which is currently one of the most popular signal classes, GenericSignal is able to consistently perform on level with, or in some cases, up to 2x faster than it. The module can do this since it is able to cache multiple coroutines for later usage, which allows signals that have yielding connections to fire much faster.
[/details]
Features:
Signal Iterators:
:FireAll()iterates through and fires all GenericSignal objects:DestroyAll()iterates through and destroys all GenericSignal objects
Metamethods:
__len()returns the number of connections__iter()iterates through connections (Useful forpairsandipairs)__call()calls the:Fire()method
Convenience (Non-base Variant):
- Method chaining
- Error handling
- Settings / Constants
Usage:
Variants:
This ModuleScript currently has two variants, one being Default which includes the extra features (for convenience) mentioned previously, and the other being Base, which sacrifices some features or error handling for faster runtimes.
General Usage (For all Variants):
[details = “Creating Signals”]
Creating a GenericSignal Object:
Creating a GenericSignal object is very simple since the constructor doesn’t take in any arguments. All you have to do is require the ModuleScript and call .new() and you have a new GenericSignal object.
local GenericSignal = require(game:GetService("ReplicatedStorage").GenericSignal )
local newSignal = GenericSignal.new()
- GenericSignal should be stored in RPS so both the server and client can access it
- You can change constants inside the module (Ex:
COROUTINE_CACHE_SIZE)
[/details]
[details = Adding Connections]
Adding Connections to a GenericSignal Object:
Three methods can be used to add different types of connections to a GenericSignal object: :Connect(), :Once(), and :Wait(). These methods have slightly different behaviors and different use-cases so make sure to choose whichever one is best for your situation:
:Connect()is the most basic and adds a connection.:Once()is similar to:Connect(), but the connection is destroyed after the signal is fired (hence the nameOnce).:Wait()is the most unique, and can be used to halt the thread it is called from until the signal is fired (it is also destroyed after this).:Wait()will also return the arguments passed as arguments to the corresponding:Fire()call.
newSignal:Connect(function() print(“this is a normal connection”) end)
newSignal:Once(function() print(“this connection is only triggered once”) end)
local fireArguments = newSignal:Wait(function() print(“this yields the running thread until it is triggered”) end)
[/details]
[details = “Firing Signals”]
Firing a GenericSignal Object:
Firing a GenericSignal object is pretty self-explanatory. All you have to do is call the :Fire() method and pass in any arguments for the signal’s connections.
newSignal:Connect(function(value) print(value) end)
newSignal:Fire(56) -- prints 56
[/details]
[details = “Resetting and Destroying Signals”]
Clearing and Destroying GenericSignal Objects:
Resetting or destroying a GenericSignal object is as easy as typing in the method name: :Reset() resets the object (disconnects all connections), and :Destroy() destroys it. It’s simple as that.
newSignal:Reset()
newSignal:Destroy()
[/details]
Other Info:
Most info on the ModuleScript can either be found in this post or in the README script found in its model, but if you have any feedback or questions about the module, feel free to ask them in this thread and I will try to respond to them within a day.