VortDyn Posted August 1, 2021 Share Posted August 1, 2021 Is it possible to create two (or more) functions with the same name, but different number of arguments? Personally, it didn't work for me, maybe there are some hidden stones (Perhaps this is due to the fact that I did this for exports)? Something like this: functionOutput (text) end function Output (text, player) end function Output (text, player, colorR, colorG, colorB) end Link to comment
Moderators Patrick Posted August 2, 2021 Moderators Share Posted August 2, 2021 Hi, it's not possible because one overwrite the other. You have to handle this inside one function. function Output(...) -- "magic parameter" for all local args = {...} -- put them into a table local length = #args if length == 1 then -- 1 parameter passed to function local text = args[1] elseif length == 2 then -- 2 parameter passed to function local text, player = args[1], args[2] elseif length == 5 then -- 5 parameter passed to function local text, player, colorR, colorG, colorB = unpack(args) -- or you can use unpack instead of indexing table one by one end end 1 1 Link to comment
Simple0x47 Posted August 2, 2021 Share Posted August 2, 2021 16 hours ago, VortDyn said: Is it possible to create two (or more) functions with the same name, but different number of arguments? Personally, it didn't work for me, maybe there are some hidden stones (Perhaps this is due to the fact that I did this for exports)? Something like this: functionOutput (text) end function Output (text, player) end function Output (text, player, colorR, colorG, colorB) end That's overloading, mate. No such thing here on Lua, mate. But you could simulate that behavior as @Patrickputs it, and then proceed to redirect the arguments to a specific range of functions. local function OutputText(text) -- code end local function OutputTextToPlayer(text, player) -- code end local function OutputTextToPlayerWithColor(text, player, r, g, b) -- code end -- Patrick's suggestion. function Output(...) -- "magic parameter" for all local args = {...} -- put them into a table local length = #args if length == 1 then -- 1 parameter passed to function local text = args[1] OutputText(text) elseif length == 2 then -- 2 parameter passed to function local text, player = args[1], args[2] OutputTextToPlayer(text, player) elseif length == 5 then -- 5 parameter passed to function local text, player, colorR, colorG, colorB = unpack(args) -- or you can use unpack instead of indexing table one by one OutputTextToPlayerWithColor(text, player, colorR, colorG, colorB) end end 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now