Grabbing data using pairs (help)

3 replies [Last post]
sirnameless
User offline. Last seen 46 weeks 2 days ago. Offline
Joined: 29 Oct 2012

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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

And here's where I call it (player.lua):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

When I print "#self.spriteFactory", I get...

1
2
3
4
5
6
7
8
9
10
11
12
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

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?

Replies

nevin
User offline. Last seen 1 year 19 weeks ago. Offline
Joined: 30 Sep 2011

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:

1
2
3
                                local newFactory = loqsprite.newFactory(img)
                                newFactory.startup = true
                                table.insert(self.spriteFactory, newFactory)

Screaming Leaf
User offline. Last seen 1 year 49 weeks ago. Offline
Joined: 1 Oct 2011

I think you only get six because of the loop:

1
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:

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

The "#" returns the length of the table.

sirnameless
User offline. Last seen 46 weeks 2 days ago. Offline
Joined: 29 Oct 2012

Thank you for the quick responses! Both suggestions worked well. I'll master the Lua language yet. :)

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.