3

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:

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
neonoxd
  • 57
  • 7

2 Answers2

3
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.

  • oh wow, then I totally misunderstood how tables work, then how should I edit the tables? – neonoxd Oct 28 '13 at 16:48
  • @neonoxd I've just explained that... Use indices always. Read the [documentation](http://www.lua.org/pil/3.6.html) if you're lost -- the behavior of table constructors is clearly written down there. –  Oct 28 '13 at 16:48
  • sorry i commented before you edited your comment, so i didn't see the last part, now i understand :) – neonoxd Oct 28 '13 at 16:49
  • @neonoxd Cool, [still, here's a li'l demo](http://ideone.com/Tm0ylP) so that you can be absolutely sure. Also, consider using more whitespace (especially around operators) in your code in order it to be easier to read. –  Oct 28 '13 at 16:51
  • thank you, that little demo is super effective, now its totally clear :D And thanks for your advice i'll keep that in mind :D – neonoxd Oct 28 '13 at 16:53
0

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)
111WARLOCK111
  • 857
  • 2
  • 7
  • 14
  • edited the question, so now i include the function itself too, so you'll see why i need to access them by their indexes. – neonoxd Oct 28 '13 at 16:47