How do i define multiple variables at once?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
TopDole
Prole
Posts: 1
Joined: Fri Nov 14, 2014 11:07 pm

How do i define multiple variables at once?

Post by TopDole »

I read most of the lua book a while back and have been coding for quite a while but i havent found out how to define multiple variables yet. Does anyone know? Is it even possible?

A couple things I tried that I wish worked:

P1.x, P2.x, P3.x, P4.x = 0
P1.x = P2.x = P3.x = P4.x = 0
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: How do i define multiple variables at once?

Post by Wojak »

Code: Select all

P1.x, P2.x, P3.x, P4.x = 0,0,0,0
http://www.lua.org/pil/4.1.html
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How do i define multiple variables at once?

Post by zorg »

You can define multiple vars as how Wojak wrote, but you cannot assign one thing to multiple variables at once.
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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How do i define multiple variables at once?

Post by s-ol »

zorg wrote:You can define multiple vars as how Wojak wrote, but you cannot assign one thing to multiple variables at once.
You can write a helper though:

Code: Select all

function all( val, num ) 
 local r = {} 
 for i=1,(num or 10) do
  r[i] = val
 end
 return unpack(r)
end

-- use
local a, b, c, d, e = all( 0 ) -- for up to 10 vars
local a, b, C = all( 0, 3 ) 
Last edited by s-ol on Thu Dec 04, 2014 12:17 pm, edited 1 time in total.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: How do i define multiple variables at once?

Post by Muris »

I know this is bit of a derailing thing, but one of the best things I've found about lua is as following:

Code: Select all

 x, y = y, x 

The swapping is just so brilliant compared to other languages where you usually end up writing a helper function to do it.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How do i define multiple variables at once?

Post by zorg »

S0lll0s wrote: code-snip
Don't forget to

Code: Select all

return unpack(r)
in that helper function though, else anything you define with that will be nil :3
Also our postcounts were in sync, and will be again if you reply :D
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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How do i define multiple variables at once?

Post by s-ol »

-*Accidental double submit -
Last edited by s-ol on Thu Dec 04, 2014 12:16 pm, edited 1 time in total.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: How do i define multiple variables at once?

Post by s-ol »

zorg wrote:
S0lll0s wrote: code-snip
Don't forget to

Code: Select all

return unpack(r)
in that helper function though, else anything you define with that will be nil :3
Also our postcounts were in sync, and will be again if you reply :D
Whoops, That's what I get for coding on mobile...
yaaaaay :ultrahappy:

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests