铝Lua定义lua函数。

使用AluminumLua我有一个lua文件,在其中我设置了一些函数到变量中,如下所示:

local Start = function() print("Inside Start!") end

在.NET中,我试图加载这个文件,但它卡在解析方法上,从未返回。

class Program
{
    static void Main(string[] args)
    {
        var context = new LuaContext();

        context.AddBasicLibrary();
        context.AddIoLibrary();

        var parser = new LuaParser(context, "test.lua");

        parser.Parse();

    }
}

有什么想法它卡了吗?

点赞
用户2679394
用户2679394

我还没试过 AluminiumLua,但我之前已用过 LuaInterface 多次。如果你想让你的函数在启动时加载,可以像这样包含或 DoFile/DoString 你的文件,并运行函数:

local Start = function() print("Startup") end
Start()

如果你试图在 Lua 中定义钩子,可以使用带有 KopiLua 的 LuaInterface,然后按照如下方式操作:

C#:

static List<LuaFunction> hooks = new List<LuaFunction>();

// 注册此 void
public void HookIt(LuaFunction func)
{
    hooks.Add(func);
}

public static void WhenEntityCreates(Entity ent)
{
    // 如果我们返回 lua 中的第一个参数为 true,则删除实体,如果第二个参数为 true,则隐藏实体
    foreach (var run in hooks)
    {
        var obj = run.Call(ent);
        if (obj.Length > 0)
        {
            if ((bool)obj[0] == true) ent.Remove();
            if ((bool)obj[1] == true) ent.Hide();
        }
    }
}

lua:

function enthascreated(ent)
    if ent.Name == "Chest" then
          return true, true
    elseif ent.Name == "Ninja" then
          return false, true
    end
    return false, false
end
2013-10-24 14:08:50