I am trying to overwrite the .new() lua function(method?) with LuaJ. I tried to set a "new" key in the metatable for my table, but when calling .new() in lua it is ignored and runs the default java-backed function of creating the table as a java object.
If I rename the key to (for example) "new2", and create it with "table.new2()" it works no problem.
Was hoping anyone could help me out!
Code so far:
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 tester:
local test1 = Vector3.new(2, 3, 4); --> Does not print test message
local test2 = Vector3.new2(4, 4, 4); --> Does print test message
The reason behind doing this is so I can apply a metatable to each instance of "Vector3" created automatically, to hide internal functionality from the user.