# How to make a while loop with key presses

**URL:** <https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518>\
**Category:** Blueprint\
**Tags:** while-loop, unreal-engine, need-help, Blueprint, question\
**Created:** [May 5, 2015, 2:54pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518 "2015-05-05T14:54:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 2:54pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/1 "2015-05-05T14:54:41Z")

</div>

I don’t have a lot of so hopefully someone jumps on this quick. I need to stay in this while loop as long as one of the arrow keys aren’t pressed and when they are it needs to run the compare checks than exit. I’m not really sure how to do that with this loop body thing. Help! Please!

 ![41428-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/8/8/488fa888daf9e36933fda7cd9ce43dccbbcdc04c.png)

---

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 3:04pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/2 "2015-05-05T15:04:41Z")

</div>

Another way to put it is. I need check that one of these 4 keys was pressed if that’s true check if matches then break from a loop of some sort

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_776011b61](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_776011b61](https://forums.unrealengine.com/u/anonymous_user_776011b61)\
**Post date:** [May 5, 2015, 3:49pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/3 "2015-05-05T15:49:23Z")

</div>

Why not run the loop without keypress

Put a bool, and break loop when Key is pressed?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [May 5, 2015, 3:54pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/4 "2015-05-05T15:54:27Z")

</div>

What you are asking for is “Get Player Controller” → “Is Input Key Down” and then just or them all together and put them into your while loop condition.

edit: Why do you need to be in the loop though? I don’t think this will do what you want it to do. If you just want it to run what you have when keys are released, then your blueprint already does that without the loop, but you shouldn’t be handling input that way, you should be setting your input in the project settings so you can rebind your keys or have multiple keys bound to the same action.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_776011b61](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_776011b61](https://forums.unrealengine.com/u/anonymous_user_776011b61)\
**Post date:** [May 5, 2015, 3:57pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/5 "2015-05-05T15:57:11Z")

</div>

No, to have release, you must press first.

Run it of key pressed, not release.

**edit**

Unless all 4 keys are pressed at once

---

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 3:58pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/6 "2015-05-05T15:58:00Z")

</div>

I was trying to do something like that. It wasn’t working for me though.

![41438-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/7/4/37411402b36eb8fb7acbaa8fc37400e1b715b10c.png)

However I have a new Idea. Do you have any idea how to store an actor so I can transfer it outside a function?

 ![41440-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/2/1/d21f167b232957aa854445e84a88febcfd36864c.png)

So instead of destroying the actors I can place one of them into some kind of place holder pull it out of the function and destroy in another function?

---

<div class="post-metadata">

**Author:** ![juniortan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@juniortan](https://forums.unrealengine.com/u/juniortan)\
**Post date:** [May 5, 2015, 3:58pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/7 "2015-05-05T15:58:12Z")

</div>

Gate might help

---

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 4:02pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/8 "2015-05-05T16:02:49Z")

</div>

I’m trying to get 4 arrows to spawn at once then the player goes through one by one matching input once all 4 are matched correctly or incorrectly spawn a new set i thought if i was able to do it with functions it would be easier however i cant figure out how to get keypress to work in a function call

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [May 5, 2015, 4:04pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/9 "2015-05-05T16:04:06Z")

</div>

What exactly are you trying to do?

If you want to run your check any one of those keys is pressed, then just hook up what you want to run to the pressed events. If you want to run your check any one of those keys is released then just hook up what you want to the released events. It’s not entirely clear what you are trying to accomplish and how that is different from what you have.

---

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 4:06pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/10 "2015-05-05T16:06:41Z")

</div>

Right now I do have it working for one arrow. The arrow spawns flashes on the screen then is destroyed and the player can hit the button that he thinks is the correct one. I want to have 4 arrows stand still on the screen and then as the player goes through and hits an input the first arrow would be destroyed then then the next… till the last one is destroyed then it starts over again

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [May 5, 2015, 4:10pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/11 "2015-05-05T16:10:15Z")

</div>

