[ARCHIVED] ConnectionController

Hey there! :wave: 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 :broom:, 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
]]
Module Models

ConnectionController(Categories) - Roblox
ConnectionController(NoCategories) - Roblox

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.

11 Likes

Hey, this module looks epic. I’ve been trying to use it like this

My problem is that the function params don’t work. Did i do it wrong?
image

The function doesn’t receive the parameters

1 Like

hmm, I’ll look into that and see what’s up

I’ve updated the code and models with a change that should fix your issue, if it doesn’t work, let me know :sweat_smile: (p.s. these functions also pass on parameters that are built into certain signals, e.g. DeltaTime is the first parameter of Heartbeat)

It still does not work
I’ve removed everything from the function besides print() for simplicity’s sake

It still prints nil

PS: I am using the version with categories

1 Like

Just looked over your module and found what was wrong

Here this is the working addSignal version

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 = {...}
  local conn = signal:Connect(function()
  	func(table.unpack(vars))
  end)

  categoryTables[category][name] = {
  	Connection = conn,
  	Signal = signal,
  	Function = func,
  	Disabled = false,
  	Params = vars
  }

  return conn, vars

end,

1 Like

Thanks for helping :grinning_face_with_smiling_eyes:, but that method removes the built-in variables of signals. Don’t worry though, I just found a solution that fixes the issue. Won’t be long, just tidying it up.

1 Like

Oh okay oops i didn’t test it i only tested it for my issue
Will wait for the new version🤞

1 Like

Sorry, took a little longer than planned, but this update should fix your issue while letting you keep the built-in parameters. Thanks for letting me know about the issue!

1 Like

Just tried, it works great! Thanks for fixing it, this module is so useful

1 Like

No problem, once again thanks for the feedback :grinning_face_with_smiling_eyes: