AI stopping instead of continuing to follow path

Hello there,

I am having an issue with my AI scripting. I have set up an enemy character, as well as an actor called AiTarget. I have made the enemy create an array of all the targets, pick a random one, and move to it. Then, when it overlaps on the target point, the script is run again. This worked fine, until I modified it so that I could have multiple paths, with an enemy following a path each.

My way of managing this was to give the AiTarget an editable integer variable called ‘PathNumber’, and give the enemy character an editable integer variable called ‘target’. I then placed two enemy characters into the world, and 6 target points, half with a path number of 1, half with a path number of 2. I gave one enemy character a target of 1, and the other a target of 2. This is my blueprint for the enemy character:

First, I trigger on begin play, and when the character overlaps a targetPoint. This should make the script keep going.

I then go on to add every targetPoint to the array, and then remove ones that aren’t part of the target path.

Finally, I delay for between 2 and 8 seconds, then move to a random targetPoint in the array.

For some reason though, when I click play, the code works for a bit, and then one of the characters tends to stop moving, and the other starts going to points from both parts. Sometimes the last character then stops. The code works fine with one character. Here is a video showing what happens:

I have tried a few other methods as well, but to no avail. Any idea why the AI stops after some time? Help is much appreciated.

Thanks a lot.

Hey debeerboy,

The first thing that could go wrong when the AI is running is that if the AI randomly chooses the point he is already at, if this happens he will not choose any new points because he can only get a new instruction when he BeginsOverlap with a target point.

The second thing that is wrong (I think you don’t want it like this) is every time he overlaps a target point he will choose a new point to move to, he does not actually check if he arrived at the position he wants to go or not he just checks a point of class, so the moment he runs over a point he will add a move to instruction to kinda like a stack of instructions with delays in between. To test this you can remove the delay and you will see what I mean :slight_smile:

The third thing that is not recommended is deleting items out an array while in a for-loop of this array, this can give some strange behaviors and you will only understand if you exactly know what it does in code. Try creating a new array and add the Items you want to use, then you can use this array to choose the actual targetpoint.

Solve all of these problems your AI will behave like you want it to :slight_smile:

If you have any other questions you can ask!

Hope this helps,
Elias

Thanks, can’t believe I didn’t think of #1. I will take your advice and let you know what happens.

Thanks, it worked!