LuaInterface问题

我在使用 C# 的 luainterface 库时遇到了一些问题:

1. 我加载了一个脚本并提取了其中的函数:

LuaFunction function = lua.GetFunction("Update");

但如果我加载了两个包含相同名称函数的不同脚本,该如何从 script1 和 script2 中提取两个不同名称的函数?

2. 如果我将函数加载到内存中,是否可能只丢弃一个特定的函数而不是所有函数?

3. 当我使用 Lua.DoFile 方法时,我想要执行文件中的特定函数。有什么想法吗?

编辑

2. 我发现可以像这样做

string f = @"
        function hh()
          end";

        var result = lua.DoString(f)[0] as LuaFunction;

但出于某种原因,我收到了 null 异常。有什么想法为什么?

点赞
用户198353
用户198353

DoString 将返回你的脚本所返回的内容。

lua.DoString ("return 10+10")[0]; // <-- 将返回 Double 类型的 20

如果你想要将你的 Lua 函数以 LuaFunction 的形式获取,你需要将你的函数返回,或者更好的方式是使用 [] 运算符获取 hh 的全局值。

lua.DoString ("function hh() end");
var hh = lua["hh"] as LuaFunction;
hh.Call ();

这里有一个例子: https://github.com/codefoco/NLuaBox/blob/master/NLuaBox/AppDelegate.cs#L46 (但使用 NLua 而非 LuaInterface)

记得在你不再需要该函数时,调用 Dispose 释放你的 LuaFunction

2013-11-16 16:00:18