Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Getting Input Key Time Down in C++

Hey there!

I hope you guys can help me with this. First of all sorry for my english, I'll try to explain as good as possible.

I have a MyDoor class in my project whose father is an UsableItem class. I want my player (own coded player which have no controller), whose father is ACharacter to close the door if he holds down the "use button" more than 1.5 seconds. The problem is, I cannot access to the Get Input Key Time Down function because the target is a Player Controller and I don't have one of those hehe (or that's what I think).

I've tried some solutions but none of them worked such as: -Casting the ACharacter to MyPlayer in order to get the controller -Adding the generic controller class to MyDoor to get the function -I want to try to create my own timer, but I don't know how to get the key I press because I'm overriding the OnUsed function from UsableItem.

Hierarchy: ACharacter --> MyPlayer UsableItem --> MyDoor

Could you help me out, please? If you want, I can post screenshots of my code, but I don't think it's necessary.

Thank so much in advance.

Regards, j0s3m4.

Product Version: UE 4.14
more ▼

asked Mar 01 '17 at 10:16 AM in C++ Programming

avatar image

5 4 3 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first


try the function GetPlayerController: As world context object you can use anything that implements GetWorld(), for instance ACharacter::GetWorld(), and as index use 0. Note that this function returns NULL during actor construction so if you are calling it in a tick make sure to check for NULL. You did not specify whether this is a multiplayer game or not. If it is, be advised that obtaining the player controller like this will only work on clients and non-dedicated servers. If the code you are writing might potentially run on both client and server, call IsLocalPlayer() on the player controller.

With that answered, I'd just like to note that every player-controlled pawn always has a player controller. For me it does not really come through as to what is the player controlled pawn; however, on whichever pawn that is you can just call GetController() on the pawn and cast it to a player controller safely. Casting your pawn to a player controller as you suggested won't work because the classes are unrelated.

Cheers, Univise

more ▼

answered Mar 01 '17 at 12:15 PM

avatar image

201 9 10 22

avatar image j0s3m4 Mar 01 '17 at 12:38 PM

Hey Univise, thank you so much for helping me out and for your quick answer!

I tried that you said and FINALLY I could get the PlayerController and in consequence, the GetInputKeyTimeDown() function. And to clarify something, my game is a non-multiplayer game. We just control one player. And yes, I realized that last thing you said about casting my player to a controller and OF COURSE, I can't do that haha.

My problem now is that I'm overriding the OnUsed function from my UsableItem in MyDoor class and I cannot get the time I hold down my "Spacebar" key. I'm trying this:

 bool AMyDoor::OnUsed(ACharacter* character)
    if (StatesEnum == EDoorStates::DS_POSHALFOPENED && GetWorld()->GetFirstPlayerController()->GetInputKeyTimeDown(FKey("Spacebar")) == 2.0)
         StatesEnum = EDoorStates::DS_CLOSED;
         return true;
                 return false;

This is: if my door is halfopened and I press spacebar during 2 seconds, close the door.

But it's not working and I don't know why.

Again, thank you so much for your help!

avatar image j0s3m4 Mar 07 '17 at 09:42 AM

Ok, solved!

My problem was that I was trying to get the time I press a key in an "OnUsed" event. And that only triggers when I press (not release) the key. My solution was to change the InputEvent to "Release" and calculate that time in the Tick(). Once I had that it was easy to create the logic in the OnUsed function.

Thank you so much for your help, Univise!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

As addition to the accepted answer by Univise, that's how you access it:


documentation of the method.: http://api.unrealengine.com/INT/API/Runtime/Engine/GameFramework/UPlayerInput/GetKeysForAction/index.html

more ▼

answered Mar 20 '18 at 12:43 PM

avatar image

919 21 46 64

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question