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
How do i define multiple variables at once?
Re: How do i define multiple variables at once?
Code: Select all
P1.x, P2.x, P3.x, P4.x = 0,0,0,0
- 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?
You can define multiple vars as how Wojak wrote, but you cannot assign one thing to multiple variables at once.
Me and my stuff True 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.
Re: How do i define multiple variables at once?
You can write a helper though:zorg wrote:You can define multiple vars as how Wojak wrote, but you cannot assign one thing to multiple variables at once.
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.
Re: How do i define multiple variables at once?
I know this is bit of a derailing thing, but one of the best things I've found about lua is as following:
The swapping is just so brilliant compared to other languages where you usually end up writing a helper function to do it.
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.
- 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?
Don't forget toS0lll0s wrote: code-snip
Code: Select all
return unpack(r)
Also our postcounts were in sync, and will be again if you reply
Me and my stuff True 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.
Re: How do i define multiple variables at once?
-*Accidental double submit -
Last edited by s-ol on Thu Dec 04, 2014 12:16 pm, edited 1 time in total.
Re: How do i define multiple variables at once?
Whoops, That's what I get for coding on mobile...zorg wrote:Don't forget toS0lll0s wrote: code-snipin that helper function though, else anything you define with that will be nilCode: Select all
return unpack(r)
Also our postcounts were in sync, and will be again if you reply
yaaaaay
Who is online
Users browsing this forum: Ahrefs [Bot] and 6 guests