lua-users home
lua-l archive

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


On Fri, Jan 28, 2000 at 03:49:03PM -0200, Flavio Spolidoro Ferreira Gomes wrote:
> I am trying to register a C++ object method to be called from the
> lua enviroment but I am getting the following warning:

Lua only calls back into void functions. So it either has to be a
global function (not an object method) or a static method (which has
no "this" reference),

You can setup the callback so that lua passes in the C++ object
pointer as the first lua paramater, and then it'll work the way you
expect, but it looks a little weird at first.

> lua_register(LuaState, "ev_trap", this->EngineExec);

That looks fine, but you need to make sure your class/method looks
like this:

class LuaEngine {
  static void EngineExec(void);
};

static void LuaEngine::EngineExec(void) {
}

Then, you can use one of several methods to call into your object
method. I shove the C++ object pointer into Lua as a piece of
userdata, and then explicitly call into the C++ method with that
userdata.

static void LuaEngine::EngineExec(void) {
  LuaEngine *this;
  lua_Object object = lua_getparam(1);

  if (!lua_isuserdata(object)) {
    lua_error("incorrect argument 1 to function C_obj_followsprite()
  } 
  this = (LuaEngine *)lua_getuserdata(object);
}


-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net