Page 1 of 1

### [Solved] get table in-between {}

Posted: Fri Jan 22, 2021 10:21 pm
So, i kinda new to lua and i encountered with problem:

Code: Select all

``````a = {a, b}
a.b = {
{
c = 1
}
}

print(a.b.c) --> nil instead of 1``````
so there exist way to get tables like this without deleting this brackets?:

Code: Select all

``````{
c = 1
}``````

### Re: get table in-between {}

Posted: Fri Jan 22, 2021 10:31 pm
With a = {a, b}, you're actually doing this: a = {[1] = a, [2] = b}, and at that time, both a and b are either nil, or whatever you set those, if you did.

The second issue is you doubling up the brackets, so again, you'll get this:
a.b = {[1] = {['c'] = 1}}

which can be printed with a.b[1].c instead... lose the extra brackets and it'll work:
a = {}
a.b = {
c = 1
}

You can also do it with the opening bracket on its own line, lua shouldn't complain:
a = {}
a.b =
{
c = 1
}

### Re: get table in-between {}

Posted: Sat Jan 23, 2021 12:45 am
zorg wrote: Fri Jan 22, 2021 10:31 pm With a = {a, b}, you're actually doing this: a = {[1] = a, [2] = b}, and at that time, both a and b are either nil, or whatever you set those, if you did.

The second issue is you doubling up the brackets, so again, you'll get this:
a.b = {[1] = {['c'] = 1}}

which can be printed with a.b[1].c instead... lose the extra brackets and it'll work:
a = {}
a.b = {
c = 1
}

You can also do it with the opening bracket on its own line, lua shouldn't complain:
a = {}
a.b =
{
c = 1
}
Well, a.b[1].c it exactly what I need, thank you
(but it kinda inconvenient)

Main problem with it I experience in Tiled
When it creat .lua file, it put some tables like

Code: Select all

``````tileset = {
{
<Data>
}
}``````
with double brackets instead of one
And it sucks

Manually deleting it can be painful, especially when you create/modify maps often

### Re: get table in-between {}

Posted: Sat Jan 23, 2021 9:47 am
GVovkiv wrote: Sat Jan 23, 2021 12:45 am Main problem with it I experience in Tiled
When it creat .lua file, it put some tables like

Code: Select all

``````tileset = {
{
<Data>
}
}``````
with double brackets instead of one
And it sucks
It's for a reason - Tiled has to have a way to denote multiple tilesets. Your quick snippet actually says "tileset" instead of "tilesets", so I guess that's where the confusion came from. If you only use one tileset, then it's absolutely correct to just take the first element from the table using [1]. I guess that for some use cases it would be better if Tiled exported it like:

Code: Select all

``````tilesets = {
ground = { <Data> }
trees = { <Data> }
}
``````
but I guess they didn't put that much thought into it. Or maybe I'm wrong and it's more important for them to have tilesets ordered and be able to use ipairs() instead of pairs() to iterate over.

### Re: get table in-between {}

Posted: Sat Jan 23, 2021 12:48 pm
ohai wrote: Sat Jan 23, 2021 9:47 am
GVovkiv wrote: Sat Jan 23, 2021 12:45 am Main problem with it I experience in Tiled
When it creat .lua file, it put some tables like

Code: Select all

``````tileset = {
{
<Data>
}
}``````
with double brackets instead of one
And it sucks
It's for a reason - Tiled has to have a way to denote multiple tilesets. Your quick snippet actually says "tileset" instead of "tilesets", so I guess that's where the confusion came from. If you only use one tileset, then it's absolutely correct to just take the first element from the table using [1]. I guess that for some use cases it would be better if Tiled exported it like:

Code: Select all

``````tilesets = {
ground = { <Data> }
trees = { <Data> }
}
``````
but I guess they didn't put that much thought into it. Or maybe I'm wrong and it's more important for them to have tilesets ordered and be able to use ipairs() instead of pairs() to iterate over.
("tileset" just for example)

Well, thanks for explaining, but i found out it myself

for example, in tiled i can create 2 layer with different objects on it
in exported file it will looks something like

Code: Select all

``````layers = {
{ -- first layer
*layer 1 objects*
}

{ -- 2nd layer
*layer 2 objects*
}
}
``````
which, i believe, can be fetch with something like:

Code: Select all

``````l = 1 -- layer index, i guess
print(layers[l])
``````
where "l" can be "l = l + 1" when game done loading layer 1 or sometning like that

### Re: [Solved] get table in-between {}

Posted: Sat Jan 23, 2021 7:49 pm
GVovkiv wrote: Fri Jan 22, 2021 10:21 pm So, i kinda new to lua and i encountered with problem:

Code: Select all

``````a = {a, b}
a.b = {
{
c = 1
}
}

print(a.b.c) --> nil instead of 1``````
so there exist way to get tables like this without deleting this brackets?:

Code: Select all

``````{
c = 1
}``````

Code: Select all

``print(a.b[1].c)``

### Re: [Solved] get table in-between {}

Posted: Sat Jan 23, 2021 7:56 pm
darkfrei wrote: Sat Jan 23, 2021 7:49 pm
GVovkiv wrote: Fri Jan 22, 2021 10:21 pm So, i kinda new to lua and i encountered with problem:

Code: Select all

``````a = {a, b}
a.b = {
{
c = 1
}
}

print(a.b.c) --> nil instead of 1``````
so there exist way to get tables like this without deleting this brackets?:

Code: Select all

``````{
c = 1
}``````

Code: Select all

``print(a.b[1].c)``