Reading Lua table from a file

Hi,

I have a problem and I don’t know if it’s related with Corona SDK or Lua itself so I’m asking here.

My problem is that when I try to read a Lua table from a file, I noticed that it is being read out of order like reading first of 5 entries and then reading the last one etc. goes on like this.

Is there a way to overcome this issue or do I need to figure out a workaround to the problem?

[lua]
function getData()
local levelData = {}

levelData.background = “images/levels/level1/background.jpg”

levelData.objects =
{
heartNecklace =
{
src = “images/levels/level1/necklace.png”,
x = 170,
y = 83,
},
playingCardBox =
{
src = “images/levels/level1/cardbox.png”,
x = 311,
y = 55,
},
tableTennisRacket =
{
src = “images/levels/level1/racket.png”,
x = 430,
y = 66,
},
statueOfLiberty =
{
src = “images/levels/level1/statue.png”,
x = 350,
y = 66,
},
}

return levelData
end
[/lua]

The read order is like this:
[lua]
heartNecklace
statueOfLiberty
tableTennisRacket
playingCardBox
[/lua] [import]uid: 154911 topic_id: 36282 reply_id: 336282[/import]

Are both of them [lua]levelData.objects[1][/lua] or is it supposed to be like [lua]levelData.objects[1][/lua] and [lua]levelData.objects[2][/lua]? [import]uid: 154911 topic_id: 36282 reply_id: 144192[/import]

I’m going to oversimply here but create your table like this:

levelData.objects = {}  
levelData.objects[1] = {name = "heartNeckLace", src = "filepath", x = 200, y = 500}  
levelData.objects[2] = {name = "playingCarBox", src = "filepath", x = 100, y = 80}  

And so on… Then when you want to read them out call them like this:

print(levelData.objects[1].name)  

good luck!
[import]uid: 70134 topic_id: 36282 reply_id: 144166[/import]

Good call! Yes, I edited my post, you’re supposed to use incremental numbering! [import]uid: 70134 topic_id: 36282 reply_id: 144193[/import]

Hmm I’m getting an error from the syntax. What may be the problem?

[lua]
levelData.objects = {}
levelData.objects =
{
levelData.objects[1] =
{
src = “images/levels/level1/necklace.png”,
x = 170,
y = 83,
},
levelData.objects[2] =
{
src = “images/levels/level1/cardbox.png”,
x = 311,
y = 55,
},
levelData.objects[3] =
{
src = “images/levels/level1/racket.png”,
x = 430,
y = 66,
},
levelData.objects[4] =
{
src = “images/levels/level1/statue.png”,
x = 350,
y = 66,
},

}
[/lua] [import]uid: 154911 topic_id: 36282 reply_id: 144196[/import]

Ah ok, I figured out my problem :slight_smile: Thank you. I’m trying this now [import]uid: 154911 topic_id: 36282 reply_id: 144197[/import]

Was just about to step in but I’m guessing you’ll be fine now! [import]uid: 70134 topic_id: 36282 reply_id: 144198[/import]

Lua tables that are indexed by number will have their order guaranteed, but those that are done using a text key can be in any order. [import]uid: 199310 topic_id: 36282 reply_id: 144215[/import]

Thank you for the help. It worked perfectly for me. [import]uid: 154911 topic_id: 36282 reply_id: 144306[/import]

You can keep your key naming convention by using a function like this ( which iterates through them in order ) : http://lua-users.org/wiki/SortedIteration [import]uid: 84637 topic_id: 36282 reply_id: 144330[/import]

Hmm… I’ll keep that in mind and in my archives Danny. Thank you for providing the method. I think I need to read more Lua documentation. [import]uid: 154911 topic_id: 36282 reply_id: 144336[/import]

Are both of them [lua]levelData.objects[1][/lua] or is it supposed to be like [lua]levelData.objects[1][/lua] and [lua]levelData.objects[2][/lua]? [import]uid: 154911 topic_id: 36282 reply_id: 144192[/import]

I’m going to oversimply here but create your table like this:

levelData.objects = {}  
levelData.objects[1] = {name = "heartNeckLace", src = "filepath", x = 200, y = 500}  
levelData.objects[2] = {name = "playingCarBox", src = "filepath", x = 100, y = 80}  

And so on… Then when you want to read them out call them like this:

print(levelData.objects[1].name)  

good luck!
[import]uid: 70134 topic_id: 36282 reply_id: 144166[/import]

Good call! Yes, I edited my post, you’re supposed to use incremental numbering! [import]uid: 70134 topic_id: 36282 reply_id: 144193[/import]

Hmm I’m getting an error from the syntax. What may be the problem?

[lua]
levelData.objects = {}
levelData.objects =
{
levelData.objects[1] =
{
src = “images/levels/level1/necklace.png”,
x = 170,
y = 83,
},
levelData.objects[2] =
{
src = “images/levels/level1/cardbox.png”,
x = 311,
y = 55,
},
levelData.objects[3] =
{
src = “images/levels/level1/racket.png”,
x = 430,
y = 66,
},
levelData.objects[4] =
{
src = “images/levels/level1/statue.png”,
x = 350,
y = 66,
},

}
[/lua] [import]uid: 154911 topic_id: 36282 reply_id: 144196[/import]

Ah ok, I figured out my problem :slight_smile: Thank you. I’m trying this now [import]uid: 154911 topic_id: 36282 reply_id: 144197[/import]

Was just about to step in but I’m guessing you’ll be fine now! [import]uid: 70134 topic_id: 36282 reply_id: 144198[/import]

Lua tables that are indexed by number will have their order guaranteed, but those that are done using a text key can be in any order. [import]uid: 199310 topic_id: 36282 reply_id: 144215[/import]

Thank you for the help. It worked perfectly for me. [import]uid: 154911 topic_id: 36282 reply_id: 144306[/import]

You can keep your key naming convention by using a function like this ( which iterates through them in order ) : http://lua-users.org/wiki/SortedIteration [import]uid: 84637 topic_id: 36282 reply_id: 144330[/import]