- Home /
Weird behavior with types in Unity JScript
say I have a class like this:
class Point{
public var X: int;
public var Y: int;
function Point(x: int, y: int)
{
this.X = x;
this.Y = y;
}
function GetX() : int
{
return X;
}
function GetY() : int
{
return Y;
}
static function Add(p1 : Point, p2 : Point) : Point
{
return new Point(p1.X + p2.X, p1.Y + p2.Y);
}
static function Equals(p1 : Point, p2 : Point) : boolean
{
Debug.Log(p1.X +" == " + p2.X + " && " + p1.Y + " == "+ p2.Y);
return (p1.X == p2.X && p1.Y == p2.Y);
}
}
If I do something like
var P1 : Point = new Point(1,1);
var P2 : Point = new Point(2,2);
Debug.Log(Point.Equals(P1, P2);
everything works out great. However, if I have another class that has a method like:
function GetPositionPoint() : Point
{
var returnPoint : Point = new Point(x, z);
return returnPoint;
}
and I attempt to call
Debug.Log(Point.Equals(class1.GetPositionPoint(), class2.GetPositionPoint()));
The method just returns false. What's more, the debug statement inside the Equals function doesn't even run.
The following things fix this behavior:
Changing the method's name from
"Equals" to "isEqual"
Changing the method's signature from Equals(p1: Point, p2: Point) to Equals(p1,p2)
Calling Add on the points before calling Equals, like:
Point.Equals(Point.Add(class1.GetPositionPoint(), new Point(0,0)),Point.Add(class2.GetPositionPoint(), new Point(0,0)));
obviously i've found several ways to work around this, but the behavior here really baffles me. I'm sure it's some odd interaction between unity/jscript's typing of objects and the fact that i'm hiding the already existing Equals() method that unity/mono objects have... but if anyone can better explain this, please do.
--
Answer by Statement · Apr 04, 2011 at 02:09 AM
What's more, the debug statement inside the Equals function doesn't even run.
Where are you running it from?
I don't know what you're doing but that code runs fine for me.
// So they don't serialize in inspector. private var x = 3; private var z = 6; private var r = Point.Equals( Point.Add(GetPositionPoint(), new Point(0,0)), Point.Add(GetPositionPoint(), new Point(0,0)));
print(r);
function GetPositionPoint() : Point { var returnPoint : Point = new Point(x, z); return returnPoint; }
I don't know really what else can be done. Look over your code and make sure the serialized values don't toss anything unexpected.
Update
However if you rename "Equals" to "isEqual" or something else, then it'll use the Equals of System.Object (System.Object.Equals) which default implementation is a reference check.
To me it sounds this is the case somehow since you get unexpected result from the check and you also don't get the debug log.
Call Point.Equals(GetPositionPoint(), GetPositionPoint()) ins$$anonymous$$d. This returns false.
Nope. Still return true. However if you rename "Equals" to "Equal" or something else, then it'll use the Equals of System.Object which default implementation is a reference check.
Check your code for typos. It looks like you're using System.Object.Equals (inherited into Point)
Or, you're using System.Object references. You must be explicit about the type.
Check out this article about how to define value equality: http://msdn.microsoft.com/en-us/library/dd183755(v=VS.90).aspx
Answer by Bunny83 · Apr 04, 2011 at 03:48 AM
I've just created a small test and it works fine like Statement said:
class Point{ public var X: int; public var Y: int;
function Point(x: int, y: int)
{
this.X = x;
this.Y = y;
}
function GetX() : int
{
return X;
}
function GetY() : int
{
return Y;
}
static function Add(p1 : Point, p2 : Point) : Point
{
return new Point(p1.X + p2.X, p1.Y + p2.Y);
}
static function Equals(p1 : Point, p2 : Point) : boolean
{
Debug.Log(p1.X +" == " + p2.X + " && " + p1.Y + " == "+ p2.Y);
return (p1.X == p2.X && p1.Y == p2.Y);
}
}
class Test123 { var x : int; var y : int; function Test123(aX : int,aY : int) { x = aX; y = aY; } function GetPositionPoint() : Point { var returnPoint : Point = new Point(x, y); return returnPoint; } }
function Start (){ var C1 : Test123 = new Test123(10,20); var C2 : Test123 = new Test123(10,20); Debug.Log(Point.Equals(C1.GetPositionPoint(),C2.GetPositionPoint()));
C2 = new Test123(11,20);
Debug.Log(Point.Equals(C1.GetPositionPoint(),C2.GetPositionPoint()));
}
Result:
10 == 10 && 20 == 20
True
10 == 11 && 20 == 20
False