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

P#10071 2015-04-20 23:09 ( Edited 2015-04-21 13:29)

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)

P#10072 2015-04-20 23:44 ( Edited 2015-04-21 03:44)

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.

P#10073 2015-04-21 00:15 ( Edited 2015-04-21 04:15)


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.

P#10079 2015-04-21 09:29 ( Edited 2015-04-21 13:29)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 01:44:01 | 0.006s | Q:12