How do I read input axes from C++?

How can I read input axes from C++ code without setting up events? In Blueprint I can just read the axis values with the Get Axisname node, however I’m not able to get these values with the C++ API.

Any of the following lines I tried always return 0 in both a APawn and a APlayerController inheritance.

InputComponent->GetAxisValue(TEXT("Accelerate"));
GetInputAxisValue(TEXT("Accelerate"));
InputComponent->GetAxisKeyValue(TEXT("W"));
GetInputAxisKeyValue(TEXT("W"));

Accelerate is set up as an AxisMapping in the project settings with W and S with scales of 1.0 respective -1.0. Auto Receive Input is set to Player 0 in the Blueprint instance of the actor.

What do I have to do to be able to read input axes, preferably from a PlayerController?

I had the same question, and this is not the answer because he asks how to do that without setting up events (i.e. in Tick).
Also I think there is no need to multiply by DeltaSeconds in LookUpAtRate, because when adding an Input, the engine resolves the movement by itself having into account DeltaSeconds (although i’m not 100% sure, it should be that way, because when adding movement input to a pawn, it works the same way)
I don’t think it is a good idea to handle anything that uses DeltaSeconds in the Input delegates, maybe is better to store in a variable and use that value in Tick

It is actually possible, you have to bind, but you can get the input in Tick,
if you use InputComponent->GetAxisValue(TEXT("Accelerate"));, you need to put InputComponent->BindAxis(TEXT("Accelerate")); in your SetupInputComponent, so it registers that axis value so you can read it.

Hey guys,

Unfortunately you can just get key from InputComponent without key binding.
If I correctly understand you, you want to do something like this:

// PlayerInput exist in PlayerController
if (PlayerInput)
{
	float BValue = PlayerInput->GetKeyValue(EKeys::B);
	// to do smth
}

Best regards,

oops, i didn’t see the “without setting up events” part. i misunderstood the question. and as for the DeltaSeconds part, that’s how DefaultPawn handles it for some reason. i thought it seemed strange when i copied it as an example. so i guess DefaultPawn is almost frame independent, but not quite.

you’re right, it is better to use DeltaSeconds from within an event called by tick… but maybe since axis inputs are updated very often, they might actually be called by tick. but if that’s true, i wonder why they don’t have a DeltaSeconds parameter passed into axis events… maybe to optimize function parameter passing overhead?

Yes, this is explained in the API for the 1st overload of BindAxis here. If you followed programming tutorials, you probably know the 2nd overload, with delegate functions.