Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple values from functions in lua

Tags:

lua

I am experimenting with the following lua code:

function test() return 1, 2 end
function test2() return test() end
function test3() return test(), 3 end

print(test()) -- prints 1 2
print(test2()) -- prints 1 2
print(test3()) -- prints 1 3

I would like test3 to return 1, 2, 3

What is the best way to achieve this?

like image 840
Kara Avatar asked Oct 09 '12 19:10

Kara


1 Answers

you could do something like this if you aren't sure how many values some function may return.

function test() return 1, 2 end
function test2() return test() end
function test3()
    local values = {test2()}
    table.insert(values, 3)
    return unpack(values)
end


print(test3())

this outputs:

1   2   3
like image 188
Mike Corcoran Avatar answered Sep 20 '22 19:09

Mike Corcoran