Getting infinite loop issues but I know it'll eventually finish

I have a timer function running throughout the game and at times I want to speed this up (eg when sleeping). Everything is working fine except when using a while loop to check if the target time has been reached yet or not at which point I get an infinite loop error. I know that this will finish once the world time reaches the target time but Unreal doesn’t. BP below:


The first timer changes the timer to the faster speed, the second returns it to the default.

I’ve seen that I can override this by changing the max iterations, but it has got me wondering if there’s a more elegant solution to this problem. One thought I had was to have the event tick constantly check if the condition has been met or not and trigger custom events to change the timer’s times but I try to avoid using event tick where I can.

Anyone any thoughts?

Seems you do have an infinite loop. In Blueprints a WhileLoop runs in the same frame. Timers/Delays don’t tick while in the loop, so the condition never changes triggering max-iteration error.

Imo the condition should be after a branch where the first timer is executed.

2 Likes

You can extend the allowed number of loops before a loop is consider infinite somewhere in the engine preferences but won’t solve you issue and it will slow down your compile.

That being said I can’t possibly imagine why would you need a loop to check a timer. As @pezzott1 noted, the loop is executed in a single frame which means the timer is the same through the whole execution. And the loop is of course infinite in that case.

Maybe you can explain what you are trying to achieve with those timers so we can help clean up your BP and get rid of the loop altogether.

1 Like

Thanks for the reply! The context in the game is that various things will cause the time to speed up for a given amount of time before returning to the default time. For example, if the player goes to sleep, the timer speeds up rather than skipping straight to the morning, as things may still happen during the night. The times, however, need to be dynamic as the player will need to set times to spend on certain tasks.

Really struggling to find documentation on timers or tutorials that go more in depth than “this is how you set/pause/reset/etc a timer”

Thanks for the reply! I’m struggling to see how this causes an infinite loop (not doubting you, just trying to comprehend how timers are handled - documentation is hard to come by and tutorials only scratch the surface). It seems crazy to me that the time value can’t be checked/updated. If that’s what you’re telling me of course!

Hello again,

Check if this will help you understand how you can manipulate time:

You can place this in the player controller.

0, 1 and 2 will allow you to change the time speed and K will increase the time for a certain duration.

The actual SleepFor() is really simple:

  • It stores the current Time Dilation (Speed)
  • Sets a new, higher Time Dilation
  • Sets and stores a new timer which will automatically call EndSleep() on the tick it is completed
  • EndSleep() just restores the previously saved time

Ofc this is not perfect. You can still move around while yo “sleep” and you can still change the time speed while sleeping but I’m sure you can adapt it to your situation.

1 Like

This is absolutely perfect thanks! I had no idea about time dilation