My question is how (or if) you can insert two values into a lua table.
I got a function that returns (a variable number of values)
function a(x, y)
return x, y
end
and another function that inserts that points into a table,
function b(x, y)
table.insert(myTable, x, y)
end
So how can i make, that i can call function b with a variable number of arguments and insert them all into my table?
If the last parameter for your function is ...
(called a vararg function), the Lua interpreter will place any extra arguments into ...
. You can convert it to a table using {...}
and copy the keys/values into your global table named myTable
. Here's what your function would look like:
function b(...)
for k, v in pairs({...}) do
myTable[k] = v
end
end
b(1, 2) -- {[1] = 1, [2] = 2} is added to myTable
You should tweak the function depending on whether you want to replace, merge or append elements into myTable
.