ForEachLoopWithBreak sends singal through "Completed" even if loop has been canceled [proposed fix]

For me it’s counter-intuitive:

If this loop has been cancelled by Break input, signal still goes through “Completed” output. This is wrong, loop was broken, not completed. But macro doesn’t care, it always ends with “Completed” :wink:

Proposed fix

Let’s look at simple example to explain what would be intuitive and effective for me.
I use this loop to check if every array element is equal to zero.
A. If any element isn’t equal to zero, I just break the loop. Signal goes through “Broken” output (this doesn’t exist in current macro).
B. If all elements are equal to zero, signal goes though “Completed” output.

So I made simple mutation of original loop. Just added single condition check and"Broken" output. My scripts are bit simpler thanks to that. What you think, guys? Would be useful if Epic team would apply this tiny modification to original macro? :slight_smile:

1 Like

Hey miligram9-

The completed pin of the loop node is meant to run once the loop had been exited (through completion or breaking early). Having experimented with the same concept in relation to another post, the solution you posted actually is a great way to handle different behavior based on if the loop exits early or if the entire loop is ran.

Cheers

1 Like

I like the proposed fix and +1 on adding a new standard macro for this. The break in c++ code would work like this while a for loop with an else statement is more like the proposed. I suggest that Epic adds an else output pin to the ForLoopWithBreak that only fires if the Break is triggered and Completed fires at loop end regardless of how the loop ends. This would match for/else semantics in some programming languages, provide a new feature, and be backward compatible with existing uses of for loop.

Thanks,
-X

Super counter-intuitive, as the original poster said.

I thought I had written faulty code. :frowning:

I can’t believe that even after 5 years, Epic hasn’t added an “Aborted” output pin to this node.

What is the point of having a for each loop with break if it doesn’t work the way people use for loops with break statements in normal coding scenarios? The fact that it sends a completed signal even after hitting a break makes it useless. We’re now in 2024 into UE5 and for each loop with break still functions this way.

…and here we are another 4 years later and 9 years after the original post and the foreachloopwithbreak is still not working correctly. I also thought I’d written faulty code or just was completely in the dark on how that node is supposed to work. As is, it’s pretty useless. Surprised yet not at all that it’s not fixed yet. Thanks for solution and the sanity check.