# Infinite loop in branch? Infinite loop error bug

**URL:** <https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323>\
**Category:** Blueprint\
**Tags:** unreal-engine, bug-report, Blueprint, question\
**Created:** [October 8, 2023, 4:08pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323 "2023-10-08T16:08:58Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [October 8, 2023, 4:08pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/1 "2023-10-08T16:08:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leomerya12](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/leomerya12/32/926321_2.png) [@Leomerya12](https://forums.unrealengine.com/u/Leomerya12)\
**Post date:** [October 8, 2023, 4:22pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/2 "2023-10-08T16:22:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [October 8, 2023, 4:47pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/3 "2023-10-08T16:47:08Z")

</div>

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

 ![loopBug1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/4/4/544aed3544b646eef6c72c4b4669bcd987130cd0.png)

Part of the function that changed (worked before):

 ![loopBug2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/e/1/3e10e2822f3813282f4736db8c103d676188bc3a.png)

The other for loops inside SetLOSCells:

 ![loopBug3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/d/6/1d6e8faef360b625c373ac1388a6eea396562261.png)

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

---

<div class="post-metadata">

**Author:** ![Leomerya12](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/leomerya12/32/926321_2.png) [@Leomerya12](https://forums.unrealengine.com/u/Leomerya12)\
**Post date:** [October 8, 2023, 5:06pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/4 "2023-10-08T17:06:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [October 8, 2023, 5:10pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/5 "2023-10-08T17:10:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leomerya12](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/leomerya12/32/926321_2.png) [@Leomerya12](https://forums.unrealengine.com/u/Leomerya12)\
**Post date:** [October 8, 2023, 5:40pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/6 "2023-10-08T17:40:40Z")

</div>

You can put the delay right before the branch.

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

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [October 8, 2023, 6:03pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/7 "2023-10-08T18:03:19Z")

</div>

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

 ![loopBug4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/c/3/bc3117c5f81fb3f52583f283ac3f9196e024eb99.png)  
 ![loopBug5](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/1/0/8100f621c52b2593ad164a2d49a58260bfeab935.png)

---

<div class="post-metadata">

**Author:** ![Leomerya12](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/leomerya12/32/926321_2.png) [@Leomerya12](https://forums.unrealengine.com/u/Leomerya12)\
**Post date:** [October 8, 2023, 6:13pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/8 "2023-10-08T18:13:40Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Rev0verDrive](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rev0verdrive/32/907214_2.png) [@Rev0verDrive](https://forums.unrealengine.com/u/Rev0verDrive)\
**Post date:** [October 8, 2023, 10:33pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/9 "2023-10-08T22:33:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leomerya12](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/leomerya12/32/926321_2.png) [@Leomerya12](https://forums.unrealengine.com/u/Leomerya12)\
**Post date:** [October 8, 2023, 10:35pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/10 "2023-10-08T22:35:53Z")

</div>

Good to know!

---

<div class="post-metadata">

**Author:** ![Arodi007](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arodi007/32/1022275_2.png) [@Arodi007](https://forums.unrealengine.com/u/Arodi007)\
**Post date:** [October 9, 2023, 3:28am UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/11 "2023-10-09T03:28:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UnrealEverything](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UnrealEverything](https://forums.unrealengine.com/u/UnrealEverything)\
**Post date:** [October 9, 2023, 3:58pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/12 "2023-10-09T15:58:31Z")

</div>

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](https://forums.unrealengine.com/t/absolute-bs-infinite-loop-error/836778) 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.

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [October 11, 2023, 4:25pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/13 "2023-10-11T16:25:48Z")

</div>

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):

 ![forloopwithdelay](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/e/0/3e0e93c64c3f9d166d4691848f553c84ba478f49.png)

The call:

 ![forloopwithdelay2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/7/e976bdb48b43dee3424ebe9672dd21f9ebc1b58a.png)

---

<div class="post-metadata">

**Author:** ![Arodi007](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arodi007/32/1022275_2.png) [@Arodi007](https://forums.unrealengine.com/u/Arodi007)\
**Post date:** [October 11, 2023, 5:03pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/14 "2023-10-11T17:03:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [October 12, 2023, 11:43am UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/15 "2023-10-12T11:43:38Z")

</div>

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 😕

---

<div class="post-metadata">

**Author:** ![Arodi007](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arodi007/32/1022275_2.png) [@Arodi007](https://forums.unrealengine.com/u/Arodi007)\
**Post date:** [October 12, 2023, 1:24pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/16 "2023-10-12T13:24:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Maffinius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/maffinius/32/546414_2.png) [@Maffinius](https://forums.unrealengine.com/u/Maffinius)\
**Post date:** [October 12, 2023, 3:55pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/17 "2023-10-12T15:55:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Arodi007](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arodi007/32/1022275_2.png) [@Arodi007](https://forums.unrealengine.com/u/Arodi007)\
**Post date:** [October 12, 2023, 4:24pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/18 "2023-10-12T16:24:34Z")

</div>

there also [Function Timer](https://docs.unrealengine.com/4.26/en-US/InteractiveExperiences/UseTimers/) 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](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/Blueprints/UserGuide/Debugging/#:~:text=Breakpoints%20are%20markers%20that%20can,the%20Blueprint%20Editor%27s%20graph%20view.) to follow the execution flow of your code ingame.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [November 11, 2023, 4:24pm UTC](https://forums.unrealengine.com/t/infinite-loop-in-branch-infinite-loop-error-bug/1326323/19 "2023-11-11T16:24:47Z")

</div>

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