Is it possible to run a function with the : operator from a string?

I’m gonna keep this post simple, I want to be able to run a function automatically parsing self without having to mess with all of my code only to make a few exceptions.
What I’m trying to do:

module[funcname]() but with the : operator instead.
module:[funcname]() something like this

If there is a way to do this (that doesn’t involve loadstring since this is for the client), let me know.
Otherwise I’ll have to make a bunch of secondary functions to just run a function whilst parsing self, really not a pretty workaround.

1 Like

Modules are tables of functions, therefore if you reference the function name in the module.

luaModule[funcname]()

should work!

I am trying to run it with the : operator, which automatically parses self. This is vital, because otherwise I will be forced to add self as the first parameter of all of my code, which will require a bunch of reworking.

Can you explain what your system you want to make is? This could be a flaw in your system’s design. I’m not sure what the parsing is for.

It is an OOP class. I need it to run via a string because I have two duplicate oop classes running between the server and the client, except both have code unique to them.
image
like so.
That way, if I run the :Destroy() command on the server, I can destroy, remove my entity from a table and also have the client run the removal command on it’s own entity.

Edit: I would rather avoid having 5 million different remotes for each individual function and having to program each of them in manually, so I am instead trying to find the function the server is trying to run on your client via a string.

a:b(...) is syntactic sugar for a.b(a, ...) where a is defined as self if fired both ways. So to achieve your desired result:

module[funcname](module)

That is precisely what I’m trying to avoid. The order for the parameters will get moved around without the : operator.

I also want to avoid having 2 functions hard coded in for a single function, one to interpret self and parse it to the non : function, and one which just does it directly.

You don’t need to define two different functions to achieve the result of one. As I’ve mentioned before, a:b(...) is syntactic sugar, or the equivalent, for a.b(a, ...), so a.b(a, ...) calls your function like if it was defined only using colon syntax:

local t = {}
local name = "hello"

function t:hello(...)
    print("hello", ...)
end

local args = {"hi", "bonjour", "hola"}

-- for comparison
t:hello( unpack(args) ) 
t[name](t, unpack(args) )

I have tested and I can confirm it works. I will do some testing as to the relation of : and the first parameter.

Edit: Your function being declared with a : makes it so that if it is ran using . rather than :, it will interpret the first parameter as Self. Running it with yet another : simply parses the table itself by default as the first parameter.

Thanks for the help!

…Time to rework my server sided commands yet again :upside_down_face: