Listening to Input in PlayerController

I’m trying to listen to Input in PlayerController, similarly to how it was done in Character.

However it seems like it works differently, there’s no function similar to Character::SetupPlayerInputComponent, and I see there’s this function: Controller::PushInputComponent

How do I approach doing it?

1 Like

It is better to forward input event from Character to Controller.

Like this:



void AYourCharacter::DoSomething()
{
        Cast<AYourPlayerController>(Controller).DoSomething(Input);
}


or this:



void AYourCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
        // Set up gameplay key bindings
	check(InputComponent);
	InputComponent->BindAction("SomeAction", IE_Pressed, this, &AYourPlayerController::DoSomething);
}


But with Controller it works this way (not 100% sure):

YourPlayerController.cpp



void AYourPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	InputComponent->BindAction("SomeAction", IE_Pressed, this, &AYourPlayerController::DoSomething);
}

Yeah that’s it, thanks.

I’ve already brought up the issue of where input should be processed, if you have something to add please do:

Update:

The build itself is successful and it works for custom actions I make (e.g. attack), but any function related to BindAxis that is called from the controller causes in UE crashing.

The functions in TestCharacter remained untouched, they were just made public.

My speculation is that the input in the character is processed after the input in controller, possibly after some function that is crucial for the calculations to work.
So when I call functions that were normally supposed to be called when the character input is processed, when the controller input is processed, UE crashes as something is missing for it to work.

What do you think?

Still need an answer

Have you tried debugging where it crashed? My best guess from your code is that P is NULL either because the axis is beig processed before tick or that the player controller handles input at a point where it isn’t controlling a pawn. I’d just call GetControlledPawn in each function and check it for NULL and see if that resolves your crash.

You are right thanks, so I’m guessing the BindAxis processing occurs before the Tick and the BindAction occurs after? That’s why the latter worked.

It is more that Axis delegates are called every frame with a value of 0 if there is currently no input which could occur before the Pawn exists. Actions, on the other hand, only get called when you press the button so that won’t happen until you’re finished spawning in to the level and the controlled Pawn is fully valid.

I understand, thanks :slight_smile: