Infinite loop in branch? Infinite loop error bug

Hey all,

i got a weird bug: Sometimes the engine throws an infinite loop error.
Sometimes because:

  • the function works most of the times
  • the error only gets thrown with larger inputs or if called in some static loops
  • the function works perfectly fine with small inputs

Even weirder: The call stack leads to the default UE5 branch node/macro.
The function that uses the branch also only has loops that terminate after a fixed amount (no dynamic loops or anything). I even tried hard-capping the amount of loop iterations, but the error persists.

How does a branch can be infinite?
How does a function can be sometimes infinite sometimes not?
Do i have to supress the error? (If yes how?)

If you need code help me let me know, but i didnt even know where to start with this.

Best regards,
M

1 Like

You need a delay, most likely.

Add a tiny delay (like .001 or .0001) to your function.

Also, please provide video and/or screenshots. It’ll make helping you waaaayyyy easier.

I restarted the engine and now i got the same bug inside the standard UE5 for-loop instead of the branch. The original issue stays the same tho.

Here are some screenshots:
Callstack/Error

Part of the function that changed (worked before):

The other for loops inside SetLOSCells:

I will add more images of the other for loops. If you have a specific guess pls let me know :slight_smile:

1 Like

Thanks for the screenshots, but where is the delay I suggested?

Where exactly you would put the delay? Also the delay node is only useable in the event graph and not in functions no?

You can put the delay right before the branch.

Also, there’s no reason delays can’t be used in fucntions.

Well how do i do a delay in functions? The delay node is simply missing in functions and only available in event graphs :confused:


1 Like

Ah, I was mistaken. You’re correct; you cannot create delays in Functions.

Do me a favor, extract/copy all of your code out of the functions, and drop them directly into the Event Graph with delay.

Do they work?

You don’t have to change any code. Leave everything the same, just attach EVENTBEGINPLAY to the copied code (with delay).

You can use delays in functions if done through a Macro Library (Actor).

1 Like

Good to know!

try to break your code into fewer parts, i believe it the while loop that causing the issue and add some print string to see how many times you are actually looping.

for example, only test the while loop first, if it does x10 print string, then you can test further with the first for loop…

all your loop in the same execution line get add together including ue5 default macro for loop, that why the error pop up there because it reach the limit of iteration.

1 Like

This is not a bug. If you trigger the infinite loop error you get the callstack that was executing whenever the runaway threshold was reached. Depending on what happens in your game, what other actors are present etc. this may not always be the exact same location. However it should more or less trigger in a place that is most frequently executing (i.e. has the highest probability to be executing when the limit is reached).

I explained some of the technical details under the hood in this thread here which also contains a few potential solutions. With the CellsToCheck you seem to have an array variable already so your’s isn’t exactly the same problem but most should still be applicable.

With the Loop until the queue is empty you should already be mostly setup to early out from the loop even before the queue is empty based on a simple maximum number of entries to process per call. Then you can just continue the next tick until the queue is actually empty by simply calling the function again.

1 Like

Using delays does not work because calling a delay multiple times has no effect. I also tried using a custom delay-for loop but it really messes with the max loop index. (Max index will sometimes be ignored).
So how do i do a working delay node inside a nested for-loop?

If i just use my custom delay-for loop (that works for a single for-loop) it will run out out the last index for some reason.

Wanted output:
0 0
0 1
1 0
1 1

The output i get with a delay in the for loop (even if i set the delay to 0):
0 0
1 1
1 0
2 0
3 1

My macro (like all the tutorials out there suggest):

The call:

delay doesnt work in loop body, you should break your code into smaller parts first and then figure out where the infinite loop occur.

Like i said there is no real infinte loop. The maximum number of steps is around ~250 - ~300 wich is no problem for smaller maps. As soon as i try bigger maps i get a (false) infinite loop error :confused:

You can change the limit from your project setting>engine:general setting>maximum loop iteration count.

note that if you do for each loop of 10, the iteration count will actually be more since there are more iteration inside the node for each itself.

it also add up all your loop together which i guess your while loop is causing it to reach max iteration.

1 Like

Alright i doubled the max iteration count and it works for now. Altough the real issue is not really solved i can at least look at the numbers now. Marking this as resolved!

1 Like

there also Function Timer if you want to make a manual loop with a timer, this will avoid infinite loop as well and improve performance.

Or you can use breakpoint to follow the execution flow of your code ingame.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.