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

0 favourites
  • 5 posts
From the Asset Store
A set of retro 16-Bit Neon UI elements to make your menus pop!
  • 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!

  • There is a condition under the chosen object, Pick Nearest/Futhest. Can you utilise that?

  • 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!!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • 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!

    I appreciate your help!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)