ID:2140803
 
Which code is more efficient if you want to know if something is in your view and know the range

var/cview=6
if(target in view(cview,src)

or
var/cview=6
if(get_dist(src,target)<=cview)


My logic is telling me get_dist is more efficient since its a pure calculations while the 'in view' does some sort of looping thru view(). But I would like some verification on this.
They're not logically equivalent, since view() takes mob sight, opacity, and z-level into account.

But if you're okay with that, it might actually be most efficient to compare coordinates one at a time:
z == target.z && abs(target.x - x) <= cview && abs(target.y - y) <= cview
since there's some short-circuiting involved.

Even more efficient would be to not perform the check too frequently. In the case of AI checking for targets, it shouldn't happen every tick.
thx bro
If you're looking for a same-Z distance, the best of both worlds is probably checking get_dist() and then the z coordinate.

get_dist() will do some math internally to get the x, y, and z coordinates. That very same math is done when you access a coordinate individually, except it's done each time. Therefore for fastest results, you'd want to use get_dist() first, and then if that check passes, try the Z coordinate.

If for some reason this check might be done more against things that are just about anywhere, then checking z first might end up being the better option; but chances are it won't matter much for performance. Also if you're going to be looping through a list of objects in a certain range, then range() is probably the better bet by far.
It would be nice though if there was a byond function to do a line of sight check between two objects alone. Replicating view() with your own code to check line of sight is HARD.
The view calculations are done as a whole, though, so there's really no way to separate the view() call from the individual LOS check. If BYOND did true shadowcasting that wouldn't be the case, but it uses its own algorithm.