Get Input Key Time Down

Hi,

I have some small Blueprint here which is calling function Throw. Throw function comes from C++ and takes one parameter Power, and Power how I want it to work is to be based on the amount of time that key is being held (the longer key is pressed, the bigger value of Power). To achieve that I used Get Input Key Time Down, but it does not work. Helper function Print always prints 0. Does someone knows why?

Cheers,
szwarc

Something like this looks like it could help you

Releasing the key triggers the print or any other function, and the variable now contains the time, the key has been held down.

EDIT: even nicer: you could enable tick only if key gets pressed, so removing the need for unnecessary ticking and saving the gate node

1 Like

Thanks. I just checked your 2nd solution, unfortunately it does not work for me. I still get 0s. Also, even the simples Blueprint prints out 0s no matter how long i hold the key:

I suspect the logic from my first post in general is ok, and all these Gates etc. are working correctly. Only problem seems to be with Get Input Key Time Down, which for some reason doesn’t work. I suspect it might be related to the issue from last part (starts at 2:30) of this video:

However, my case is different (I do not have any BP logic in the level and I don’t know how to set up my Player Controller correctly).

Hey @szwarc9311, how do you find this approach? It uses Timers to get how long something happened here! So every second the Timer fires off the “IncrementHoldTime” Event which +1 the HoldTime Variable. On release, it gets the elapsed time since the last loop and adds that float to the Holdtime, ends the Timer and does something, here, it PrintStrings the Holdtime for prove.

Edit:

If you want to use the HoldTime in f.e. a progress bar, you could lower the Timer delay for better responsiveness and a neater Code:

Mind the delay as it could cost performance if set too low. :slight_smile:

1 Like

To clarify what’s happening, I think the reason you’re getting “Time down = 0” is because you’re triggering on release, and at that point the key is not held down at all, it’s released, so time down will be 0.

I think the simplest modification to your setup would be to just store “current game time in seconds” when you press down the button, and when you release the button subtract “current game time in seconds” from that saved time.

Hi there,

I tested @herb64 solution and it works, that may help you, you don’t need to setup a custom player controller, just the basic class in your game mode will work.



image

hi @szwarc9311 , for some reason there was no notification about your answer.
The code snippet you pasted does not process any “Pressed” key event at all. So this will definitely print 0.0 - because when getting time on “Released” this of course returns 0. The important part in my second variant is to enable ticking and keep value stored in a variable at times, while the key is pressed and just disable when releasing the key.
The relevant part in Matthew Wadsteins video you mention is about receiving input.
The only important part is of course, that you can receive input - but you already did setup this in your original blueprint with your input events - so the code I showed you should work if you paste it in there.

Thank you all. It works now.

Hey @szwarc9311 , Why did you choose this option with the Tick Enabled over my solution? Did it bring you better performance? I would rather Not Tick this Everytime Because I expect you to not throw all the time tbh…

I used L.F.A’s solution.

Ain‘t that too inperformant Because of the Tick?

To be honest I just used the solution that i could almost literally copy/paste into my project. Your solution for example has Custom Event created, which I do not know how should look like in my case. I will try to get back to this problem later, after getting more experience with the engine.

1 Like

Alright, good to know. I was just wondering myself what could’ve been wrong with mine.

I’ve been trying to use this same node without having to use Tick and I just found an amazing solution in this video tutorial. Maybe it will help you all.

1 Like

You could also setup your Input Keys to activate Events and Functions this way. You would have to create specific Booleans for each Key. I hope you all find this helpful.