将C#中的对象传递给Lua脚本

我在C#中使用LuaInterface,并已正确设置了一切。

我想实现的是,当使用lua.DoFile()启动脚本时,脚本可以访问我可以发送的Player对象...

当前代码:

public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
    QMain.lua.DoFile(LuaScriptPath);
}

但是,如您所见,脚本将无法访问Player对象。

原文链接 https://stackoverflow.com/questions/7724791

点赞
stackoverflow用户169828
stackoverflow用户169828

我看到两种选择。第一个是将您的玩家设置为 Lua 的全局变量:

QMain.lua['player'] = Player 

然后您可以在脚本中访问 player

第二个选择是让脚本定义一个接受玩家作为参数的函数。因此,如果您的当前脚本包含 ...code... 现在它将包含:

function RunQuest(player)
    ...code...
end

而您的 C# 代码将类似于:

public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
    QMain.lua.DoFile(LuaScriptPath); // 这将不会运行任何内容,只是定义一个函数
    QMain.lua.GetFunction('RunQuest').Call(player);
}
2011-10-11 12:22:17