calling multiple things in functions

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
User avatar
skyHights
Citizen
Posts: 78
Joined: Mon Aug 25, 2014 12:14 pm

calling multiple things in functions

Post by skyHights »

Adding to my long list of questions (you can probably guess what I'm doing by looking at them :P)

Anyway, to draw a polygon you just keep adding more and more points:

Code: Select all

love.graphics.polygon( mode, ... )
How would you do that with a function, and is it possible to do that with two different variables, i.e.

Code: Select all

function a(b, c)
      -- do whatever
end

a(b, c, d, e, f, g, h, i)
Kinda like that?

And then how would you handle them?


(also, how would one go about see their previous topics created?)
Learning to Löve.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: calling multiple things in functions

Post by Nixola »

You can define a "vararg" (that's their name, if I recall correctly) function like this:

Code: Select all

function a(b, c, ...)
  local args = {...} --not required, you could use the select function instead of a table
  --do whatever
end
The three dots inside the function will be a list of the arguments "past" b and c; so if you called a(1, 2, 6, "foo", nil, "bar") the second line will behave as if it was `local args = {6, "foo", nil, "bar"}`.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: calling multiple things in functions

Post by ivan »

The easiest way would be:

Code: Select all

function a(...)
  local t = { ... }
  -- t[1] = first parameter, t[2] = second parameter, etc
end
how would one go about see their previous topics created
Click on "Posts: 50" under your avatar.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: calling multiple things in functions

Post by Positive07 »

Try this too!

Code: Select all

a = function (...)
   for i=1, select("#", ...) do
      local var = select(i, ...)
      print(var)
   end
end

a("whatever", 1, {}, function () end, 3239, 0xff, "0x00", a)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

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