lua-users home
lua-l archive

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


I think this answers the question you're asking:

The C++ function you make available to Lua must be a static member function.
I really should have mentioned this. In Lua, I call the function using the :
operation, so it sticks a copy of the object on the stack. From that I grab
the ID, and that allows me to get at the C++ object.

Gary


On 17/6/02 17:08, "Robert Sadedin" <serenity@nosubstancesoftware.com> wrote:

> Hmmm.  Interesting, and cool.  How do you store a reference to the methods
> in a class?
> I can store a pointer to an object no sweat, but I don't know how to call a
> method relating to that object without adding to lua to handle pointers to a
> specific class method.
> 
> ----- Original Message -----
> From: "Gary Makin" <garym@ssg.com.au>
> To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
> Sent: Monday, June 17, 2002 1:33 PM
> Subject: Re: C++ creation of object, and Lua co-calling
> 
> 
>> 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
>> store).
>> 
>> 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
>> other.
>> 
>> 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 fro
> m
>> 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" <serenity@nosubstancesoftware.com>
> 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
>>> 
>> 
>