How to make a while loop with key presses

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!

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

Why not run the loop without keypress

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

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.

No, to have release, you must press first.

Run it of key pressed, not release.


Unless all 4 keys are pressed at once

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


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

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?

Gate might help

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

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.

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

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.

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

You need to this a completely different way

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.


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

May need to clean this post up.

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

Then in your Populate Arrows do something like this:

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.

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

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

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?

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:

Or like this:

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.