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?