Jump to content

One name, two+ functions


VortDyn

Recommended Posts

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

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

 

  • Like 1
  • Thanks 1
Link to comment
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

 

  • Like 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...