从C#将对象传递到Lua脚本 [英] Pass Object to Lua Script from C#
问题描述
我在C#中使用LuaInterface,并且一切设置正确.
I am using LuaInterface with C#, and have got everything set up correctly.
我想做的是,当使用lua.DoFile()启动脚本时,该脚本可以访问我可以发送的Player对象.
What I want to be able to do is that when the script is started using lua.DoFile(), that the script has access to a Player object that I can send...
当前代码:
public static void RunQuest(string LuaScriptPath, QPlayer Player)
{
QMain.lua.DoFile(LuaScriptPath);
}
但是如您所见,脚本将无法访问Player对象.
But as you can see the script will not have access to the Player object.
推荐答案
我看到两个选项.第一个是使您的玩家成为Lua的全局变量:
I see two options. The first one is to make your player a global variable for Lua:
QMain.lua['player'] = Player
然后您就可以在脚本中访问player
Then you'll be able to access player
in your script
第二个选项是让脚本定义一个接受播放器作为参数的函数.因此,如果您当前的脚本包含...code...
,它将包含:
The second option is to have the script define a function accepting player as parameter. So if your current script contains ...code...
now it will contain:
function RunQuest(player)
...code...
end
,您的C#代码将如下所示:
and your C# code will look something like this:
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#将对象传递到Lua脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!