ID:2175986
 
(See the best response by Nadrew.)
I am trying to locate a specific object within a 5 tile radius of my own mob. If it finds multiple of the same, it should only return the closest one.
Is there already a premade function for this purpose? I have tried locate(), but scanning the whole map everytime does not seem like a good idea.
Best response
You were on the right path with locate().

var/obj/my_obj/found = locate() in view(src,5)
if(found)
src << "Found [found.name]!"
else
src << "Didn't find any /obj/my_obj within range.


This should always also give you the closest one.
Thanks.
If I wanted to get the distance between the object and the mob, is there a standard function for that?
In response to Lelcat
All objects A in range(Dist, B) satisfy the condition get_dist(A, B) <= Dist.

Here's the relevant chapter in the Guide for both of your questions. I recommend skimming through the entire Guide when you have the time. It's old and no longer complete, but still valid.
http://www.byond.com/docs/guide/chap14.html

Of course, you can look up every built-in proc (and more) in the Developer Reference built into Dream Maker by pressing F1.