1

我正在尝试用 LuaJ 覆盖 .new() lua 函数(方法?)。我试图在我的表的元表中设置一个“新”键,但是当在 lua 中调用 .new() 时,它被忽略并运行默认的 java 支持的函数,将表创建为 java 对象。

如果我将密钥重命名为(例如)“new2”,并使用“table.new2()”创建它,它就没有问题。

希望有人可以帮助我!

到目前为止的代码:

    LuaValue vectorClass = CoerceJavaToLua.coerce(Vector3Lua.class);
    luaj.globals.set("Vector3", vectorClass);

    LuaTable table = new LuaTable();
    table.set("new2", new ThreeArgFunction() {
        public LuaValue call(LuaValue x, LuaValue y, LuaValue z) {
            System.out.println("Created new Vector: " + x + ", " + y + ", " + z);
            return LuaValue.NIL;
        }
    });
    table.set("__index", table);

    vectorClass.setmetatable( table );

Lua 测试器:

    local test1 = Vector3.new(2, 3, 4);  --> Does not print test message
    local test2 = Vector3.new2(4, 4, 4); --> Does print test message

这样做的原因是我可以将元表应用于自动创建的每个“Vector3”实例,以向用户隐藏内部功能。

4

0 回答 0