How do I choose the nearest enemy which can be reached

Just started using Construct 2? Post your questions here

Post » Thu Mar 13, 2014 2:54 pm

Hi There, I am trying to create an AI for a unit. In plain words it would be:

- Pick nearest enemy and go to him. But if you can't reach him (obstacles in the way), go to the second nearest.
If you look at the picture attached to this post, 1 should go to A, 2 to B and 3 to C. But currently 1 doesn't move because the command „pick nearest“ picks out D. How could I exclude D from the list of possible targets?

All the O are the same type of unit, and all the X are a different type of unit. The box which D is in might get destroyed, so it should still be targetable as soon as it is "reachable".

For the moment I am using

On mouse click
System > For each X
O > Pick Nearest to (X.x, X.y)
Find path to (O.x, O.y)
Move along path

As I said, it works perfectly, as long as a path can be found to the nearest target. As soon as a path cannot be found to the nearest target, the X doesn't move.

I would be very grateful if you could grant me some insight into a solution to this problem.

Posts: 4
Reputation: 233

Post » Fri Mar 14, 2014 2:37 am

A way you can do it I think, is to make a search bot kind of thingy like a google bot, in form of a check object. So if you add an object called "Checker". Which only purpose is to check each target to see if they are reachable and make the checker invisible, it should just run in the background.

Then to the enemies you add a Boolean variable called "Reachable".

As the checker goes through each enemy you changes this variable to either true or false. So if "Checker" found path to enemy you set "Reachable" to true, if it fails you set it to false.

To your other objects, you can use the same code as you do now, but just add that it should only check enemies where "Reachable" is true.
Posts: 1,209
Reputation: 7,537

Post » Fri Mar 14, 2014 12:27 pm

Hi Nimos! Thank you for your answer, that sounds like a great idea, I will try this right now.
Posts: 4
Reputation: 233

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest