Get X, Y coordinates from a nested Lua table

I need to get values ​​from a nested table in Lua, I just can’t figure out how to do this, I tried a lot of online examples, but nobody works.

Any help would be appreciated

Table

xy = { a={x=0,y=0},b={x=0,y=100}, c={x=0,y=200}}

if unpack (route) contains a and c, how can I get the x, y values ​​from the above table.

I tried

for _, v in pairs(xy) do
  print(v[1], v[2])
end

But all that I will return is nil

+4
source share
2 answers

You do not use arrays, so neither unpacking nor numerical indexing will help you. You can use syntactic sugar instead:

print(xy.a.x, xy.a.y)

If you want to iterate over all of them:

for i,v in pairs(xy) do
    print(i..": "v.x, v.y)
end
+3
source

pairs key, value, value - x y; :

print(v.x, v.y)

v[1] v[2] , x y, nil .

: t.index1.index2 .. , t[1][2], : , t.

+5

Source: https://habr.com/ru/post/1629740/


All Articles