forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

problem with for statement and tables

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
Mr Billy



Joined: 26 Oct 2005
Posts: 10

PostPosted: Fri Dec 23, 2005 10:40 am    Post subject: problem with for statement and tables Reply with quote

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:
for y=1,16 do
     screen:blit(0, ((y*32)-32), images[layout[y].block1])
end


thanks
_________________
"There is no spoone"
-The Matricks
Back to top
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Sat Dec 24, 2005 4:25 pm    Post subject: Reply with quote

Your Code
Code:

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:

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:

     if (images[value.block1]) and (key <= 16) then
          screen:blit(0, ((key*32)-32), images[value.block1])
     end


*Edited for typo' in key (Within code)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group