Andrew-101's Closest Function.

This is a function that Andrew-101 made and it is super useful. You can use it to get the closest object to the target.

For people who need the prop to be true.

def GetClosest(target, prop):
    obj_list = [obj for obj in GameLogic.getCurrentScene().objects if obj.get(prop)]
    closest = obj_list[0] # Default closest
    distance = target.getDistanceTo(closest)

    for object in obj_list[1:]:
        new_distance = target.getDistanceTo(object)
        if new_distance < distance:
            closest = object
            distance = new_distance
            
    return(closest)

For the people who just need the object to have the prop

def GetClosest(target, prop):
    obj_list = [obj for obj in GameLogic.getCurrentScene().objects if obj.has_key(prop)]
    closest = obj_list[0] # Default closest
    distance = target.getDistanceTo(closest)

    for object in obj_list[1:]:
        new_distance = target.getDistanceTo(object)
        if new_distance < distance:
            closest = object
            distance = new_distance
            
    return(closest)

Thank you Andrew!! :smiley:

No worries :stuck_out_tongue:

:smiley: This function is just so useful. In any AI you could use this for multiple things.

pretty cool! Nice Andrew! Very simple to use :), I also made one of those, but it was more complicated then it should of been 0.o.

Thanks for sharing!

A thing that you can change if you want it to be so that the property does not have to be true is, on the line that says:
obj_list = [obj for obj in GameLogic.getCurrentScene().objects if obj.get(prop)]

Just change it to:
obj_list = [obj for obj in GameLogic.getCurrentScene().objects if obj.has_key(prop)]

This is one of my favorites, I first found this in Plant Persons as the crow flies pathfinding a few years ago.

I never knew that Andrew-101 made it. One more to add to the list of cool things Andrew-101 did :wink:

If you want an example of how I used it, just check out this cool AI I created. LINK

In my file, I changed it a bit and just named the function closest instead of GetClosest.

Haha, if you found it years ago I probably didn’t make it, I only coded this about a month ago


obj_list = [obj for obj in GameLogic.getCurrentScene().objects if obj.get(prop)]

i think you broke my mind D:

can has explanation of how this works plz

Simplified Code:

scene = GameLogc.getCurrentScene()
objList = scene.objects

obj_List = for obj in objList if obj.get(prop)

The quotes around it show that it is a list. All it is doing is getting all the objects in the scene then adding them to the list if obj.get(prop) == True.

EDIT:
Its the same thing as doing this:


obj_list = []
for obj in GameLogic.getCurrentScene().objects:
        if obj.get(prop) == True:
               obj_list.append(obj)



Its called “list comprehension”, each iteration adds an element to the list. You can give it a google for some better explinations that what I can give, but basically it lets you construct lists with some elegance I guess.

Cool I didnt know that Andrew. Awesome.

This is awesome. Does this work if you want to do “get closest object with the property ____” ?
Or does it need to be changed ?

Its made to work with a property, just call it like:

player = cont.owner
closest_baddie = GetClosest(player, 'Baddie')

and it will get the closest object with the Baddie prop.

Awesome!!! So many uses coming to mind right now. Thank you so much.

:o hum… I’m a little shy to ask this so newbish question but… what does it do exactly ? I know its supposed to get you to the closest object with the prop “yourpropname”… but how would I implement this into a game ? How does the object move ? Does it face the object it goes to ? How can we adjust speed ?

I’m not requesting a tutorial, its just that I’m a little confused.

Happy modeling

It gets all the objects in the scene, then if the object has the defined property, it adds it in the list. Then it sorts the list by distance, and gets the closest one from you.

That is all the script does, you can add speed, tracking etc. if you want.

What you would do is, put the code into your script. Then, where ever you want to find the closest object, you would say:

GetClosest(player,‘enemy’)

“player” is the main object that the function will search around.

“‘enemy’” is the property that each object must have in order to be found by the function.

So basically, it finds all objects around the player object that have the ‘enemy’ prop. Then, it returns the closest one.

Thank you :yes:.