Why am I getting infinite loops?

I am trying to do some cave generation but for some reason whenever I run these blueprints I get an infinite loop in each one.

In the above I get an infinite loop in the second for each loop even though it’s not doing anything at the moment. I have checked the startY and endY values and startY is always less than endY.

In the above I get an infinite loop in the ForEachLoop but when I disconnect the loop from the switch statement I no longer get an infinite loop.

You might be running into the Blueprint Loop limit. Although this defaults to 1000000, which should be enough for most cases, nested loops can cause you to reach the limit anyway.

You can increase the limit in the Project Settings > Engine: General Settings > Blueprints.

Infinite loop can happen due to the time it takes as well and not necessarily the issue of the code. there is a maximum number of statement per loop supported for UE4 blueprints. check this thread How to bypass the loop limit? - World Creation - Unreal Engine Forums

although the best solution is to write large codes in C++ and make a function blueprint for them

Both the outer loop and the inner loop have a max difference of 4 meaning that the max iterations between the both of them is 25.

have you tried placing a break point (F9) and stepping through the execution?

I placed a break point but when I press Play it never gets caught and just crashes because of the infinite loop. No matter where I place the break point.

does that not mean your crash occurs outside of the code you tested?
have you checked the crash log for hints?

Not it means breakpoints aren’t working, because I put a print statement in the loop and a breakpoint on that and run it and it prints but it doesn’t break.

The crash log says there is an infinite loop at my two loops.

Just for clarification: Both of these screenshots show separate loops, right? And both are aborted as “infinite loops”?

In the first example, what happens inside the second loop? The first screenshot stops just where it gets interesting. :slight_smile: How many iterations of loop 2 does it go through? (Use a Print node if you haven’t done that already.)

Yes, they both are.

It crashes without doing anything in the second loop. It goes though all 25 iterations