Locations are actually objects in memory. When you use
Location(0, 0)
, that creates a completely
new object in memory that contains the coordinates (0, 0). However, what happens if you make another location at (0, 0)?
Try this out:
JASS:
if Location(0, 0) == Location(0, 0) then
call BJDebugMsg("Equal!")
else
call BJDebugMsg("Not equal!")
endif
Our intuitive guess would say that it should print "equal". However, it doesn't. It prints "Not equal!", because the equality operator
==
only checks if the objects are the same
in memory; it isn't sophisticated enough to check that they contain the same coordinates. For this reason, some programming languages will allow you to rewrite the behavior of
==
when comparing special objects, such as Locations. We can't rewrite the behavior of
==
in JASS, but we can always just write a function to help ourselves out!
JASS:
function AreLocationsEqual takes location a, location b returns boolean
return GetLocationX(a) == GetLocationX(b) and GetLocationY(a) == GetLocationY(b)
endfunction
That will directly compare the coordinates to see if they are equal. In most cases, that will work. However,
real
variables are a bit imprecise at times. They don't always make intuitive sense when we compare them with
==
, because they might be represented differently internally even if they are really close in value. To make a human analogy: if I am loading up a truck and I want the average apple to weigh 6 oz, I probably won't care whether it is 6.2 oz or 5.8 oz per apple. As long as it is reasonably close, I'll let it slide. But if it gets too far on either end, such as 5.5 oz or 7 oz, then it'll be concerning. As such, you might want to write an "approximately equal" function that handles that for you:
JASS:
function ApproximatelyEqual takes real a, real b, real epsilon returns boolean
return RAbsBJ(a - b) <= epsilon
endfunction
In your case, you might want to do something like this:
JASS:
set x = GetLocationX(udg_SUG_Point)
set y = GetLocationY(udg_SUG_Point)
set rx = GetRectMaxX(bj_mapInitialPlayableArea)
set ry = GetRectMinY(bj_mapInitialPlayableArea)
exitwhen ApproximatelyEqual(x, rx, 0.1) and ApproximatelyEqual(y, ry, 0.1)
You can change
0.1
to whatever precision you want.