0

I am trying to create a Vector class for use with LuaJ. The end goal is to have the user not write much lua, and have most of the work done on my Java engine.

As my understanding goes, I need to set the metatable for the lua representation of my Java vector class? The problem I am having though, is that when I am trying to overwrite some metatable functionality it doesn't seem to have any affect in my lua script. What I am trying to do now is overwrite the + operator, so I can add two vectors together or add a vector by a constant.

Here is my Vector class so far:

package math;

import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.lib.jse.*;

public class Vector3Lua {
 public float X;
 public float Y;
 public float Z;

 public Vector3Lua unit;

 static {
  // Setup Vector class
  LuaValue vectorClass = CoerceJavaToLua.coerce(Vector3Lua.class);

  // Metatable stuff
  LuaTable t = new LuaTable();
  t.set("__add", new TwoArgFunction() {
   public LuaValue call(LuaValue x, LuaValue y) {
    System.out.println("TEST1: " + x);
    System.out.println("TEST2: " + y);
    return x;
   }
  });
  t.set("__index", t);
  vectorClass.setmetatable(t);

  // Bind "Vector3" to our class
  luaj.globals.set("Vector3", vectorClass);
 }

 public Vector3Lua() {
  // Empty
 }

 // Java constructor
 public Vector3Lua(float X, float Y, float Z) {
  this.X = X;
  this.Y = Y;
  this.Z = Z;

  this.unit = new Vector3Lua(); // TODO Make this automatically calculate

  System.out.println("HELLO");
 }

 // Lua constructor
 static public class New extends ThreeArgFunction {

  @Override
  public LuaValue call(LuaValue arg0, LuaValue arg1, LuaValue arg2) {
   return CoerceJavaToLua.coerce(new Vector3Lua(arg0.tofloat(), arg1.tofloat(), arg2.tofloat()));
  }
 }

 // Lua Function - Dot Product
 public float Dot(Vector3Lua other) {
  if ( other == null ) {
   return 0;
  }

  return X * other.X + Y * other.Y + Z * other.Z;
 }

 // Lua Function - Cross Product
 public LuaValue Cross(Vector3Lua other) {
  Vector3Lua result = new Vector3Lua( Y * other.Z - Z * other.Y,
    Z * other.X - X * other.Z,
    X * other.Y - Y * other.X );
  return CoerceJavaToLua.coerce(result);
 }
}

Here is the lua script that makes use of this:

local test1 = Vector3.new(2, 3, 4);
local test2 = Vector3.new(1, 2, 3);
print(test1);
print(test2);
print(test1+2); 

The last line produces an error, as it says I cannot add a userdata and a number together. However, in my vector class I tried to get it to just print what is being added, and just return the original data (to test). So I believe my problem is how I am defining my metatable; In my vector class, the two print's are never called.

orange451
  • 21
  • 4
  • I don't know LuaJ and what is the use case, but `print(test1+2);` seems a little bit confusing to me. Isn't it `print(test1+test2);` ? – PeterMmm Oct 18 '17 at 18:27
  • It's a test in this case. In the future it could be adding two vectors, but I want to also implement a vector added by a constant. – orange451 Oct 18 '17 at 20:01

1 Answers1

0

print(test1+2); should be print(test1+test2);. You got that error because test1 is a userdata (basically an under-the-hood version of a table) and 2 is a number.

Blue
  • 22,608
  • 7
  • 62
  • 92
NetherGranite
  • 1,940
  • 1
  • 14
  • 42
  • While I appreciate the reply, this is a misunderstanding of the question. The question was not "why can I not add two incompatible datatypes together?". the question was "Why can I not CHECK if I am adding two incompatible datatypes together?". – orange451 Aug 09 '18 at 12:13
  • @orange451 Oh, oops, sorry about that. I just reread your question, and I'm still not sure where in it you ask that question specifically. I would recommend revising your question (unless I am actually just stupid and couldn't find it, please tell me if this is the case). – NetherGranite Aug 11 '18 at 22:22
  • "The problem I am having though, is that when I am trying to overwrite some metatable functionality it doesn't seem to have any affect in my lua script. What I am trying to do now is overwrite the + operator, so I can add two vectors together or add a vector by a constant." "The last line produces an error, as it says I cannot add a userdata and a number together. However, in my vector class I tried to get it to just print what is being added, and just return the original data (to test)" Regardless, this question is almost a year old. I solved it a long time ago. – orange451 Aug 12 '18 at 04:54
  • @orange451 Oh, I see. Sorry for the misunderstanding. There is a feature of Stack Overflow that allows you to answer your own question. I would consider doing this to help others who run into the same issue as you. – NetherGranite Aug 12 '18 at 07:55