Ai move to each point in a foreach loop?

You have to keep in mind that your game itself is a loop.

If you call a ForEachLoop it will be executed during a single tick or frame. In your example you give the AI len(array) directions at once and it will only move to the last one.

You have to wait until “On Success” before you walk to the next location (get the next array element) before you call “AI Move To” again.


Please note that I’m not using a behavior tree and I’m doing this inside of the AI character. I don’t want to use behavior trees because they’re either quite buggy, or I’m awful at understanding them for the time being. That aside, my issue is that the AI will only move to the first element in the array. Is there any way ! can stop the loop from performing the next iteration until it has reached the first point in the array?

Thanks for the reply. I know that it’s all executing at once, but how do I wait until its successful? Do I need to increment the index on success? I’m not at my computer right now so I can’t test

You could do a function which returns you the target actor given an index (int).

You create an event which has your move to logic and call it after your delay. One variable for the index which you increase each time after you called your “get actor function” and then simply plug that into your Ai move to.

On successful you call the same even again. If the index is out of bounds you simply stop calling it which will break your loop and stop your movement logic all together.

You could call a new even there (something like path finished) and implement further logic there.


Thanks for the help! Got it working. Had to ditch the ForEachLoop all together.

how did you solve this? I am having a similar issue, ive tried putting delays in to get some kind of feedback to no avail.

You mustn’t work with a for loop.

Unreal Engine is a kind of loop. Whatever loop you create will be executed during a single frame if you don’t do anything special.

You need to manually loop it (using an execution line and plug it in earlier) when “On Success” is triggered. Just have an array available and once you reached the last index break out of that loop (simple branch).