RetroBASIC

Retrogamecoding(.org) => EGSL => Topic started by: Osgeld on August 24, 2013, 08:10:43 PM

Title: oddity with tables
Post by: Osgeld 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

weird
Title: Re: oddity with tables
Post by: Tomaaz 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.
Title: Re: oddity with tables
Post by: Osgeld 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]
baddies[drawOrder[d][2]][1]