Tables...aways tables.....

Sorry about my english.

I can’t find my error.

Im improving my game and I use themes to change graphics.

I store the graphics names like this:

[lua]enemy_list = {“2players.png”,“player-ia1.png”,“player-ia2.png”}[/lua]

So, when I will use this graphics, I just use something like that:

[lua]enemy_pic = display.newImageRect(“image/”…theme…"/"…enemy_list[player_num], 320, 150)[/lua]

where THEME is the image directory, and enemy_list is the table above.

My problem is: When I print these values BEFORE call display.newImageRect I have this values:

player_num 1
enemy_list[1] 2players.png
enemy_list[2] player-ia1.png
enemy_list[3] player-ia2.png

BUT, enemy_list[player_num] (using the player_num variable)aways becomes nil

What Im doing wrong?

Thanks for any help! [import]uid: 9133 topic_id: 15678 reply_id: 315678[/import]

Perhaps player_num is exceeding the array? Does player_num ever become greater than 3? [import]uid: 84637 topic_id: 15678 reply_id: 57861[/import]

Maybe enemy_list is not visible when you call display.newImageRect()

Make sure the table is in scope, maybe try declaring it right before

enemy\_pic = display.newImageRect("image/"..theme.."/"..enemy\_list[player\_num], 320, 150)

Thanks for your help.

No, player_num NEVER is bigger than 3. And the table and the variables is in the same scope. Thats why I dont understand the error…

Im using director class, but all is inside the same function and same scope.

The values I wrote is in the code. If I print (player_num) always have a number. If I print enemy_list[3] (or other number), I aways have the right value. But if I use togheter, I receive nil.

Strange… [import]uid: 9133 topic_id: 15678 reply_id: 57878[/import]

How are you assigning a value to player_num?

Could it be that player_num is “3” (as a string) instead of 3 (a number)?

You could try something like this:

enemy_pic = display.newImageRect(“image/”…theme…"/"…enemy_list[tonumber ( player_num )], 320, 150)

You can check the type of player_num using type():

print( type( player_num ) )

make sure it is assigned a number

hope it helps

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 15678 reply_id: 57892[/import]

Raúl,

Thanks!!

That was EXACTLY the problem. When I ran the program the first time, everything worked, so I was confused. The variable had a number in it.

When closing the program, this value is written to a file and is read by the program and of course, was loaded as a text. I was not able to see the error.

Thanks for the help.
[import]uid: 9133 topic_id: 15678 reply_id: 57897[/import]

Glad I could help :slight_smile:

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 15678 reply_id: 57900[/import]