Question

Hy there! currently i'm working on a "simple" animation system for my game. And i have some problems with tables. I made a settings file, to load the informations of the animations (to be specific, it loads the alias of the animation, and the number of frames it contains) so, my settings file looks like this: (animsettings.lua)

animlist = {idle, run}

animlist.idle = { frames, alias }
animlist.idle.frames = 1
animlist.idle.alias = "idle"

animlist.run = { frames, alias }
animlist.run.frames = 5
animlist.run.alias = "run"

And i want to access each animation's properties (frames, and alias) with their indexes like this: animlist[1][1], and that should be the value of animlist.idle.frames, which is 1. So, am I misunderstanding something, or this thing should work? because, when I try to print animlist[1][1] or animlist[1].frames, it prints a "nil" but when i print animlist.idle.frames, it prints the actual value which is 1.

So it loads the file correctly, but i can't seem to use their indexes.

Edit: I forgot to show, the function which tries to access these tables: The run animation contains 5 frames (per direction, so 1-r.png 2-r.png etc.) This function's purpose is to load the animation files and to add a table to player.anims so it'll be like this: player.anims.run.left , which gets its name from variables from the function which uses the resources from the animsettings.lua

function initAnims()
    player.anims = {}
    getAnimSettings('gfx/players/'..player.modelname..'/animsettings.lua')

    for i = 1, #animlist do
        for j = 1, #animlist[i][1] do
                animlist[i][2] = {
                    left = ('gfx/players/'..player.modelname..'/'..animlist[i][2]..'/'..animlist[i]..j..'-l.png'),
                    right = ('gfx/players/'..player.modelname..'/'..animlist[i][2]'/'..animlist[i]..j..'-r.png')
                }
                table.insert(player.anims, animlist[i][2])
        end
    end
end

Edit2: now i realised that with this function every iteration i replace the actual frame with the next frame, so i'll make another table inside the that table that conatins each frame's data, but my question is still about the tables, and indexing, i think i'll be able to correct the function after that D:

Was it helpful?

Solution

animlist = {idle, run}

This line initializes animlist so that it will be an array a table that has the keys 1 and 2, and their corresponding values are the tables idle and run.

Now, when you write

animlist.idle = { frames, alias }

you try to access the member corresponding to the name (string) "idle" of animlist, which is not the member at key 1.

What you have to do is initialize the two sub-tables at keys 1 and 2 instead of using the names. First, remove the following line:

animlist.idle = { frames, alias }

This is superfluous, since you've already got a table at index 1. Then change the rest to

animlist[1].frames = 1
animlist[1].alias = "idle"

etc., do this with the second inner table as well.

OTHER TIPS

If you got a value inside a table as your main table value, It's also a good idea to add values to table after table initialization:

animlist = {}
animlist.run = {}
animlist.frames = 5
animlist.alias = "run"
print(animlist.run.alias)

if animlist[1][1] returns nil, then best way is to use this method:

print(animlist.run.frames)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top