K Kay
K Kay

Reputation: 109

can someone explain line multiple local variables

1 function getCoordinates()
2   return 12, 55, 123
3 end

4 local x, y, z = getCoordinates()
5 print(x, y, z)  

output:
12  55  123

what does line 4 do? if I replace it with

local x= getCoordinates()
local y= getCoordinates()
local z= getCoordinates()

I get 12 nil 12

evenif I change the print statement to

print(x)
print(y)
print(z)

still get

12 nil 12

Upvotes: 0

Views: 51

Answers (1)

luther
luther

Reputation: 5554

The function getCoordinates returns 3 values. Your local x, y, z declaration unpacks those values to 3 new variables.

When you assign getCoordinates() to a single variable, the last two values get silently dropped, so all 3 variables get the value 12. (I don't know how you can be getting nil for y.)

Upvotes: 3

Related Questions