lua-users home
lua-l archive

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


On Mon, Jan 22, 2001 at 11:29:09PM +0200, Mikko Kallinen wrote:
> Hello,
> 
> I'm planning to use Lua to provide extensibility for a rich C++ class
> hierarchy in a performance-sensitive application. What I would like to do is
> to provide some way for the user to inherit any of the classes in the
> hierarchy and implement its virtual functions in Lua code. The fact that any
> virtual function might be implement in Lua should preferably not be visible
> to the C++ side.

As far as hiding your Lua implementation from the C++ side, I needed just that in my game. Some objects are scripted in Lua while some are not. The project was never completed but you can still find it on sourceforge, The Berzerker Throne.

My game objects inherited from a generic game object, where the do_tick() method was virtual. The scripted game object's version of do_tick would call into Lua,calling the Lua implementation of do_tick.

void ScriptGameObject::do_tick(float time_elapsed)
{
        lua_Object o = lua_getref(lua_obj_ref); // ref to the object in lua
        lua_pushobject(o);
        lua_pushnumber(time_elapsed);
        lua_pushobject(o);
        lua_pushstring("do_tick");
        lua_callfunction(lua_gettable());
}

Below is my object list where I call do_tick() on all the objects.

void ClientWorld::run_objects(float time_elapsed)
{
        
        ObjectList::iterator it = objects.begin();
        while (it != objects.end())
        {
             (*it)->do_tick(time_elapsed);
             it++;
        }
}

> Any ideas how to do this kind of thing efficiently? My current plan is to
> have objects represented with tables on the Lua side, and have them contain
> a pointer to the C++ instance as userdata. Some tag methods would probably
> need to be specified to provide delegation of indexing from the table to the
> C++ instance, etc...
> 
> --
> Mikko Kallinen
> 
>