lua-users home
lua-l archive

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


> Hello, I'm using Lua for a small game and I'm looking
> for a better way of implementing something.
> In my program, I start in C++, and then launch a Lua
> script which takes control of the flow. The script can
> then make calls to C++ functions to do various
> specialized things for my game like print text to the
> screen or draw certain things. All these functions
> were declared static because that was the only way I
> could get Lua to call a C++ function.

You mean you want Lua to call a method of a object? If it is just a C++
function, Lua should have no problem calling it regardless of whether it
is a static or an extern function.

Methods are a little trickier. The way I like doing is to declare a
simple function for each method. Each function is associated with the
object instance in some way. Here there are several options: receive it
as a parameter, retrieve it from an upvalue, take it from a global
variable, get it from the registry etc. When the function is called from
Lua,  it retrieves the pointer to the object and call the appropriate
method.  You can even make it so that you actually "object:method()" in
our Lua script.

toLua does this binding automatically for you.