Why do I need to disable input from the pawn instead of the player controller?

This question is not a bug-hunt. So there is no need to look for typos or what-not. I’m trying to understand conceptually what is going on.

Regarding the c++ code to disable user input for a pawn that is being controlled by a contoller…
I have the following code in “MyPlayerController.cpp” and i just cant figure out why i need to enable and disable from the pawn, instead of the controller. (the enable/disabling of input exists on the actor class which is a parent of both the pawn and the playercontroller)

Why does this not work?

#include "MyPlayerController.h"

void AMyPlayerController::SetPlayerEnabledState(bool p_bEnabled)
{
	if (p_bEnabled)
	{
		EnableInput(this);
	}
	else
	{
		DisableInput(this);
	}
	bShowMouseCursor = p_bEnabled;
}

But this does:

#include "MyPlayerController.h"

void AMyPlayerController::SetPlayerEnabledState(bool p_bEnabled)
{
	if (p_bEnabled)
	{
		GetPawn()->EnableInput(this);
	}
	else
	{
		GetPawn()->DisableInput(this);
	}
	bShowMouseCursor = p_bEnabled;
}

For an understanding of the background to this question i’ve included some relevant info below.

**Class Heirachy:**
Actor
	Controller
		PlayerController
			MyPlayerController
				BP_MyPlayerController



Actor
	Pawn
		MyBasePawn
			MyTank
				BP_MyTank




**Gamemode:**
Default Pawn Class: BP_MyTank
Player Controller Class: BP_MyPlayerController







**Actor.h**
	/** 
	 * Pushes this actor on to the stack of input being handled by a PlayerController.
	 * @param PlayerController The PlayerController whose input events we want to receive.
	 */
	UFUNCTION(BlueprintCallable, Category="Input")
	virtual void EnableInput(class APlayerController* PlayerController);


	/** 
	 * Removes this actor from the stack of input being handled by a PlayerController.
	 * @param PlayerController The PlayerController whose input events we no longer want to receive. If null, this actor will stop receiving input from all PlayerControllers.
	 */
	UFUNCTION(BlueprintCallable, Category="Input")
	virtual void DisableInput(class APlayerController* PlayerController);




**Actor.cpp**

void AActor::EnableInput(APlayerController* PlayerController)
{
	if (PlayerController)
	{
		// If it doesn't exist create it and bind delegates
		if (!InputComponent)
		{
			InputComponent = NewObject<UInputComponent>(this, UInputSettings::GetDefaultInputComponentClass());
			InputComponent->RegisterComponent();
			InputComponent->bBlockInput = bBlockInput;
			InputComponent->Priority = InputPriority;

			UInputDelegateBinding::BindInputDelegatesWithSubojects(this, InputComponent);
		}
		else
		{
			// Make sure we only have one instance of the InputComponent on the stack
			PlayerController->PopInputComponent(InputComponent);
		}

		PlayerController->PushInputComponent(InputComponent);
	}
}

void AActor::DisableInput(APlayerController* PlayerController)
{
	if (InputComponent)
	{
		if (PlayerController)
		{
			PlayerController->PopInputComponent(InputComponent);
		}
		else
		{
			for (FConstPlayerControllerIterator PCIt = GetWorld()->GetPlayerControllerIterator(); PCIt; ++PCIt)
			{
				if (APlayerController* PC = PCIt->Get())
				{
					PC->PopInputComponent(InputComponent);
				}
			}
		}
	}
}

InputComponent is on AActor, meaning APlayerController and APawn would have their own input components. Which one did you use to BindAxis/Actions?

1 Like

Oh wow! Haha exactly what I needed to hear. Yes I made my bindings on the pawn class ( or one of its children).

So hypothetically it would not be strange to have instead bound the Input to the controller? I guess somehow the conteolwr class would then need to get a reference to my pawn in order to say, implement the movement… which would have added complexity to the training video…. Yep th aks mate that makes sense.

I think it would make sense to do pawn specific inputs on the pawn and the pawn agnostic things in the controller. For example pausing the game / opening the menu, to me makes sense to be on the controller.

For example with a game like GTA where you can possess different pawns for character and car. It would be strange to have to add a main menu binding for each pawn.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms