lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
I have a similar problem, instead of a Vector a have a matrix of strings that I have in C that I want to send to Lua for manipulation in there. I'm aware that I can do it with metatables but I just don't know how. Imagine I have the following function:

int LArena::getMatrix(lua_State *L)
{   
    luaL_checktype(L, 1, LUA_TSTRING);
    const char *name = luaL_checkstring(L,1);
   
    try
    {
        std::string** matrix = ArenaContainer::getArenaContainerPtr()->getMatrix(name);
        //now I want to send the matrix to Lua. How????
    } catch(...)
    {
        luaL_error(L,"object class not found");
        return 0;
    }
    return 0;
}
Can you help what to return or how to construct the metatable so the matrix colud be manipulated in Lua?
Just to check I have a similar function that return a list of objects. It seems to be working but I don't know if it's the best way to do it:

int LArena::getObjectsByClass(lua_State *L)
{   
    luaL_checktype(L, 1, LUA_TSTRING);
    const char *classe = luaL_checkstring(L,1);
   
    try
    {
        ObjectKey classId = "">        vector<Object*> objs = ArenaContainer::getArenaContainerPtr()->getObjectsByClass(classId);
        int size = objs.size();
        Object *tmp;
        ObjectKey key;
        char *buf;
       
        for (int i=0; i<size; i++)
        {
            tmp = objs.at(i);
           
            buf = (char *)lua_newuserdata(L,sizeof(ObjectKey));   
            key = tmp->getID();
            memcpy(buf,(char*)&key,sizeof(ObjectKey));
            luaL_getmetatable(L,"Zara.object ");
            lua_setmetatable(L, -2);
        }
       
        return size;
    } catch(...)
    {
        luaL_error(L,"object class not found");
        return 0;
    }
    return 0;
}

Thanks,
José Pedro Tavares