Waiting for to finish before continuing foreach loop?

I have a headache trying to figure this out.

The basic problem is this:

  1. The ForEachLoop is given an array with two items.
  2. It processes index 0 to the end. The end node is an ‘AI Move To’
  3. Without waiting for the previous ‘AI Move To’ to finish on success it moves to index 1
  4. It processes index 1 to the end. The end node is an ‘AI Move To’
  5. The ‘AI Move To’ at the end of index 0 is aborted
  6. The ‘AI Move To’ at the end of index 1 continues

So what I am trying to accomplish is to make it wait until AI Move To has succeeded (ie reached destionation), and I can’t seem to figure out how to do so.

So what I wound up doing is go away from using For Each Loop, and instead utilize my entity’s tick. In there I have a “Busy” toggle.

When not busy, if I still have items in my array I will toggle busy to on and process the first item in my array. On completion I toggle busy off again, and I am able to process the next item in the next tick.

I’ll leave this question up for a little longer to see if anyone else have alternative suggestions.

1 Like

I’m don’t have unreal on this computer, but I don’t remember For Each Loop ever skipping over anything before going to the next variable in the array.

Perhaps there’s a more clever workaround, but you could just put a delay node after AI Move To if you know how long it’ll take for it to complete. If each AI Move To is different, then perhaps you just need another variable (or another array of integers) that stores how long the movement will take, then feed it to the delay node.

Hei @Drunkenvalley, it is correct that a ForEachLoop does not wait until the AI Move To has completed. This is because AI Move To is asynchronous, which you can tell from the clock symbol. You probably do not need to resort to the tick function with a “busy” toggle. Instead you could have an array of places to go to, possibly a copy of what you already have, and do the following:

  1. Check if the array is empty, if so you are done. If not, go to (2)
  2. Take the first element from the array and AI Move To that destination
  3. On Success, which triggers happens async, the flow continues:
  4. Remove the first element, because that is where the AI should be now
  5. Repeat starting from (1)

In blueprints that looks something like:

Keep in mind that the array of destinations is modified, it only has the destinations that still need to be visited. The array can be updated while the AI moves from one place to another, just never modify the first element and it should work just fine. It might be this isn’t useful for you, but you could use a similar asynchronous cycle without removing elements. In that case you just want to keep track of the current index, starting from 0 and increasing where I now removed an element. Then your “Are we done”-check should just check whether the index is equal to the length of the array.

Disclaimer, unfortunately I can’t really test this solution right now, but it should work.

1 Like

It does work! Great suggestion. Thank you.