C# 将对象从C传递到Lua脚本#
我正在将LuaInterface与C#一起使用,并且已正确设置了所有内容 我希望能够做到的是,当使用lua.DoFile()启动脚本时,脚本可以访问我可以发送的播放器对象 当前代码:C# 将对象从C传递到Lua脚本#,c#,lua,luainterface,C#,Lua,Luainterface,我正在将LuaInterface与C#一起使用,并且已正确设置了所有内容 我希望能够做到的是,当使用lua.DoFile()启动脚本时,脚本可以访问我可以发送的播放器对象 当前代码: public static void RunQuest(string LuaScriptPath, QPlayer Player) { QMain.lua.DoFile(LuaScriptPath); } 但正如您所见,脚本将无法访问Player对象。我看到两个选项。第一个是使您的播放器成为Lua的全局变
public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
QMain.lua.DoFile(LuaScriptPath);
}
但正如您所见,脚本将无法访问Player对象。我看到两个选项。第一个是使您的播放器成为Lua的全局变量:
QMain.lua['player'] = 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);
}
我看到两种选择。第一个是使您的播放器成为Lua的全局变量:
QMain.lua['player'] = 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);
}