iDevGames Forums
Finding an object by name - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Finding an object by name (/thread-8765.html)



Finding an object by name - markhula - Mar 29, 2011 07:49 AM

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


RE: Finding an object by name - OneSadCookie - Mar 29, 2011 07:59 AM

Since you're using lua anyway, use a lua table as a hashtable.


RE: Finding an object by name - markhula - Mar 29, 2011 08:08 AM

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