我在 C# 中使用 LuaInterface,并且所有设置都正确。
我希望能够做的是,当脚本使用 lua.DoFile() 启动时,脚本可以访问我可以发送的 Player 对象...
当前代码:
public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
QMain.lua.DoFile(LuaScriptPath);
}
但如您所见,脚本将无法访问 Player 对象。
最佳答案
我看到两个选项。第一个是让你的播放器成为 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);
}
关于c# - 从 C# 将对象传递给 Lua 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724791/