I would like to call a function in a required module using a variable as its name. For example, in the required module I have a few functions:
function module:Ability_1(arg1,arg2)
--stuff
end
function module:Ability_2(arg1,arg2)
--more stuff
end
in the script requiring the module above, I would like to set a variable and then call the functions depending the variable set, like this:
local abilityModule = require(theModuleAbove)
local abilityToFire = "Ability_2"
function module:FireAbility(arg1,arg2)
abilityModule:[abilityToFire](arg1,arg2)
end
I donât think this code works, I tried a bunch of way to call a function inside a required module script but had no success. What I have had to do is place a function at the head of the called module which receives and argument of which ability to fire, then routes it properly form there. I was hoping ti simplify things so thatâs why I made this post.
I mainly use : instead of . for consistency, I do use OOP in some parts of my work but not all, and not in this portion of the script. Since they operate the same, its just a good habit to be in because if you slip up an place a . instead of : somewhere, its a harder bug to find.
Is there a way to do this while still using : and not . ?
Preferences can be wrong. Just because you like something doesnât mean that itâs not wrong. Preferring : over . when you arenât even using it correctly is indeed wrong. Misusing : misconstrues the meaning of your code.
Yes a method, and a method is a member function. Your module is more of a library, like you donât use math:random() or table:insert() or do you?
So no, you donât need : since you arenât using it for self or anything.
Looks like you wonât get an answer any time soon then, because there is no answer, unless you want your code to be misleading, but whatever works for you
It isnât impossible, as incapaz has said, you will just have to use the same code he provided, but youâll pass abilityModule as the first argument:
abilityModule[abilityToFire](abilityModule, ...)
The point heâs trying to bring across is that itâs unnecessary to use colon syntax when youâre not dealing with OOP