problem with for statement and tables

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Mr Billy
Posts: 10
Joined: Wed Oct 26, 2005 10:13 am

problem with for statement and tables

Post by Mr Billy »

i have two tables, one holding my images(tiles) and one holding the layout of the tiles. i am using a for statement so i dont have many lines of code. when i use this code, i get an "attempt to index field '?' (a nil value)" error on the blit line. if i replace the y in layout[y] for a number, it seems to work. why doesnt it work with a variable?

Code: Select all

for y=1,16 do
     screen:blit(0, ((y*32)-32), images[layout[y].block1])
end
thanks
"There is no spoone"
-The Matricks
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Your Code

Code: Select all

for y=1,16 do 
     screen:blit(0, ((key*32)-32), images[layout[y].block1]) 
end
Try this its simpler and will never give you any issues.
This is to make shure the table Images has what your looking for.

Code: Select all

for key,value in layout do
     if (images[value.block1]) then
          screen:blit(0, ((key*32)-32), images[value.block1]) 
     end
end
The code Checks if the value exists before trying to print it.
So you dont have errors like that if the data is nothing. Also it goes through the entire loop so you can set it with another statement.
If you just want it to do up to number 16 (that is if you numbered it)

Code: Select all

     if &#40;images&#91;value.block1&#93;&#41; and &#40;key <= 16&#41; then
          screen&#58;blit&#40;0, &#40;&#40;key*32&#41;-32&#41;, images&#91;value.block1&#93;&#41; 
     end
*Edited for typo' in key (Within code)
Post Reply