# How do I make an object select the closest of two others?

Get help using Construct 2

### » Fri Oct 07, 2016 5:13 pm

Hi all!
Apologies if this has been solved already. I took a gander at the faq post and tried the search, but may not have been looking for the proper terms :/

I'm trying to make a condition where if object A has line of sight to object B and object C, it will select/pathfind the closest of the two. I can narrow it down to the closest instances of each object individually (using the 'pick nearest instance' setting), but I can't seem to figure out how to make it determine which one is actually closer...I'd go ahead and write my own condition that compares the X and Y of each to Object A, plus math maybe, and have that make the decision, but I'm not exactly sure how to do that sort of custom condition/function deal, nor am I sure if there's an even easier or maybe built-in way of doing it!

Thanks in advance for any tips and help!
B
2
Posts: 3
Reputation: 158

### » Fri Oct 07, 2016 6:25 pm

There is a condition under the chosen object, Pick Nearest/Futhest. Can you utilise that?
B
13
S
2
Posts: 15
Reputation: 1,595

### » Fri Oct 07, 2016 6:48 pm

Yeppers, and thanks for the response! However, each object is distinct and different in this case, and that function does not seem to be available unless a specific object is selected. So I've already used that function to determine which instance of Object B is closest to me, and which instance of Object C is closest, but now I must determine which of those is closer.

I'm trying to have 'enemy AI' automatically attack the player, as well as 'friendly AI' when they come into line of sight.
I believe I can pull it off with just the order of events, but that would seem to lead to a priority of selecting one over the other if both were in view. Like, I could just have it check for the player first, and then attack, and if there's no player, then check for the 'friendly AI' and attack that instead...but aside from being a practice/learning experience in the finer points of Construct, it's just not how I'd prefer the AI to react at the moment.

Another way I've toyed with is just making it random, in the sense where if it detects both in sight, assign some random number to both of the detected, nearest instances of those objects, and then select the one with the lower (or higher, whatever) number. I'm leaning towards this option, if in fact I simply cannot make it choose the closest target, but I want to believe that with someone's creative ingenuity, this can be (and has already been) done!

We must believe!!
B
2
Posts: 3
Reputation: 158

### » Fri Oct 07, 2016 8:13 pm

This scenario is what 'families' are for but unfortunately those are not available in the free version.

You can still achieve what you want by storing the info in variables and comparing.

Pick closest objectA; store distance in variableA
Pick closest objectB; store distance in variableB
If variableA < variableB; attack ObjectA
-Else; attack ObjectB
B
44
S
26
G
11
Posts: 517
Reputation: 10,500

### » Fri Oct 07, 2016 8:28 pm

Ahh, awesome! Thanks for the tip! I'll have to look into this family business and the full version.
Til then I'll have another crack at it and see if i can make sense of it with your advice!

B
2
Posts: 3
Reputation: 158

### Who is online

Users browsing this forum: brunopalermo and 14 guests