Checking if an entity is in a table.

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
Psyk
Prole
Posts: 8
Joined: Thu Sep 11, 2014 10:40 pm

Checking if an entity is in a table.

Post by Psyk »

Code: Select all

ent = {}
ent.create("apple", 0, 50, 50, "consumable", 150, 50, 50)
function ent.create( name, id, x, y, type, r, g, b )
	table.insert(ent, {name=name, id = id + 1, x=x, y=y, type=type, r=r, g=g, b=b})
end

function ent.getID( ent_name )
	for k,v in pairs(ent) do
		if( v == ent_name ) then
		for i = 1, #ent do
			return ent[i].id
		end
		else
			print(" Error:  " .. ent_name .. " is not a valid entity.")
		end
	end
	return false
end
I am getting the error that it was not found in the table, but it is in the table.What am I doing wrong?
User avatar
hardcrawler
Prole
Posts: 12
Joined: Fri Jun 27, 2014 11:31 pm

Re: Checking if an entity is in a table.

Post by hardcrawler »

This code is "working too hard" to find the entity. You are not taking advantage of the benefit provided by tables' keys. (i.e. you are using integer keys and trying to iterate through to find your named entity)

Here is my cleanup of your code to make it work, without straying too much from the pattern you are attempting:

Code: Select all

ent = {}
function ent.create( name, id, x, y, type, r, g, b )
   ent[name] = {name=name, id = id + 1, x=x, y=y, type=type, r=r, g=g, b=b}
end


ent.create("apple", 0, 50, 50, "consumable", 150, 50, 50)

function ent.getID( ent_name )
        if not ent[ent_name] then
                print(" Error:  " .. ent_name .. " is not a valid entity.")
                return false
        end
        return ent[ent_name].id
end

print( tostring(ent.getID("apple")) )
Edit:

To answer your question, the reason your code wasn't working is that table.insert results in *integer indexed* tables, and your search logic was trying to find your entity making the (incorrect) assumption that you had stored it with your "name" as a key.

Edit 2:

Actually, after looking at it some more, it looks like your pairs loop is actually correct, but then you're looping through the "v" table using the # operator (but that table is actually k,v pairs -- not indexed). At any rate, your lookup code had a really weird structure that is hard on my brain. That's why I simplified it :)
User avatar
Ortimh
Citizen
Posts: 90
Joined: Tue Sep 09, 2014 5:07 am
Location: Indonesia

Re: Checking if an entity is in a table.

Post by Ortimh »

Code: Select all

function ent.getID(name)
	for index, entity in pairs(ent) do
		if (entity.name == name) then
			return entity.id
		end
	end
end
Without using hard-coded code, just check if current entity name in for loop is the name that wanted to find, if it is then return the ID.
Psyk
Prole
Posts: 8
Joined: Thu Sep 11, 2014 10:40 pm

Re: Checking if an entity is in a table.

Post by Psyk »

Thanks for the help! What you guys said made sense. I updated the code a bit and it works.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests