lua-users home
lua-l archive

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

I think the solution depends on just what you want to do. For my project,
I've decided to forego using tolua, and to do it all manually.

I've got a bunch of different C++ classes, and objects of those classes, and
I wanted to have parallel objects available in Lua. I ended up putting a
routine in each class to create the Lua object for each C++ object.

The code creates a new Lua table, and gets a reference to it (telling Lua
not to garbage collect the table). The reference is stored in the C++
object, and I use that whenever C++ needs to access Lua.

In the Lua table I've added a read-only ID entry that I use to find the C++
object (I'm using STL, so pointers and iterators aren't always safe to

For each method, or data item, that I want to have accessible from Lua, I've
added a table entry for the C++ routine. Of course, these C++ routines need
different parameters than a C++ only routine would need, so I've tended to
end up with two versions of a lot of the routines, with one calling the

I've also added some inheritance, so I'm making a single table that's the
parent for all the tables of one class. For example, I've got one master
Ruin table that is the parent for all the Ruin tables that represent real
Ruins in the game. I've added all the common methods to the parent Ruin, so
I don't need to have copies of all the functions in all the ruins, or copies
of pointers for C++ functions (as I'm using Lua functions to be called from
Lua whenever possible).

As a caveat, I've only got this working in the last week, so I don't have a
fully working game using this, but it seems like the system will hold up.

Gary Makin
Lead Programmer
Strategic Studies Group

On 17/6/02 6:55, "Robert Sadedin" <> wrote:

> For my game scripting, I need to be able to expose methods from objects
> created in my c++ class to Lua.
> I know there are a number of solutions for providing c++ classes in lua so
> long as they are created in Lua, but what about when the construction is in
> the host application?
> Essentially, I need method sharing with the creation process being
> consumated by the c++ app.
> What is the simplest, and least ambitious method for doing this that you
> experts can think of?
> Thanks,
> Rob