Why am I getting an Infinite Loop?

I’ve just recently started learning blueprints, so I tried to make a simple while loop that runs an action for 5 seconds before breaking away and running some other stuff. I’m getting an infinite while loop, and it seems to only occur when my condition is related to time (or at least my method of comparing time). Why is this? And, help!

Hi @NigwodMonkey!

While Loops are executed during the same frame, and is blocking the engine from moving forward until the condition is met,

Your condition for the while loop will never be true due to how you’ve set it up. Since the engine is blocked during the while loop, no frames (nor time seconds) will update at all.

Details
At beginplay the StartTime will be set to GetTimeSeconds. 
Let us say that it has a value of 1 at that time. 
So now both StartTime and GetTimeSeconds is 1.

The loop condition for exiting is that  GetTimeSecond(1) - StartTime(1) is *less* than 0. 
Which it during this frame never will be due to not updating (additionally StartTime 
should be subtracted GetTimeSeconds, not the other way around).

Usually, you want to update the condition inside the while loop, making sure that you can get out from it with code that actually executes.

Alternatives

Using a timer could achieve this if it’s a delay you’re after:

Or using a delay

2 Likes

Thank you for the wonderful explanation!