我如何加载多个脚本而没有变量问题



我有一个我阅读lua的类在执行另一个脚本的第一个脚本中。

我希望该程序能够输出 -

print = 123
print = Hi 0
Result = 3
print = Hi 1
Result = 3
print = Hi 2
Result = 3
print = Hi 3
Result = 3
print = Hi 4
Result = 3
print = Hi 5
Result = 3
print = Hi 6
Result = 3
print = Hi 7
Result = 3
print = 123
print = Hi 0
Result = 3
print = Hi 1
Result = 3
print = Hi 2
Result = 3
print = Hi 3
Result = 3
print = Hi 8
Result = 3
print = Hi 9
Result = 3
print = Hi 10
Result = 3
print = Hi 11
Result = 3

,但她将其取出 -

print = 123
print = Hi 0
Result = 3
print = Hi 1
Result = 3
print = Hi 2
Result = 3
print = Hi 3
Result = 3
print = Hi 4
Result = 3
print = Hi 5
Result = 3
print = Hi 6
Result = 3
print = Hi 7
Result = 3
print = 123
print = Hi 0
Result = 3
print = Hi 1
Result = 3
print = Hi 2
Result = 3
print = Hi 3
Result = 3
print = Hi 4
Result = 3
print = Hi 5
Result = 3
print = Hi 6
Result = 3
print = Hi 7
Result = 3

这是我的代码:

    #pragma once
    #pragma comment(lib, "lua53.lib")
    extern "C" {
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
    }
    #include <iostream>
    #include <LuaBridge/LuaBridge.h>
    using namespace std;
    using namespace luabridge;
    class Lua_sup
    {
    private:
    string m_name;
    lua_State* L ;
    bool init_state = false;
    inline void PrintError(int run_status)
    {
        switch (run_status)
        {
        case LUA_ERRRUN:
        {
            printf( "LUA: Runtime Error: %sn", lua_tostring(L, -1));
            break;
        }
        case LUA_ERRSYNTAX:
        {
            printf("LUA: Syntax Error: %sn", lua_tostring(L, -1));
            break;
        }
        case LUA_ERRMEM:
        {

            printf("LUA: Memory Alloc Error: %sn", lua_tostring(L, -1));
            break;
        }
        case LUA_ERRERR:
        {
            printf("LUA: Error returning Error: %sn", lua_tostring(L, -1));
            break;
        }
        default:
        {
            printf("LUA: Unknown Error: %sn", lua_tostring(L, -1));
            break;
        }
        }
    }
    public:
    Lua_sup(string name, lua_State* s);
    ~Lua_sup();
    void init();
    void test();
    bool get_state() { return init_state; }
    };
//.cpp
#include "Lua_sup.h"

void printMessage(const std::string& s) {
    cout << "print = " << s << endl;
}
string get_string() {
    return "Hi ";
}
Lua_sup::Lua_sup(string name, lua_State* s)
{
    m_name = name;
    L = lua_newthread(s);
}

Lua_sup::~Lua_sup()
{

    lua_close(L);
}
void Lua_sup::init()
{
    //L = luaL_newstate();
    getGlobalNamespace(L).addFunction("printMessage", printMessage);
    int load_status = luaL_loadfile(L, m_name.c_str());
    if (load_status != 0)    {
        cout << "Error";
        PrintError(load_status);
        init_state = false;
    }
    lua_pcall(L, 0, 0, 0); 
    getGlobalNamespace(L).addFunction("get_string", get_string);
    getGlobalNamespace(L).addFunction("printMessage", printMessage);
    init_state = true;
}

void Lua_sup::test()
{
    LuaRef hello_world_sum = getGlobal(L, "hello_world_sum");
    LuaRef plot = getGlobal(L, "plot");
    for (int i = 0; i < 3; i++)
    {
        int result = hello_world_sum(1, 2);
        cout << "Result = " << result << endl;

    }

    int result = hello_world_sum(1, 2);
    cout << "Result = " << result << endl;
    getchar();
}

//my main
#include "Lua_sup.h"
using namespace luabridge;


int main() {
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);

    Lua_sup lua("script.lua", L);
    lua.init();
    lua.test();
    lua.test();
    Lua_sup lua1("123.lua", L);
    lua1.init();
    lua1.test();
    lua.test();
}

我正在使用luabridge

您想要的是用自己的环境加载每个文件。我可以充满信心地告诉您如何从LUA完成此操作,但是由于您使用C( (,我只能依靠手册,该手册说,当加载LUA代码时,它将设置环境为"在索引处"注册表中的lua_ridx_globals"(我假设您知道C注册表以及如何访问它(。

由于您很可能希望原始环境保持可用,因此您必须创建一个新的环境,并以__索引原始环境。请记住,这仍然允许代码通过_G变量明确设置全局变量,这可能是一件好事。

这是LUA中的等效代码,您必须将其移植到C:

local meta = {_index=_G}
-- Metatable for the new environment
local function load_sandboxed(file)
  local new_env = setmetatable({}, meta}
  -- Create a new environment with metatable. Missing keys
  -- will be looked for in _G before returning nil
  loadfile(file, "t", new_env)
  -- Load the file with a custom environment.
  -- Only allow text files.
end

Lua_sup的每个实例都需要它自己的 lua_State实例。

int main() 
{
    lua_State* stateA = luaL_newstate();
    luaL_openlibs(stateA);
    Lua_sup lua("script.lua", stateA);
    lua.init();
    lua.test();
    lua.test();
    lua_State* stateB = luaL_newstate();
    luaL_openlibs(stateB );
    Lua_sup lua1("123.lua", stateB );
    lua1.init();
    lua1.test();
    lua.test();
    lua_close(stateA);
    lua_close(stateB);
}

这将每个脚本保持分开,并且它们不相互作用。

注意,当您完成状态时,最好关闭您的状态

相关内容

  • 没有找到相关文章

最新更新