Table search?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 21
- Joined: Fri Nov 02, 2012 10:23 pm
Table search?
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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Need some help
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:
If you don't know the key, you can parse the table and check the values one by one, like this:
And you can use the function like this:
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:
You can use "something" as the key, like this:
And now knowing wether something is inside t is very simple:
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.
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
Code: Select all
function contains(t, something)
for _,v in pairs(t) do
if v == something then return true end
end
return false
end
Code: Select all
if contains(t, something) then
-- t contains something
else
-- t does not contain something
end
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
Code: Select all
t[something] = true -- adding something to t, as a key
Code: Select all
if t[something] then
-- something is inside of t
else
-- something is not inside of t
end
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.
Re: Need some help
Please, say the topic of your help request in the title (help others help you).TheConfuZzledDude wrote:This may be a nooby question, but how do you check if something is part of a certain table?
Denis
... la vita e estrany ...
-
- Prole
- Posts: 21
- Joined: Fri Nov 02, 2012 10:23 pm
Re: Need some help
Sorry about that, changed the topic name.spir wrote:Please, say the topic of your help request in the title (help others help you).TheConfuZzledDude wrote:This may be a nooby question, but how do you check if something is part of a certain table?
Denis
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?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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Need some help
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.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?
When I write def I mean function.
-
- Prole
- Posts: 21
- Joined: Fri Nov 02, 2012 10:23 pm
Re: Need some help
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?kikito wrote: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.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?
- Attachments
-
- Defamed.love
- (94.19 KiB) Downloaded 75 times
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Need some help
You literally do this: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?
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.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 229 guests