Log In  


I'm trying to put together a simple game in which the player must avoid obstacles. I'm trying to put together a table that holds all the obstacles, with their x/y positions, as tables within tables, so that the table structure looks like this:

ActorsTable
*Wall1
 1.Wall1's X
 2.Wall1's Y
*Wall2
 1.Wall2's X
 2.Wall2's Y

However, when I want to get--for instance--ActorsTable.Wall1.X, I get an error:

"ATTEMPT TO INDEX FIELD '?' (A NIL VALUE)".

I know that this means something's not initialized. But what exactly hasn't been initialized?

For reference, the code so far. I'm working on making a series of walls for the player to fly past. Error occurs at line 86.
http://pastebin.com/4k7DBv2X



When you use:

for i in all(w) do
  -- code
end

"i" is not an index, but the object in the collection itself.
Therefore, you can access its properties like this:

i["x"] 
i["h"]

or

i.x  
i.y 

(and you save some valuable characters)


you can also use

foreach(actorstable, checkcollision)

function checkcollision(i)
--i can be any object you want to check for a collision
end

then you can keep your walls as one set of tables and other types of objects as another.



Gerard_02 ::

When you use:

for i in all(w) do
  -- code
end

"i" is not an index, but the object in the collection itself.


Well, that makes the whole thing a lot clearer. No wonder I was getting errors, I was trying to use the content of the table as an index to access the table!


akilism ::

you can also use

foreach(actorstable, checkcollision)

function checkcollision(i)
--i can be any object you want to check for a collision 
end

then you can keep your walls as one set of tables and other types of objects as another.

That could shave some code off my _update and _draw functions. Definitely gonna try it.



[Please log in to post a comment]