Table search?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
TheConfuZzledDude
Prole
Posts: 21
Joined: Fri Nov 02, 2012 10:23 pm

Table search?

Post by TheConfuZzledDude »

This may be a nooby question, but how do you check if something is part of a certain table?
Last edited by TheConfuZzledDude on Tue Nov 13, 2012 10:00 pm, edited 1 time in total.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Need some help

Post by kikito »

It depends on two things: 1. How things are "be part of the table" (there are several ways) and 2. What you know.

Everything inserted in a table can be retrieved using its key. So if you know the key, you can do this:

Code: Select all

if t[keyOfSomething] == nil then
  -- something is not inside of t
else
  -- something is inside of t
end
If you don't know the key, you can parse the table and check the values one by one, like this:

Code: Select all

function contains(t, something)
  for _,v in pairs(t) do
    if v == something then return true end
  end
  return false
end
And you can use the function like this:

Code: Select all

if contains(t, something) then
  -- t contains something
else
  -- t does not contain something
end
This option is kindof slow, but always works. You can do optimizations in the for loop (i.e. using a numerical index instead of pairs, if you know that t is an array-like table), but if you are going to be calling contains very often on a table you might as well use the third option.

The third option relies on one very special property of tables - the keys of a table can be anything, not just numbers or strings. So when inserting stuff on the table, instead of using an array-like table, like this:

Code: Select all

t[#t + 1] = something -- adding something to t, as in an array
You can use "something" as the key, like this:

Code: Select all

t[something] = true -- adding something to t, as a key
And now knowing wether something is inside t is very simple:

Code: Select all

if t[something] then
  -- something is inside of t
else
  -- something is not inside of t
end
Notice that you don't need to know the key of something here - because something is the key already.

This kind of table is faster for "checking up if stuff exists", but it's a bit slower for "parsing all the elements", since you can't use a numeric for, as you can in array-like tables. So it's a bit of a tradeoff. If you are going to be looking up stuff all the time in a table, use the third option. If you are going to be parsing all the elements in the table (i.e. to draw all the enemies on the screen, you must parse them all), use an array.
When I write def I mean function.
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Need some help

Post by spir »

TheConfuZzledDude wrote:This may be a nooby question, but how do you check if something is part of a certain table?
Please, say the topic of your help request in the title (help others help you).

Denis
... la vita e estrany ...
TheConfuZzledDude
Prole
Posts: 21
Joined: Fri Nov 02, 2012 10:23 pm

Re: Need some help

Post by TheConfuZzledDude »

spir wrote:
TheConfuZzledDude wrote:This may be a nooby question, but how do you check if something is part of a certain table?
Please, say the topic of your help request in the title (help others help you).

Denis
Sorry about that, changed the topic name. :)
kikito wrote:It depends on two things: 1. How things are "be part of the table" (there are several ways) and 2. What you know.

Everything inserted in a table can be retrieved using its key. So if you know the key, you can do this:

This kind of table is faster for "checking up if stuff exists", but it's a bit slower for "parsing all the elements", since you can't use a numeric for, as you can in array-like tables. So it's a bit of a tradeoff. If you are going to be looking up stuff all the time in a table, use the third option. If you are going to be parsing all the elements in the table (i.e. to draw all the enemies on the screen, you must parse them all), use an array.
OK, I think I get that. So if I had a table where I loaded all of a certain type of objects, and I wanted to check if the object my player was colliding with was in one of these tables, and this was continuous, which method would I use?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Need some help

Post by kikito »

TheConfuZzledDude wrote:OK, I think I get that. So if I had a table where I loaded all of a certain type of objects, and I wanted to check if the object my player was colliding with was in one of these tables, and this was continuous, which method would I use?
If they are only ever going to be "checked against the player", then an array type will do. If you think you will need to check the same collisions for other things (like bullets, platforms, etc) then use option 3.
When I write def I mean function.
TheConfuZzledDude
Prole
Posts: 21
Joined: Fri Nov 02, 2012 10:23 pm

Re: Need some help

Post by TheConfuZzledDude »

kikito wrote:
TheConfuZzledDude wrote:OK, I think I get that. So if I had a table where I loaded all of a certain type of objects, and I wanted to check if the object my player was colliding with was in one of these tables, and this was continuous, which method would I use?
If they are only ever going to be "checked against the player", then an array type will do. If you think you will need to check the same collisions for other things (like bullets, platforms, etc) then use option 3.
OK, so I can now check if a certain object is inside that table, but how do I actually check if the object the player is colliding with is in the table?
Attachments
Defamed.love
(94.19 KiB) Downloaded 75 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Need some help

Post by kikito »

TheConfuZzledDude wrote: OK, so I can now check if a certain object is inside that table, but how do I actually check if the object the player is colliding with is in the table?
You literally do this:

Code: Select all

if theTable[theObject] then
  -- the table contains the object
else
  -- the table does not contain the object
end
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 229 guests