c++lualua-c++-connection

Lua c++ lib sethook: Gives error with hook function arg


Been googling for a while, have no idea whats happening. So I have a class called luaScript which handles, lua scripts ( Surprising! I know. ), which in it's current state, could not exit or pause scripts currently being executed.

I found out about hooks, which allow me to run code every time something happens. Also found a cool stackoverflow question from 2009 showing how to use them.

Copied code, got hook done, tried to compile, and... nothing. Nothing but an error saying that the function I passed ( void hookRoutine(lua_State *L, lua_Debug *ar) ) was incompatible with the type lua_Hook ( Which I find total bs because lua_Hook is defined as typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); which as far as I know is exactly what I gave it. )

This is the code I copied: lua_sethook(mL, hookRoutine, LUA_MASKCOUNT, 0); mL being the lua instance and hookRoutine being the hook.

Things I have tried so far:

lua_Hook hookRoutine
lua_sethook(mL, &hookRoutine, LUA_MASKCOUNT, 0);
lua_sethook(mL, lua_Hook(hookRoutine), LUA_MASKCOUNT, 0); 

I am completely stumped on this and have been googling for hours, can someone please explain what kind of stupid error I made?


Solution

  • My guess is that hookRoutine is a regular member function of your class. It has to be a static member function instead because Lua does not know anything about C++ and the this pointer. You need to find some other way to pass the this pointer to your hook function in case you need it (probably by storing it in the Lua state, in the registry).