简体   繁体   English

从C#将对象传递到Lua脚本

[英]Pass Object to Lua Script from C#

I am using LuaInterface with C#, and have got everything set up correctly. 我在C#中使用LuaInterface,并且一切设置正确。

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... 我想要做的是,当使用lua.DoFile()启动脚本时,脚本可以访问我可以发送的Player对象。

Current Code: 当前代码:

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

But as you can see the script will not have access to the Player object. 但是如您所见,脚本将无法访问Player对象。

I see two options. 我看到两个选择。 The first one is to make your player a global variable for Lua: 第一个是使您的玩家成为Lua的全局变量:

QMain.lua['player'] = Player

Then you'll be able to access player in your script 然后您就可以在脚本中访问player

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: 因此,如果您当前的脚本包含...code...现在它将包含:

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

and your C# code will look something like this: 并且您的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);        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM