Grabbing data using pairs (help)

It seems like my code is overwriting itself, so even though I should end up with 12 items, I end up with 6. What am I doing wrong?

Here’s the data (player_assets.lua):
[lua]
local PlayerAssets = {
default = “default”,
layers = {“body”, “head”, “legs”, “sword”, “shoulder”, “glove”},
modes = {
default = {
glove = “art.player.1_Glove”,
shoulder = “art.player.2_Shoulder”,
sword = “art.player.3_Sword”,
legs = “art.player.4_Legs”,
head = “art.player.5_Head”,
body = “art.player.6_Body”,
},
pain = {
head = “art.player.5_Head_Pain”,
},
fire = {
glove = “art.player.1_Fire_Glove”,
shoulder = “art.player.2_Fire_Shoulder”,
legs = “art.player.5_Fire_Legs”,
head = “art.player.6_Fire_Head”,
body = “art.player.7_Fire_Torso”,
},
},
}
return PlayerAssets
[/lua]

And here’s where I call it (player.lua):
[lua]
function bhvr.general.init(self)
local loqsprite = require (“loq_sprite”)
self.assets = require(“player_assets”)

local img = nil;
self.spriteFactory = {}
local mode = self.assets.modes

for mode,assets in pairs(self.assets.modes) do
for i=1, #self.assets.layers do
local part = self.assets.layers[i]
if (assets[part]) then
img = bhvr.general.loadPlayerImg(assets[part])
self.spriteFactory[i] = loqsprite.newFactory(img)
self.spriteFactory[i].startup = true
–print("img: ", img)
print(#self.spriteFactory)
end
–print("i: ", i)
end
end
end

function bhvr.general.loadPlayerImg(file)
local img = file
return img
end

[/lua]

When I print “#self.spriteFactory”, I get…
[lua]
2013-03-11 17:35:11.183 Corona Simulator[9554:f03] 1
2013-03-11 17:35:11.185 Corona Simulator[9554:f03] 2
2013-03-11 17:35:11.187 Corona Simulator[9554:f03] 3
2013-03-11 17:35:11.188 Corona Simulator[9554:f03] 3
2013-03-11 17:35:11.189 Corona Simulator[9554:f03] 3
2013-03-11 17:35:11.191 Corona Simulator[9554:f03] 3
2013-03-11 17:35:11.193 Corona Simulator[9554:f03] 3
2013-03-11 17:35:11.195 Corona Simulator[9554:f03] 3
2013-03-11 17:35:11.196 Corona Simulator[9554:f03] 6
2013-03-11 17:35:11.198 Corona Simulator[9554:f03] 6
2013-03-11 17:35:11.199 Corona Simulator[9554:f03] 6
2013-03-11 17:35:11.201 Corona Simulator[9554:f03] 6
[/lua]

If I uncomment the prints above, “i” will count up to 12, and “img” will correctly show all of the data. What did I miss? [import]uid: 191140 topic_id: 37526 reply_id: 67526[/import]

The first thing that jumps out at me is that you’re not guaranteeing that the sprite factory table will have no holes (although it shouldn’t in your particular case).

Perhaps more importantly, you’re overwriting previously written slots in the table; it will only ever have six slots in it, at most. You probably wan want way to collect your factories that isn’t based solely on numerical indexes.

As a quick fix, use table.insert instead of straight assignment, if the rest of your code can find things in the resulting table:

[lua]
local newFactory = loqsprite.newFactory(img)
newFactory.startup = true
table.insert(self.spriteFactory, newFactory)
[/lua] [import]uid: 94737 topic_id: 37526 reply_id: 145694[/import]

I think you only get six because of the loop:

for i=1, #self.assets.layers do  

only has 6 elements.

and when you write to self.spriteFactory you are using “i”. Which means the highest that number will reach is 6.

so try what the previous poster suggested with table.insert() or the following, which does the same exact thing:

self.spriteFactory[#self.spriteFactory+1] = loqsprite.newFactory(img)  
self.spriteFactory[#self.spriteFactory].startup = true  

The “#” returns the length of the table. [import]uid: 94868 topic_id: 37526 reply_id: 145738[/import]

Thank you for the quick responses! Both suggestions worked well. I’ll master the Lua language yet. :slight_smile: [import]uid: 191140 topic_id: 37526 reply_id: 145776[/import]

The first thing that jumps out at me is that you’re not guaranteeing that the sprite factory table will have no holes (although it shouldn’t in your particular case).

Perhaps more importantly, you’re overwriting previously written slots in the table; it will only ever have six slots in it, at most. You probably wan want way to collect your factories that isn’t based solely on numerical indexes.

As a quick fix, use table.insert instead of straight assignment, if the rest of your code can find things in the resulting table:

[lua]
local newFactory = loqsprite.newFactory(img)
newFactory.startup = true
table.insert(self.spriteFactory, newFactory)
[/lua] [import]uid: 94737 topic_id: 37526 reply_id: 145694[/import]

I think you only get six because of the loop:

for i=1, #self.assets.layers do  

only has 6 elements.

and when you write to self.spriteFactory you are using “i”. Which means the highest that number will reach is 6.

so try what the previous poster suggested with table.insert() or the following, which does the same exact thing:

self.spriteFactory[#self.spriteFactory+1] = loqsprite.newFactory(img)  
self.spriteFactory[#self.spriteFactory].startup = true  

The “#” returns the length of the table. [import]uid: 94868 topic_id: 37526 reply_id: 145738[/import]

Thank you for the quick responses! Both suggestions worked well. I’ll master the Lua language yet. :slight_smile: [import]uid: 191140 topic_id: 37526 reply_id: 145776[/import]