hi, I was working at a module to make guns easier but I want developers to can create they own functions, but always it returns a table. The module script (a part):
function GunData:CreateNewFunction(name,func)
self[name] = func
end
how I use it:
local Shoot = Gun:CreateNewFunction('Shoot',function(input)
print(input)
end)
Gun:Shoot('Thing')
i try to make a function that u can create Custom Functions to a Gun, but like in the example, the parameters (i don’t know why) transforms into random tables, any help?
Your issue is that calling functions using colon syntax, table:func(), passes the original table to the function as the first argument.* If defined using colon syntax, function table:func() ... end, then the implicit self variable exists as you use it.
Edit: *that’s why adding the _ at the beginning fixes it here, obviously. Something like this is a bit of a code smell, whatever you’re trying to do might have a more sensible means of accomplishing the same task
Yes, you could wrap it in another function that throws away the first parameter:
local function noSelf(f)
return function(self, ...)
return f(...)
end
end
local Shoot = Gun:CreateNewFunction('Shoot', noSelf(function(...)
print(...)
end))
Gun:Shoot('foo', 'bar')
The call to ‘noSelf’ can be included in your CreateNewFunction method so you don’t need to think about it at all.