Extending a C++ class hierarchy

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Extending a C++ class hierarchy

Mikko Kallinen
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.

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



Reply | Threaded
Open this post in threaded view
|

Re: Extending a C++ class hierarchy

Lenny Palozzi-4
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
> 
> 

Reply | Threaded
Open this post in threaded view
|

Re: Extending a C++ class hierarchy

Luiz Henrique de Figueiredo
In reply to this post by Mikko Kallinen
Isn't this what tolua does?
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Extending a C++ class hierarchy

Christian Vogler-2
On Mon, Jan 22, 2001 at 07:38:24PM -0200, Luiz Henrique de Figueiredo wrote:
> Isn't this what tolua does?

No. What this boils down to is that he has to call lua code
transparently from the C++ program. tolua solves the opposite problem.

So, how about a program called toc++? :-)

Actually, IIRC, if you chec the list archives, someone already has
written a framework for this.

- Christian