# While Loop Crashing Editor

**URL:** <https://forums.unrealengine.com/t/while-loop-crashing-editor/436990>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 3, 2018, 11:52pm UTC](https://forums.unrealengine.com/t/while-loop-crashing-editor/436990 "2018-11-03T23:52:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_b392f3e9](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b392f3e9](https://forums.unrealengine.com/u/anonymous_user_b392f3e9)\
**Post date:** [November 3, 2018, 11:52pm UTC](https://forums.unrealengine.com/t/while-loop-crashing-editor/436990/1 "2018-11-03T23:52:40Z")

</div>

I am trying to use a while loop to move forward fast. This is my code:

```
void AMyCharacter::Fast()
{
	gofast = true;

	while(gofast == true){
		AddMovementInput(GetActorForwardVector() * 99999);
	}
}

void AMyCharacter::StopFast()
{
	gofast = false;
}

void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAction("Fast", IE_Pressed, this, &AMyCharacter::Fast);
	PlayerInputComponent->BindAction("Fast", IE_Released, this, &AMyCharacter::StopFast);
}

```

when i press the key to make it go fast the editor crashes and i am not sure why.

---

<div class="post-metadata">

**Author:** ![sjcobs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sjcobs/32/918649_2.png) [@sjcobs](https://forums.unrealengine.com/u/sjcobs)\
**Post date:** [November 4, 2018, 1:17am UTC](https://forums.unrealengine.com/t/while-loop-crashing-editor/436990/2 "2018-11-04T01:17:09Z")

</div>

You are crashing the engine with an infinite loop. You need to figure out another way of doing this, like using tick or a timer instead.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b392f3e9](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b392f3e9](https://forums.unrealengine.com/u/anonymous_user_b392f3e9)\
**Post date:** [November 4, 2018, 1:24am UTC](https://forums.unrealengine.com/t/while-loop-crashing-editor/436990/3 "2018-11-04T01:24:11Z")

</div>

Isn’t a while loop a infinite loop anyways? isn’t that the point?

---

<div class="post-metadata">

**Author:** ![sjcobs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sjcobs/32/918649_2.png) [@sjcobs](https://forums.unrealengine.com/u/sjcobs)\
**Post date:** [November 4, 2018, 1:43am UTC](https://forums.unrealengine.com/t/while-loop-crashing-editor/436990/4 "2018-11-04T01:43:03Z")

</div>

No not really. It still needs a way to eventually come out of it. Its a loop that does something while something else is being done (implying that it will finish on its own). When you enter your loop even for just a few seconds it will execute millions of times and detects an inifinit loop and crashes because nothing is saying that things will change for it to break out. Most loops do not need to be run a million times before breaking out.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_1b1f8823](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_1b1f8823](https://forums.unrealengine.com/u/anonymous_user_1b1f8823)\
**Post date:** [November 7, 2018, 11:50pm UTC](https://forums.unrealengine.com/t/while-loop-crashing-editor/436990/5 "2018-11-07T23:50:40Z")

</div>

Here is an example of a while loop.

```
 int i=1;
       /* The loop would continue to print
        * the value of i until the given condition
        * i<=6 returns false.
        */
       while(i<=6){
          cout<<"Value of variable i is: "<<i<<endl; 
          i++;
       }

```

I think you can probably have a scaling float value that can be your fast speed and remove the while loop.

// toggling fast speed  
if(e is pressed)  
fastSpeed = 99999;

// removing fast speed  
if(x is pressed)  
fastSpeed = 1

// Your move function  
AddMovementInput(GetActorForwardVector() \* fastSpeed);

Good luck!

---

<div class="post-metadata">

**Author:** ![mightyenigma](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mightyenigma](https://forums.unrealengine.com/u/mightyenigma)\
**Post date:** [November 20, 2018, 8:17am UTC](https://forums.unrealengine.com/t/while-loop-crashing-editor/436990/6 "2018-11-20T08:17:42Z")

</div>

The game has to finish executing all the functions you call before it can go to the next Tick and do the next frame.  
A while loop will run infinite times before the next frame of the game can move.
