• <i id='zx6UU'><tr id='zx6UU'><dt id='zx6UU'><q id='zx6UU'><span id='zx6UU'><b id='zx6UU'><form id='zx6UU'><ins id='zx6UU'></ins><ul id='zx6UU'></ul><sub id='zx6UU'></sub></form><legend id='zx6UU'></legend><bdo id='zx6UU'><pre id='zx6UU'><center id='zx6UU'></center></pre></bdo></b><th id='zx6UU'></th></span></q></dt></tr></i><div id='zx6UU'><tfoot id='zx6UU'></tfoot><dl id='zx6UU'><fieldset id='zx6UU'></fieldset></dl></div>

      • <bdo id='zx6UU'></bdo><ul id='zx6UU'></ul>
      <tfoot id='zx6UU'></tfoot>

      <small id='zx6UU'></small><noframes id='zx6UU'>

        <legend id='zx6UU'><style id='zx6UU'><dir id='zx6UU'><q id='zx6UU'></q></dir></style></legend>
      1. 使用句柄从 .lua 调用 lua 函数?

        时间:2023-09-27
        <i id='Dv8Ex'><tr id='Dv8Ex'><dt id='Dv8Ex'><q id='Dv8Ex'><span id='Dv8Ex'><b id='Dv8Ex'><form id='Dv8Ex'><ins id='Dv8Ex'></ins><ul id='Dv8Ex'></ul><sub id='Dv8Ex'></sub></form><legend id='Dv8Ex'></legend><bdo id='Dv8Ex'><pre id='Dv8Ex'><center id='Dv8Ex'></center></pre></bdo></b><th id='Dv8Ex'></th></span></q></dt></tr></i><div id='Dv8Ex'><tfoot id='Dv8Ex'></tfoot><dl id='Dv8Ex'><fieldset id='Dv8Ex'></fieldset></dl></div>
        • <bdo id='Dv8Ex'></bdo><ul id='Dv8Ex'></ul>

            <legend id='Dv8Ex'><style id='Dv8Ex'><dir id='Dv8Ex'><q id='Dv8Ex'></q></dir></style></legend>
          • <small id='Dv8Ex'></small><noframes id='Dv8Ex'>

              <tbody id='Dv8Ex'></tbody>

            <tfoot id='Dv8Ex'></tfoot>
                1. 本文介绍了使用句柄从 .lua 调用 lua 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在做一个小项目,试图将 lua 与 C++ 集成.然而,我的问题如下:

                  I'm working on a small project trying to integrate lua with c++. My problem however is as follows:

                  我有多个 lua 脚本,我们称它们为 s1.lua s2.lua 和 s3.lua.其中每一个都有以下函数:setVars() 和 executeResults().

                  I have multiple lua scripts, lets call them s1.lua s2.lua and s3.lua. Each of these has the following functions: setVars() and executeResults().

                  现在我可以通过 LuaL_dofile 并在使用 setVars() 和/或 executeResults() 之后立即调用 lua 文件.然而这里的问题是,在我加载 s2.lua 之后,我无法再调用 s1.lua 的函数.这意味着我必须在 s1.lua 上重做 LuaL_dofile 以重新获得对该函数的访问权限,这样做我将无法访问 s2.lua 中的函数.

                  Now I am able to to call a lua file through LuaL_dofile and immediately after use setVars() and/or executeResults(). The problem here however is that after I load s2.lua I can no longer call the functions of s1.lua. This would mean I have to redo the LuaL_dofile on s1.lua to regain access to the function and by doing so I lose access to the functions in s2.lua.

                  有没有办法简单地连续加载所有lua文件,然后开始随意调用它们的函数?像 s1->executeResults() s5->executeResults() s3->setVars() 等

                  Is there a way to simply load all lua files in a row, and afterwards start calling their functions at will? Something like s1->executeResults() s5->executeResults() s3->setVars() etc.

                  我目前已经有一个系统,使用 boost::filesystem 来检测文件夹中的所有 lua 文件,然后我将这些文件名保存在一个向量中,然后简单地遍历该向量以连续加载每个 lua 文件.

                  I currently already have a system in place using boost::filesystem to detect all lua files in a folder, I then save these files names in a vector and then simply iterate over the vector to load each lua file in a row.

                  放弃用 lua 文件名填充向量,我的插件加载函数现在看起来像这样:

                  Foregoing the filling of the vector with lua file names my plugin load function looks like this at the moment:

                  void Lua_plugin::load_Plugins(){
                   std::vector<std::string>::const_iterator it;
                   for (it=Lua_PluginList.begin(); it!=Lua_PluginList.end(); it++){
                    std::cout<<"File loading: " << *it << std::endl;
                    std::string filename =  *it;
                    std::string filepath = scriptdir+filename;
                    if (luaL_loadfile(L, filepath.c_str()) || lua_pcall(L, 0, 0, 0)) {
                     std::cout << "ScriptEngine: error loading script. Error returned was: " << lua_tostring(L, -1) << std::endl;
                    }
                   }
                  }
                  

                  为了更清楚一点,我在 .lua 中的所有内容是这样的:

                  To make it a bit more clear, all I have in the .lua's is something like this:

                  -- s1.lua
                  
                  setVars()
                  --do stuff
                  end
                  
                  executeResults()
                  --dostuff
                  end
                  

                  等,但我希望能够在简单地连续加载后调用 s1.lua 的 setVars() 和 s2.lua 的 setVars().

                  etc, but I would like to be able to call s1.lua's setVars() and s2.lua's setVars() after simply having loaded both in a row.

                  推荐答案

                  这实际上是 gwell 提出的使用 C API 的建议:

                  This is effectively what gwell proposed using the C API:

                  #include <stdio.h>
                  
                  #include "lua.h"
                  
                  static void
                  executescript(lua_State *L, const char *filename, const char *function)
                  {
                      /* retrieve the environment from the resgistry */
                      lua_getfield(L, LUA_REGISTRYINDEX, filename);
                  
                      /* get the desired function from the environment */
                      lua_getfield(L, -1, function);
                  
                      return lua_call(L, 0, 0);
                  }
                  
                  static void
                  loadscript(lua_State *L, const char *filename)
                  {
                      /* load the lua script into memory */
                      luaL_loadfile(L, filename);
                  
                      /* create a new function environment and store it in the registry */
                      lua_createtable(L, 0, 1);
                      lua_getglobal(L, "print");
                      lua_setfield(L, -2, "print");
                      lua_pushvalue(L, -1);
                      lua_setfield(L, LUA_REGISTRYINDEX, filename);
                  
                      /* set the environment for the loaded script and execute it */
                      lua_setfenv(L, -2);
                      lua_call(L, 0, 0);
                  
                      /* run the script initialization function */
                      executescript(L, filename, "init");
                  }
                  
                  int
                  main(int argc, char *argv[])
                  {
                      lua_State *L;
                      int env1, env2;
                  
                      L = (lua_State *) luaL_newstate();
                      luaL_openlibs(L);
                  
                      loadscript(L, "test1.lua");
                      loadscript(L, "test2.lua");
                  
                      executescript(L, "test1.lua", "run");
                      executescript(L, "test2.lua", "run");
                      executescript(L, "test2.lua", "run");
                      executescript(L, "test1.lua", "run");
                  
                      return 0;
                  }
                  

                  测试脚本:

                  -- test1.lua
                  function init() output = 'test1' end
                  function run() print(output) end
                  
                  -- test2.lua
                  function init() output = 'test2' end
                  function run() print(output) end
                  

                  输出:

                  test1
                  test2
                  test2
                  test1
                  

                  为简洁起见,我省略了所有错误处理,但您需要检查 luaL_loadfile 的返回值并使用 lua_pcall 而不是 lua_call.

                  I omitted all error handling for brevity, but you'll want to check the return value of luaL_loadfile and use lua_pcall instead of lua_call.

                  这篇关于使用句柄从 .lua 调用 lua 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 C++/C 设置全局 LUA_PATH 变量? 下一篇:从Lua调用函数时如何处理C++异常?

                  相关文章

                    <legend id='fwOw8'><style id='fwOw8'><dir id='fwOw8'><q id='fwOw8'></q></dir></style></legend>
                      • <bdo id='fwOw8'></bdo><ul id='fwOw8'></ul>
                      <i id='fwOw8'><tr id='fwOw8'><dt id='fwOw8'><q id='fwOw8'><span id='fwOw8'><b id='fwOw8'><form id='fwOw8'><ins id='fwOw8'></ins><ul id='fwOw8'></ul><sub id='fwOw8'></sub></form><legend id='fwOw8'></legend><bdo id='fwOw8'><pre id='fwOw8'><center id='fwOw8'></center></pre></bdo></b><th id='fwOw8'></th></span></q></dt></tr></i><div id='fwOw8'><tfoot id='fwOw8'></tfoot><dl id='fwOw8'><fieldset id='fwOw8'></fieldset></dl></div>

                      <small id='fwOw8'></small><noframes id='fwOw8'>

                      <tfoot id='fwOw8'></tfoot>