将C函数转换为Lua函数
Converting C function to Lua function
假设我有一个回调函数,当指定的玩家死亡时执行。
function OnPlayerDeath(playerid)
end
我希望这个函数在Lua C模块中调用,而不把它放在Lua脚本中:
static int l_OnPlayerConnect (lua_State * L) {
enum { lc_nformalargs = 1 };
lua_settop(L,1);
// so here I can use playerid argument - 1 arg
return 0;
}
是否有可能在C中接收这个回调参数?
#define LUA extern "C" __declspec(dllexport) int __cdecl
LUA luaopen_mymodule(lua_State *L)
{
/* function OnPlayerConnect( playerid )
*
* end */
lua_pushcfunction(L,l_OnPlayerConnect);
lua_setfield(L,LUA_GLOBALSINDEX,"OnPlayerConnect"); //there's already OnPlayerConnect I just want to also call it here but I don't know how.
assert(lua_gettop(L) - lc_nextra == 0);
return 1;
}
我不想把这个函数压入lua堆栈,因为这个函数已经存在了。我只是希望它是已经存在的Lua函数
如果你想在Lua中从C API运行它,你需要以一种或另一种方式将它推入堆栈。如果它已经存在于全局表中的某个位置,则可以通过调用lua_getglobal来推送它。Lua_call (lua_pcall)要求被调用的函数及其参数在调用前存在于堆栈顶部。
如果你喜欢,你可以检查LuaJIT的ffi回调功能,但它不是普通的Lua。
解决。这是可能的W.B.
解决方案:从c调用loadstring
相关文章:
- 使用 LuaBridge 或 Lua 将对象传递给 Lua 函数C++
- 将 Lua 函数块转换为 C 字符串
- 调用 lua 函数的地址为 C/C++?
- 如何使用重新定义的打印函数打印Lua表?
- 如何在C++中获取lua函数作为参数,然后调用它
- C++-在没有自定义.lib文件的情况下从Lua C模块调用Lua函数
- 将带有字段的表作为参数从C++传递给 Lua 函数?
- Lua 中看起来像表的函数是什么?
- 如何在C++中从Lua函数获取返回表?
- 从Qt/C++-App并行运行两个Lua函数
- Lua C API 自定义打印函数,在字符串中传递空格时不调用
- C++ 和 Lua 函数之间的交互与 3D 矢量参数
- 如何获取从C++发送到Lua函数的表的更新值?
- 包装C++函数以使用 SWIG 获取 Lua 字符串表
- 如何在 lua cpp 模块中调用托管 c++ dll 函数
- 在C 函数中打印一个来自LUA的静态变量
- 从 LUA 脚本调用类函数C++
- 如何在LUA C API中分配全局LUA函数变量
- 如何安全地从 Lua 调用C++函数
- 尝试执行绕道附加,但无法将我的lua函数的数据类型转换为LPVOID