Reputation: 115
how can I use "for k, j in pairs() do" for 2 arrays in lua?
local blipmarker1 = {
{ x = 10 , y = 5, z = 3 },
{ x = 5, y = 5, z= 3}
}
local blipmarker2 = {
{ x = 100, y= 150, z=30 }
}
function createtext(){
local pos = GetEntityCoords(PlayerPedId(), true)
for k, j in pairs(blipmarker1,blimarker2) do
draw3DText(pos.x, pos.y, pos.z, j.x, j.y, j.z)
end
}
Upvotes: 1
Views: 486
Reputation: 10939
You could write your own stateful multipairs
iterator. Consult Chapter 9.3 “Coroutines as Iterators” of Programming in Lua for more details: https://www.lua.org/pil/9.3.html
local function multipairs(tables)
return coroutine.wrap(function()
for _, t in pairs(tables) do -- Maybe you want ipairs here
for k, v in pairs(t) do
coroutine.yield(k, v)
end
end
end)
end
local blipmarker1 = {
{ x = 10 , y = 5, z = 3 },
{ x = 5, y = 5, z= 3}
}
local blipmarker2 = {
{ x = 100, y= 150, z=30 }
}
for _, j in multipairs{blipmarker1, blipmarker2} do
print(j.x, j.y, j.z)
end
Upvotes: 0
Reputation: 1180
Function pairs() accepts only one argument of type table. You need a loop for each table:
for k,j in pairs(blipmarker1) do
...
end
for k,j in pairs(blipmarker2) do
...
end
Upvotes: 1