Author Topic: oddity with tables  (Read 1467 times)


  • Guest
oddity with tables
« on: August 24, 2013, 08:10:43 pm »
for example, this
Code: [Select]
player =
walkL   = loadimage("walk.png"),
kickL   = loadimage("kick.png"),
punchL  = loadimage("punch.png"),
walkR   = fliphorizontal(player.walkL),
kickR   = fliphorizontal(player.kickL),
punchR  = fliphorizontal(player.punchL),

fails with
game.lua:16: attempt to index global 'player' (a nil value)

but this

Code: [Select]
player ={}
player.walkL   = loadimage("walk.png")
player.kickL   = loadimage("kick.png")
player.punchL  = loadimage("punch.png")
player.walkR   = fliphorizontal(player.walkL)
player.kickR   = fliphorizontal(player.kickL)
player.punchR  = fliphorizontal(player.punchL)

works fine



  • Guest
Re: oddity with tables
« Reply #1 on: August 24, 2013, 09:13:12 pm »
In the first example you're trying to access a value of a player table before the table is created. It looks like player.walkL is not ready when you're trying to call it to set player.walkR, probably because they are part of a single statement. In the second example each line is a separate statement, so player.walkL is ready to use when you're trying to set player.walkR.


  • Guest
Re: oddity with tables
« Reply #2 on: August 25, 2013, 03:21:57 am »
yep your probably right

tables can be a double sided sword, one hand they help organize, on the other hand you get stuff like this floating around your code

Code: [Select]