Finding an object by name

Member
Posts: 117
Joined: 2010.09
Post: #1
Hi all,

I'm trying to think of an efficient method in straight C that basically achieves this:

Code:
int find_object(lua_State *L)
{
    const char *find=lua_tostring(L,1);
    OBJECT_DEF *obj;
    
    obj = objects_head;
    
    while( obj != NULL)
    {
        if (!strcmp(obj->name,find))
        {
            lua_pushlightuserdata(L, obj);
            return 1;
        }
        
        obj=obj->next;
    }    
    
    lua_pushnumber(L,0);
    return 0;
}

As you can see I have to go through an entire loop to 'find' an object.
Though ok performance wise now, will become an issue later :-)
I *guess* i really want a method where I don't iterate over all objects each frame, and also be able to deal with 'killed' objects (i.e. name removal).
Any suggestions?;

Cheers
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Since you're using lua anyway, use a lua table as a hashtable.
Quote this message in a reply
Member
Posts: 117
Joined: 2010.09
Post: #3
Won't that be slow though? (how do I do that anyway???).
I assumed some method where I call a 'c' func would be better.

But out of interest how would I achieve this in Lua?

thanks
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  finding a training center - Heeeelp Glauter 1 2,914 Dec 26, 2011 12:30 PM
Last Post: zenkimoto
  Kind of a noob question - finding artwork within my .app?? Nethfel 2 2,773 Mar 8, 2010 08:00 AM
Last Post: AndyKorth
  Finding the height of a point on a given list of vertexes? mikey 10 5,258 Jan 4, 2010 05:36 AM
Last Post: mikey
  Finding the closest point to (x1,y1) in array of [x1,y1,z1,x2... mikey 17 9,511 Aug 28, 2009 08:12 AM
Last Post: mikey
  Finding Outer Vertices of Shapes Nick 18 8,484 Nov 27, 2006 07:01 AM
Last Post: Nick