c# - 从 C# 将对象传递给 Lua 脚本

标签 c# lua luainterface

我在 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/

相关文章:

c# - 使用包含数组元素索引的数组属性序列化类

c# - 如何创建包含其他标签助手的自定义标签助手

string - 使用 string.find() 查找 '.'

c# - 在 WinForms 中以可本地化的形式在运行时更改 CurrentUICulture

c# - linq/lambda 中的 Asp.net mvc html.DisplayFor 语法

node.js - 从redis key nodejs弹出值的原子操作

lua - lua中带有可变参数的pcall

c# - LuaInterface:如何访问多维lua表?

c# - 如何将 WinForm 传递给 Lua (LuaInterface)?

c# - 为 LuaInterface 构建 Lua 模块