简体   繁体   English

Lua C ++ lib sethook:使用挂钩函数arg给出错误

[英]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. 因此,我有一个名为luaScript的类,该类处理lua脚本(令人惊讶!我知道。),它在当前状态下无法退出或暂停当前正在执行的脚本。

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. 还发现了2009年一个很酷的stackoverflow问题,展示了如何使用它们。

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. ) 唯一的错误是我传递的函数( void hookRoutine(lua_State *L, lua_Debug *ar) )与lua_Hook类型不兼容(我发现总数为bs,因为lua_Hook被定义为typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);据我所知,这正是我给的。)

This is the code I copied: lua_sethook(mL, hookRoutine, LUA_MASKCOUNT, 0); 这是我复制的代码: lua_sethook(mL, hookRoutine, LUA_MASKCOUNT, 0); mL being the lua instance and hookRoutine being the hook. mL是lua实例, hookRoutine是钩子。

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? 我对此完全感到困惑,并且已经在谷歌上搜索了几个小时,请问有人可以解释我犯了什么样的愚蠢错误?

My guess is that hookRoutine is a regular member function of your class. 我的猜测是hookRoutine是您的类的常规成员函数。 It has to be a static member function instead because Lua does not know anything about C++ and the this pointer. 它必须是静态成员函数,因为Lua对C ++和this指针一无所知。 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). 如果需要,您需要找到其他方法this指针传递给钩子函数(可能通过将其存储在注册表的Lua状态中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM