Loops in Unreal Engine

So I had a while loop inside the player to do some math every time the boolean was true. But when I started the compile sequence, the editor froze on me and i think it’s stuck in freeze. Would the actual way have been to put a timer in the loop? Or use something entirely different, like delta seconds?

Maybe provide a screenshot. basically you need to be especially careful when adding loops to the Tick Event (which fires very often)

If your while loop never close it will lock the game thread.

Not really. If the loop is tight then you’d have to be more careful, but that goes for anything that takes a lot of time i.e. raycasts or sweeps.

My loop simply did this:


 while (true) {
if (bIsRunning == true){
GetCharacterMovement()->MaxWalkSpeed = 600.0f;
}
else {
GetCharacterMovement()->MaxWalkSpeed = 200.0f;
}
} 

Pretty much all it did.

That’s an infinite loop. “while(true)” is always true, so that loop will never exit. I assume you want to just adjust the character speed according to that boolean?

Remove the loop and just place that code in OnTick, it’ll run each frame.



virtual void MyActor::OnTick(float DeltaTime) 
{
   Super::OnTick(DeltaTime);

   if (bIsRunning == true){
     GetCharacterMovement()->MaxWalkSpeed = 600.0f;
   }
   else {
    GetCharacterMovement()->MaxWalkSpeed = 200.0f;
   }
}

You can read more about Ticking here.

OH THANK YOU, I had no clue how to even get the basis of looping things down. Thank you.