ForEach doesn't stop executing on RETURN

I rewrote a large chunk of a code I wrote before in UnrealScript, in blueprint over last few days, only to find out there is a logical bug there, which was almost impossible to track down. After trying everything, I added watches and logs to whole code, only to find this: ForEach continues to go through elements of array, EVEN after you return from function, I mean what heck?? (BTW I haven’t tested but For is probably same.)

Is this expected behavior (maybe because For and ForEach are just macros and not real language flow units)? Or did I just found a critical bug?

I’m because out of 8-9 programming languages that I coded in over years, not a single one is like this, NONE. Whenever you return from a function, flow stops right there and gets back to where you called function. At least that’s what I learned as a basic fact in any language.


If this shouldn’t happen and is in fact a bug in your eyes, you can ignore next paragraph. If so, please let us know when can we expect a fix.

IF in fact in BP, ForEach can’t break on a return command, and you can’t fix it in any way, in other words, this was an expected behavior from day 1 that Bp was born, then please add an obvious warning sign somewhere that user can read before using For/ForEach, because no need to say, no one expects this kind of behavior, the “Return” command should have precedence over all flow controls. I wasted almost 3 days of my game development time, because of this silly thing, if it’s a bug I can live with it, since it seems I’m first one to face it, it’s understandable, otherwise if you are going to have such big contradiction with whole world of programming languages, please at least advertise it widely.

1 Like

Hey ,

way it’s set up in Blueprints, a Function is called and it runs everything inside it until completed. Using a Return Node output will not interrupt Loop, as it would in code, and because of this it is possible to use return nodes to return multiple variable data during loop. To achieve a break way you want, you can use a ForLoopWithBreak or ForEachLoopWithBreak node, which includes a Break input. Using whatever check you were using with Return Node, attach output to Break input and then attach your Return Node to Completed output.

Blueprints are built this way to provide more flexibility to non-coders, but using For With Break nodes can accomplish what you would normally get with code. Return Nodes do not function as breaks for functions, and I can see how that might be confusing. I will mention this to developers and see about renaming output nodes for clarity.

Hope that helps!

Thanks for reply. Yes I already replaced all ForEach nodes that return a value with ForEachWithBreak and adjusted code accordingly, before I posted this whole question.

With this post, as mentioned, I was trying to let developers know if they don’t already know about it (since I couldn’t find anywhere any mention of this in my limited search), otherwise if it is expected as you said, then request to make it crystal clear, that this is how whole thing works, because well, this is only 1 out of many languages out there that is setup like this.

I mean switch command in C and C++ is different than many other languages, but they also keep telling you how it is different when you’re learning C, so when you come form a different language, you know you’re facing a different switch than standard ones. I think this one is just as important if not more, to let users know of this critical difference. I learned it hard way, just hope others will have a better day after this difference has been advertised enough.

Hopefully this post and you mentioning it to developers will cause a change to make this a known limit, rather than a strange side effect that you have to face before you read about it.

Thanks again for both your replies, and have a good day.

It would also be helpful to have that tidbit about “Using a Return Node output will not interrupt Loop, as it would in code, and because of this it is possible to use return nodes to return multiple variable data during loop.” In documentation as well. ■■■■ it’s handy(now that I know about it), but as said, it is an ‘unexpected’ behavior for anyone coming from a previous programming environment, and it has been throwing me for a few days as well.

Could you elaborate on how you would use that to get multiple return values though? Thinking of logical flow with that, how would you keep rest of your code from progressing while you wait on those outputs from return node?

Hey RAVaught,

I’ve mentioned it to developers, but updates to documentation can take some time, especially so close to new engine releases. I’ll make a feature request for it so we have a way to track it.

I would need to see what you are attempting to achieve to answer thoroughly, but it should be simple enough to return multiple values using multiple Outputs in your function, and breaking loop when proper conditions are met. Because function is not stopping when it hits a ReturnNode, it can continue loop and get as many Output values as needed. function only stops when it is done calculating everything inside it, and assuming function is only performing calculations and not performing any heavy lifting, rest of your code will continue after function is complete.

For anyone being lead astray to this answer by google, a Return node will absolutely stop execution of a loop within a blueprint function. In 4.22 and beyond at least.

1 Like

Just wanted to confirm this as I was unsure and just tested this in UE5.EA.

If you have a For Loop or For Each Loop inside a function and trigger an “early return”, it stops execution of function.

2 Likes