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);
}
}
}
}
}