multiple function arguments

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

multiple function arguments

Post by PGUp »

so i wanna make a function to generate polygon with color, it looks like this

Code: Select all

function poly(... , color)
local edges = {...}
local color = color
end
even when i dont run the function, i got an error already, how can i achieve this ?
-
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: multiple function arguments

Post by bartbes »

The problem is that you can only have varargs as the last argument(s). So if you rewrite your function like this, it won't error:

Code: Select all

function poly(color, ...)
    local edges = {...}
end
(Note that color is already a local, as it is an argument, so 'local color = color' doesn't actually do anything useful.)
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: multiple function arguments

Post by PGUp »

bartbes wrote: Tue Nov 21, 2017 11:09 am The problem is that you can only have varargs as the last argument(s). So if you rewrite your function like this, it won't error:

Code: Select all

function poly(color, ...)
    local edges = {...}
end
(Note that color is already a local, as it is an argument, so 'local color = color' doesn't actually do anything useful.)
alright, but how do i check if the color is not nil ? using color ~= nil return an error
-
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: multiple function arguments

Post by Sir_Silver »

So you're saying you should be able to pass edges to the function while not passing a color, correct?

Is the "color" that you're passing a table that looks something like this? {255, 0, 0}. If that's what a "color" is then you could try this...

Code: Select all

function poly(color, ...)
    local args = {...}
    local edges = {}

    if type(color) ~= "table" then
        edges[1] = color
    end
    
    for i = 1, #args do
        table.insert(edges, args[i]) 
    end
end
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: multiple function arguments

Post by zorg »

I'd also be interested in where you got a error with color ~= nil, since that should work under most circumstances.

Code: Select all

local color =                          color or {255,255,255,255}
----
local color =      color  ~=  nil  and color or {255,255,255,255}
----
local color = type(color) ~= 'nil' and color or {255,255,255,255}
----
-- And the same 3 ways can be used with if-then-else, as well.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 40 guests