How to call a function in a module using a variable as the name?

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.

Anyone know how to do this?

Thanks it advance!

If your code has no notion of objects then don’t use :. Remember that a.b is syntax sugar for a["b"]. And a:b() is sugar for a.b(a).

So you would just do.

abilityModule[abilityToFire](arg1, arg2)

without the :.

Since you aren’t using oop then there is no need for a : therefore I didn’t pass abilityModule as the first argument.

5 Likes

you have to return something because require on a module script waits for a return, its mostly a table, so to use this, change it to:

local module = {}

function module:Ability_1(arg1,arg2)
  --stuff
end

function module:Ability_2(arg1,arg2)
  --more stuff
end

return module

you have to return the table at the end after the functions

Edit: just realized that the module wasn’t for the code, lol, im stupid

1 Like

i know how to use a module, i left the table out for simplicity :slight_smile:

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 . ?

No. There is no ‘consistency’ issues here. If you aren’t using oop then don’t use :. You aren’t using self anywhere, so you don’t need :.

Think of your module as just a library of utility functions or a singleton depending on how you use it.

1 Like

I gotcha, I dont “need” it but whether it is used or not that way is a matter of opinion.

: in your code signifies a method, not whether or not your using OOP.

Again, i guess my question remains, is there way to achieve the same thing wihtout switching to . instead of :

thanks!

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.

1 Like

thanks, my question still remains unanswered though your posts are very helpful.

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

1 Like

so your saying its not possible with : ?

its only possible with . ?

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

3 Likes