How is what you have not functioning the way you intend? Is your blueprint on each individual arrow? What you want is to store all of your arrow components in a list, and then compare any input event against the head of the list and then pop it out of the list, then the next input will compare against the new head of the list. Once the list is empty, repopulate it with more arrows.

You definitely don’t want to run this in a while loop, because a while loop doesn’t execute on a per frame basis like a tick. The while loop will just run infinitely and stall the game.

---

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 4:12pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/12 "2015-05-05T16:12:13Z")

</div>

Yes this sounds perfect a list how would i go about doing this in blue prints?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_776011b61](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_776011b61](https://forums.unrealengine.com/u/anonymous_user_776011b61)\
**Post date:** [May 5, 2015, 4:13pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/13 "2015-05-05T16:13:25Z")

</div>

You need to this a completely different way

---

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 4:14pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/14 "2015-05-05T16:14:24Z")

</div>

No my blue print is not on each arrow the blueprint is in the level so the arrows can be randomly spawned at certain locations.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_776011b61](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_776011b61](https://forums.unrealengine.com/u/anonymous_user_776011b61)\
**Post date:** [May 5, 2015, 4:17pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/15 "2015-05-05T16:17:06Z")

</div>

Hi

This post is a mess, without the picture, can you re-post and ask what you want?

May need to clean this post up.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [May 5, 2015, 4:22pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/16 "2015-05-05T16:22:03Z")

</div>

Ok. So you do something like this. In your level set it up something like this:

 ![41446-arrows1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/e/6/be67bf83344272d76ceb1d95768c32a9985040d8.png)

Then in your Populate Arrows do something like this:

 ![41447-arrows2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/6/4/b64f83f2380dabaeba944174212e7c851aad8bf5.png)

Then in your compare function just do whatever you need to do to compare against the two. You should be able to just reuse what you have for your compares already.

edit: I didn’t test this yet, but something like this should work. The order of the remove/destroy might be mucked up a bit, but this should point you in the right direction.

---

<div class="post-metadata">

**Author:** ![Azarus](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Azarus](https://forums.unrealengine.com/u/Azarus)\
**Post date:** [May 5, 2015, 4:23pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/17 "2015-05-05T16:23:56Z")

</div>

Use Tick instead or a gate instead of a while loop.

> **[Featured Posts - Amirite](https://www.amirite.com)**
>
> Find out who agrees with you. Post your thoughts and instantly be told if you are right!

You are miss using the while loops. A while loop runs until its true.

---

<div class="post-metadata">

**Author:** ![Tolli](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tolli](https://forums.unrealengine.com/u/Tolli)\
**Post date:** [May 5, 2015, 4:54pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/18 "2015-05-05T16:54:05Z")

</div>

This makes a lot of sense but i compare the arrows to input based on ints how would i check which actual key was checked also how did you create that compare with current arrow function?

 ![41458-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/0/8/b08d3c17d5d37336dbdc39682408730db52b91eb.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_33e74471](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_33e74471](https://forums.unrealengine.com/u/anonymous_user_33e74471)\
**Post date:** [May 5, 2015, 5:55pm UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/19 "2015-05-05T17:55:52Z")

</div>

My “Compare” function was just a placeholder that didn’t actually do anything. You can use similar to how you have except use the “==” node instead of your “CompareInt” node and plug the result of that into the branch.

Kind of like this except with all your inputs:

 ![41472-arrows3.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/3/5/b35192cfb703173299228f5fa82dec80352fd9a9.png)

Or like this:

 ![41473-arrows4.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/7/f/c7f775155c6a7967c18b4eb52b7e00dc5197b300.png)

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 30, 2018, 12:13am UTC](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518/20 "2018-01-30T00:13:14Z")

</div>

a quick addition, note that Ticks are hurting performance very badly when used constantly. you can bind/unbind events to tick on need, so it won’t have to check your stuff every freaking frame just to validate it doesn’t have to be executed now.

by binding/unbinding (or enabling/disabling Tick if u dont use it for anything less) you save yourself all that performance.

search youtube for “ue4 event dispatchers” for more info.

[Next page](https://forums.unrealengine.com/t/how-to-make-a-while-loop-with-key-presses/317518.md?page=2)
