# Nearest object of the same type

I am trying to make a city sim game and for one of the systems i want two animals of the same type to walk towards each other if close enough. I have tried using “Pick the (animal) that is nearest to (animalX, animalY)” but that usually results in the animal itself being chosen as the “nearest” instead of the actual other nearest animal. How do I make it so that the animal closest to another animal of the same type is chosen instead?

This works for picking 2nd or 3rd nearest object to something

So you could use that to pick 2nd nearest which would not be the object itself

IF not i would spawn on one object a custom object and use same logic to pick 2nd nearest object

1 Like

This does work but I cant find out how to implement it in the way I wanted. It works for finding the nearest or second nearest Tree from a Player but I want to see if there’s a way to for example find the nearest or second nearest Tree from another Tree. I want the animals in my game to be able to find the nearest animal of the same type and use the pathfinding behavior to walk towards each other. Would simply putting (pick the nearest “animal” closest to “animal.X, animal.Y”) work in the same way or should I do it differently?

ZeroX4 did mention you might spawn a custom object. If I were to expound on that I’d say to give your animals some kind of accessory object or something. Doesn’t even have to be visible. Hide it if you like.

Let’s say you have a cow. Give the cow a “shadow” object. Link the shadow to the cow. Now all cows are gonna have their own shadow linked to each of them. Where the cow goes, its shadow should go there too. So now you can apply the events to the shadow in order to find the second nearest cow.

1 Like

And what’s the problem here?
Imagine this
Imagine you have 3 types of pigs
Red ping green pig and blue pig
Now you give them variable called Color with R or G or B
And so
Condition
Pig.Variable(Color)=Pig.Variable(Color)

And here in sub event you go with crap i wrote in previous message

You could apply same crap based on animation
You could dynamically distribute variables to each pig when they should start to search
For example
If pig should start looking for another pig
You set for this ping variable Search to 2
And rest are left with 0 since it is default value
Now you set current pig color var to scene var
You check for pigs color var to be equal to color var
And you just filtered them

Same with object picking

A lot of ways to do it
BUT try what i wrote at the beginning of this message (RGB method)
If you struggle we will find some other dumb solution

1 Like

I can get the (RGB method) of object picking to work, I just don’t know how I can get an animals pathfinding to select the second nearest animal. The pathfinding behavior only has a “Move to a position” option instead of moving to one certain sprite. I assume I can use variables to store an animals position and use that as the X and Y in the pathfinding, but if there are a lot of animals all searching for their own nearest animal it sounds like they would all end up going to the same variable position. I’ll have to do some more testing to see if I could get either of the two methods working but ideally i’d prefer to use the RGB method instead of the “shadow” method that Levio had also mentioned because if there are a lot of animals on the scene I would need a second object for every animal which could slow the game more if there are too many.

Well you know most simple way is to have 2 additional variables for each animal

And they are
XPosTarget
YPosTarget

And what you would do is move pigs wherever you want
Now when there is time to find other pig you disable event which move your pigs wherever you want
And you find nearest pig of same kind
You put into your other pig these 2 variables X and YPosTarget AND set them to position of that nearest pig
NOW you simply move that ham toward X and Y position and as coordinates you give XPosTarget YPosTarget

As simple as that