I have a question, is it possible to do this and is it a good thing to do?
it’s something absurd.
Yes, technically that would work. However, it adds useless layers of complexity that makes your code much harder to read.
Instead of storing a reference to a module script that holds a function that returns a string, why not just directly store the string in the first place?
I don’t think that’s necessary. A function returning a string can be simply converted to a variable holding a string.
EDIT: Additionally, return function() return "X" end
is essentially return "X"
, but you don’t need to call a function.
It’s a reference to get what I was trying to mean but, Im making the states into their own objects
I think we should take a look into it through a code refactoring website, but at the moment I agree with @Master_Aaron it looks complicated.
I think you should also take a look at the promise library they also do a lot of absurd things I found initially including returning functions yet solves a problem dealing with complex asynchronous operations.
I’ve done similar stuff by directly defining my functions inside of a dictionary. It can be a very useful paradigm for things such as chat commands, or generally cases where you want different strings to be associated with different, complex (or very different from case to case), actions
Your implementation seems to be more complicated than it needs to be though
For example
local Commands = {}
Commands.tp = function(Player)
-- Teleport the player...
end
Commands.help = function(Player)
-- Show an help prompt
end
--
local Module = {}
function Module:ProcessCommand(Player, String)
local func = Commands[string.lower(String)] -- Here you'd probably want to separate arguments after the command, for a more complex command system
if func then func(Player) end
end
return Module
When I use this parading, I also tend to write the function as an explicit key value pair, but the following does the same. I would avoid the :
notation here because of the implicit self
that is no longer implicit when calling a function variable
function Commands.tp(Player)
-- Teleport the player...
end
I think im not ready to use Promises yet, but ill give a try