2

我正在使用带有 C# 的 LuaInterface,并且已经正确设置了所有内容。

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

当前代码:

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

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

4

1 回答 1

6

我看到两个选项。第一个是让你的播放器成为 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); // this will not actually run anything, just define a function
    QMain.lua.GetFunction('RunQuest').Call(player);        
}
于 2011-10-11T12:22:17.267 回答