如何创建和调用lua函数,即在多个文件中,从c++
how to create and call lua function, that is in multiple files, from c++
我需要创建类似onSomeEvent
函数的东西,这将在每个Lua插件文件中。我有这个:
void onSomeEvent(int someParameter) {
lua_getglobal(L, "onSomeEvent");
lua_pushnumber(L, someParameter);
lua_pcall(L, 1, 0, 0);
lua_pop(L, 1);
}
,但它只适用于最后一个Lua文件。但是,它只调用最近加载的Lua文件中的最后一个onSomeEvent
函数。我想从每个加载的Lua文件中调用onSomeEvent
函数…
你有什么好主意吗?
对不起,我的英语不好。
onSomeEvent
显然是全局的。每个定义它的脚本都会覆盖之前的定义。您可以为每个脚本提供自己的环境或Lua状态,但是让脚本告诉您它们的事件处理程序在哪里,而不是按照约定强制它在特定的位置(例如作为一个名为onSomeEvent的全局),这会更简洁。
您的C程序只需要公开一个函数,脚本可以使用它来注册事件处理程序。下面是一个示例(注意缺少错误处理或边界检查,这只是为了说明目的):
#include "lua.h"
#include "lauxlib.h"
// list of registered handlers
static int handlers[20];
// number that are current registered
int numHandlers = 0;
// allow scripts to register a new handler
static int addEventHandler (lua_State* L) {
if (!lua_isfunction(L,1)) {
luaL_argerror(L, 1, "expected function");
}
lua_pushvalue(L, -1);
handlers[numHandlers++] = luaL_ref(L, LUA_REGISTRYINDEX);
return 0;
}
// call the registered handlers
static void onEvent (lua_State* L) {
int i;
for (i = 0; i < numHandlers; ++i) {
lua_rawgeti(L, LUA_REGISTRYINDEX, handlers[i]);
lua_pcall(L, 0, 0, 0);
}
}
int main()
{
lua_State* L = lua_open();
luaL_openlibs(L);
// expose function that lets scripts register a callback handler
lua_register(L, "addEventHandler", addEventHandler);
// run test script
luaL_dofile(L, "handlertest.lua");
// call any callback(s) registered by the test script
onEvent(L);
lua_close(L);
return 0;
}
handlertest.lua
addEventHandler(function() print("anonymous function called") end)
local function localFunc() print("local function called") end
addEventHandler(localFunc)
function globalFunction() print("global function called") end
addEventHandler(globalFunction)
输出anonymous function called
local function called
global function called
如果您的脚本都定义了一个名为onSomeEvent
的全局函数,那么您需要在加载脚本时为它们提供不同的环境。当需要处理事件时,运行脚本环境列表并调用每个脚本的事件处理程序。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到