Why is GetLength() used at all?
I can only imagine 2 answers:
a) because NS2 devs didn't optimize it
b) because cost of Lua calls/interpreting is so high that it doesn't matter
What I mean is that (a-b).GetLength() < 2 is a lot more expensive than (a-b).GetLengthSquared() < 4.
Also, is Vector class moved to C++ or are parts of it in C++? First half of definitions in Vector.lua is commented out.
It's really easy to write:
IsDistanceLessThan(vec3 x, vec3 y, float dist)
IsDistanceLessThanOrEqual(vec3 x, vec3 y, float dist)
so there 2*2=4 part is hidden and all is even nicely readable.
a) because NS2 devs didn't optimize it
b) because cost of Lua calls/interpreting is so high that it doesn't matter
What I mean is that (a-b).GetLength() < 2 is a lot more expensive than (a-b).GetLengthSquared() < 4.
Also, is Vector class moved to C++ or are parts of it in C++? First half of definitions in Vector.lua is commented out.
It's really easy to write:
IsDistanceLessThan(vec3 x, vec3 y, float dist)
IsDistanceLessThanOrEqual(vec3 x, vec3 y, float dist)
so there 2*2=4 part is hidden and all is even nicely readable.
Comments
Though in most of the cases I've seen you wouldn't get any value out of changing it to GetLengthSquared, because the calls are made so infrequently that the performance hit is negligible anyway. The maintenance cost of refactoring those kinds of calls and potentially introducing new bugs is probably higher than the performance hit you get from the couple of extra cycles.
The most important cases are things like distance check loops that get called every tick and hence you're wasting a few cycles each frame, and I don't think I've seen any examples of that in the vanilla NS2 codebase since build 161 or so. If you do find one like this, mention it in the forums and I'm sure they'll plug the gap!
I didn't know about this method. Thanks for sharing the knowledge!