Hey there! ConnectionController is a module that puts connections (along with some extra info) into tables you predefine, and can be accessed cross-script. This module doesn’t provide any performance boosts, it’s mainly for organisation and having a bit more control over all your connections in your game.
Here’s a brief explanation of how it works:
Categories
-
addCategory(category)
is how you add a table you want to put connections into. It can have any name as long as it’s a string and isn’t repeated. -
removeCategory(category)
is self-explanatory, it just removes the named category after disconnecting all connection within it. -
disableCategory(category, toggle)
is how you toggle your connections on and off (note, this disconnects the signals, then reconnects them) -
clearCategory(category)
disconnects and removes all the connections in the named category. -
pruneCategory(category, override)
removes all inactive connections, setting override to true will also remove all disabled connections. (useful if you’re disconnecting outside of the module) -
returnCategoryContents(category)
returns the named category table so all connections within can be read. If category is set to nil, all categories will be returned.
Signals
-
addSignal(category, name, signal, func, ...)
adds a connection along with a chosen name and function, into the named category. ‘signal’ must be the Script Signal itself (e.g. workspace.Baseplate.Touched). The ‘…’ represents parameters of the chosen function, and can be any amount and type of parameters. -
removeSignal(category, name)
disconnects and removes the named signal in the named category. -
disableSignal(category, name, toggle)
is how you toggle a single connection on and off (note, this disconnects the signal, then reconnects it)
I hope you find this useful for keeping things easy and tidy , here’s the module:
Module Code (Categories)
local categoryTables = {}
return {
addSignal = function(category, name, signal, func, ...)
assert(categoryTables[category] ~= nil, category.." isn't a Category")
assert(typeof(signal) == "RBXScriptSignal", "The 3rd parameter of 'addFunction' must be a Script Signal (E.g. 'workspace.Baseplate.Touched')")
assert(type(func) == "function", "The 4th parameter of 'addFunction' must be a Function")
local vars = table.pack(...)
local conn = signal:Connect(function(...)
local builtInVars = table.pack(...)
for i, var in ipairs(vars) do
table.insert(builtInVars, var)
end
func(table.unpack(builtInVars))
end)
categoryTables[category][name] = {
Connection = conn,
Signal = signal,
Function = func,
Disabled = false,
Params = vars
}
return conn, vars
end,
removeSignal = function(category, name)
assert(categoryTables[category] ~= nil, category.." isn't a Category")
assert(categoryTables[category][name] ~= nil, name.." wasn't found in "..category)
categoryTables[category][name].Connection:Disconnect()
categoryTables[category][name] = nil
end,
disableSignal = function(category, name, toggle)
assert(categoryTables[category] ~= nil, category.." isn't a Category")
assert(categoryTables[category][name] ~= nil, name.." wasn't found in "..category)
local tbl = categoryTables[category][name]
if toggle then
tbl.Connection:Disconnect()
tbl.Disabled = toggle
else
tbl.Connection = tbl.Signal:Connect(function(...)
tbl.Function(..., table.unpack(tbl.Params))
end)
end
end,
addCategory = function(category)
assert(categoryTables[category] == nil, category.." already exists, use 'removeCategory' or 'clearCategory' instead and as needed")
assert(type(category) == "string", "The 1st parameter of 'addCategory' must be a String")
categoryTables[category] = {}
end,
removeCategory = function(category)
assert(type(category) == "string", "The 1st parameter of 'addCategory' must be a String")
for _, tbl in pairs(categoryTables[category]) do
tbl.Connection:Disconnect()
end
categoryTables[category] = nil
end,
disableCategory = function(category, toggle)
assert(categoryTables[category] ~= nil, category.." isn't a Category")
assert(type(toggle) == "boolean", "The 2nd parameter of 'disableCategory' must be a Boolean")
for _, tbl in pairs(categoryTables[category]) do
if toggle then
tbl.Connection:Disconnect()
tbl.Disabled = toggle
else
tbl.Connection = tbl.Signal:Connect(function(...)
tbl.Function(..., table.unpack(tbl.Params))
end)
end
end
end,
wipeCategory = function(category)
assert(categoryTables[category] ~= nil, category.." isn't a Category")
for _, tbl in pairs(categoryTables[category]) do
tbl.Connection:Disconnect()
end
table.clear(categoryTables[category])
end,
pruneCategory = function(category, override)
for func, _ in pairs(categoryTables[category]) do
local tbl = categoryTables[category][func]
local active = tbl.Connection.Connected
local disabled = tbl.Disabled
if (not active and not disabled) then categoryTables[category][func] = nil end
if override and not active then categoryTables[category][func] = nil end
end
end,
returnCategoryContents = function(category)
if (category == nil) then return categoryTables end
assert(categoryTables[category] ~= nil, category.." isn't a Category")
return categoryTables[category]
end
}
--[[
Made by M_dgettMann (shadowflame63)
Example usage for adding/removing signals to/from a category:
Module.addSignal("Category", "Name", function() end, "params", "go", "here", "with", "no", "limit") -- Adding a signal with a repeated name will overrite the previous one
Module.removeSignal("Category", "Name")
Example of toggling a signal to run:
Module.disableSignal("Category", "Name", true) -- Stops signal from running
Module.disableSignal("Category", "Name", false) -- Lets signal run again
Example of adding/removing categories:
Module.addCategory("Category") -- Adding a category with a repeated name will overrite the previous one
Module.removeCategory("Category")
Example of toggling a signals in a category to run:
Module.disableCategory("Category", true) -- Stops signals in the 'Category' category from running
Module.disableCategory("Category", false) -- Lets signals in the 'Category' category run again
Example of wiping all current running signals in a category:
Module.wipeCategory("Category") -- All signals in the 'Category' category are removed
Example of clearing all inactive signals in a category:
Module.pruneCategory("Category") -- All signals that have been disconnected but not disabled are removed
Module.pruneCategory("Category", true) -- All signals that have been disconnected and are disabled are removed
Example of diabling all current running signals in an category:
Module.disableCategory("Category", true) -- All signals in the 'Category' category are disabled
Module.disableCategory("Category", false) -- All signals in the 'Category' category are enabled
Example of accessing a snapshot of all current categories and signals:
Module.returnCategoryContents("Category") -- return all signals in the 'Category' category
Module.returnCategoryContents() -- return all signals in all categories
]]
Module Code (No Categories)
local signalTable = {}
return {
addSignal = function(name, signal, func, ...)
assert(typeof(signal) == "RBXScriptSignal", "The 3rd parameter of 'addFunction' must be a Script Signal (E.g. 'workspace.Baseplate.Touched')")
assert(type(func) == "function", "The 4th parameter of 'addFunction' must be a Function")
local vars = table.pack(...)
local conn = signal:Connect(function(...)
local builtInVars = table.pack(...)
for i, var in ipairs(vars) do
table.insert(builtInVars, var)
end
func(table.unpack(builtInVars))
end)
categoryTables[category][name] = {
Connection = conn,
Signal = signal,
Function = func,
Disabled = false,
Params = vars
}
return conn, vars
end,
removeSignal = function(name)
assert(signalTable[name] ~= nil, name.." wasn't found")
signalTable[name].Connection:Disconnect()
signalTable[name] = nil
end,
disableSignal = function(name, toggle)
assert(signalTable[name] ~= nil, name.." wasn't found")
local tbl = signalTable[name]
if toggle then
tbl.Connection:Disconnect()
tbl.Disabled = toggle
else
tbl.Connection = tbl.Signal:Connect(function(...)
tbl.Function(..., table.unpack(tbl.Params))
end)
end
end,
wipeSignals = function()
for _, tbl in pairs(signalTable) do
tbl.Connection:Disconnect()
end
table.clear(signalTable)
end,
pruneSignals = function(override)
for func, _ in pairs(signalTable) do
local tbl = signalTable[func]
local active = tbl.Connection.Connected
local disabled = tbl.Disabled
if (not active and not disabled) then signalTable[func] = nil end
if override and not active then signalTable[func] = nil end
end
end,
returnAllSignals = function()
return signalTable
end
}
--[[
Made by M_dgettMann (shadowflame63)
Example of adding/removing signals:
Module.addSignal("Name", Example.Signal, function() end, "params", "go", "here") -- Adding a signal with a repeated name will overrite the previous one
Module.removeSignal("Name")
Example of toggling a signal to run:
Module.disableSignal("Name", true) -- Stops signal from running
Module.disableSignal("Name", false) -- Lets signal run again
Example of wiping all signals:
Module.wipeSignals() -- All signals are disconnected and removed
Example of clearing all inactive signals:
Module.pruneSignals() -- All signals that have been disconnected but not disabled are removed
Module.pruneSignals(true) -- All signals that have been disconnected and are disabled are removed
Example of accessing a snapshot of all signals:
Module.returnAllSignals() -- return all signals in a table
]]
Edit: I’ve added a version that doesn’t include categories so all your signals can be in one table, the functions do the same thing, just with different names for the wiping and pruning.
This module is outdated and no longer supported.