Handling a variety of functions

Hello, in my game I have an interact system that works well. When you hover your mouse over an interactable object and press the interact key, a menu of possible actions appears:

There can be any number of possible actions based on the object, and each object type has its own actions (vehicles have those shown, machines have their own set, etc). Because of the sheer number of possible actions between every object and their actions, I’m wondering what the best structure of handling them all is.

Currently, my interact module looks like this to handle them:

local InteractTypeFunctions = {
	["Vehicle"] = {
		{InteractFunctions.Drive, "Drive"},
		{InteractFunctions.ViewInventory, "View Inventory"},
		{InteractFunctions.Delete, "Delete"}
	}
}

function InteractController:InteractWith(optionNumber, specialFunc)
	if not specialFunc then
		
		local functionsTable = InteractTypeFunctions[self.CurrentInteractType]
		local accessedFunction
	
		if optionNumber then
			accessedFunction = functionsTable[optionNumber][1]
		end
	
		accessedFunction(self.CurrentInteractingModel)
	
		self:TogglePrompt()
		
	else
		
		local part = self.CurrentlyHovering
		if part then
			local model = part.Parent
			InteractFunctions:FindFunction(model, true)
		end
		
	end
end

Then, I have another module as a child of the interact module that actually defines all of the functions:

local InteractFunctions = {}

InteractFunctions.Drive = function(currentModel)
	local remote = repStor.Remotes.SitPlayer
	local seat = currentModel:FindFirstChildOfClass("VehicleSeat")
	if not seat then warn("seat not found") return end
	remote:FireServer(seat)
end

InteractFunctions.Delete = function(currentModel)
	local remote = repStor.Remotes.DeleteVehicle
	remote:InvokeServer(currentModel)
end

InteractFunctions.ViewInventory = function()
	--view inventory lol
end

This structure works and all, but I personally don’t think it’s the most elegant way of handling all of the functions. As more things are added to my game, that interact functions module will just be filled up. One possible solution I can think of is to modularize as much as I can; that way the interact functions module won’t be doing most of the work but modules for each specific thing will.

Either way, a lot of functions will be defined in that one module, some short and some long, and it will look ugly. Thoughts on how to handle this?

I think this is already an decent way of organizing your code. All the functions for handling interactions are in just one module script, and as the script gets larger, you could use CTRL + F and type in InteractFunctions.FunctionName to get to the function you need to look at. After all, your InteractTypeFunctions table is already an index, making it fairly easy for you to look up the function in the other module.

Alternatively, you could break up the module script and create a separate module script for each object type, such as one for “Vehicle,” one for “Machine,” and so on. Then, you could define functions for each object type. This is kinda the same as the previous method, except now you have an index in the Explorer that looks something like this:

whatamidoing

Either organization would work, but I guess it depends on whether you prefer to have an Explorer filled with module scripts or just two scripts: one containing a table indexing all your functions, and the other containing all your interaction functions.

I don’t fully know what I’m talking about so idk if it totally makes sense lul. Oh well, hope this kinda helps

1 Like

I guess there truly is no way around a little bit a clutter with this many functions. After all, every function has to be defined somewhere. Thanks for the advice, I’ll keep looking.

1 Like