Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C函数到Lua函数的转换_C++_Lua - Fatal编程技术网

C++ C函数到Lua函数的转换

C++ C函数到Lua函数的转换,c++,lua,C++,Lua,假设我有一个回调函数,当指定的玩家死亡时执行 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

假设我有一个回调函数,当指定的玩家死亡时执行

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堆栈上,因为这个func已经存在。
我只想让它成为已经存在的Lua函数。

如果您想从C API在Lua中运行它,您需要以这样或那样的方式将它推到堆栈上。如果它已经存在于全局表中的某个位置,则可以通过调用lua_getglobal来推送它。lua_调用(lua_pcall)要求在进行调用之前调用函数,并且其参数存在于堆栈顶部


如果您喜欢,可以检查LuaJIT ffi回调功能,但它不是普通的Lua。

解决了。这是可能的

解决方案:从C调用loadstring