Im Currious to know why the code works this way? So I have two Questions on the behaviour of it
#1. Is SetupPlayerInputComponent()
Called before BeginPlay()
.
Because I was trying to add a bIsDead
boolean in SetupPlayerInputComponent()
before PlayerInputComponent BindAction()
or BindAxis()
& it would only return nullptr
Note that bIsDead
is being stored in my PlayerController class (Makes sense for my game design)
Code for Question #1:
.h
private:
class AGW_PlayerController* GW_PlayerController;
.cpp
// #include for AGW_PlayerController is @ top of file
void ACharacterBase::BeginPlay()
{
Super::BeginPlay();
GW_PlayerController = Cast<AGW_PlayerController>(GetController());
UE_LOG(LogTemp, Warning, TEXT("Controller Is : %s"), *GetController()->GetActorNameOrLabel());
}
void ACharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up gameplay key bindings
check(PlayerInputComponent);
// Early Return if Player is Dead = true
if (GW_PlayerController == nullptr) { return; }
if (GW_PlayerController->bIsDead) { return; }
// Bind jump events
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
// Bind movement events
PlayerInputComponent->BindAxis("Move Forward / Backward", this, &ACharacterBase::MoveForward);
PlayerInputComponent->BindAxis("Move Right / Left", this, &ACharacterBase::MoveRight);
PlayerInputComponent->BindAxis("Turn Right / Left Mouse", this, &ACharacterBase::TurnRight);
PlayerInputComponent->BindAxis("Look Up / Down Mouse", this, &ACharacterBase::LookUp);
PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &ACharacterBase::StartSprinting);
PlayerInputComponent->BindAction("Sprint", IE_Released, this, &ACharacterBase::StopSprinting);
}
Question #2 is more of an add on to Question #1
#2. Why does Moving bIsDead
to character class work only sort of?
When moving this code into Character class from playerController Class.
.h
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool bIsDead = false;
And changing.
.cpp
if (bIsDead) { return; }
Why does it only work when setting bIsDead
true before playing in editor. Whereas if I try and turn it true while playing it doesn’t work?
I did end up getting question #1 to work by moving
if (GW_PlayerController && GW_PlayerController == nullptr) { return; }
into each the main movement function in SetupPlayerInputComponent()
e.g.
void ACharacterBase::LookUp(float Value)
{
if (GW_PlayerController && GW_PlayerController->bIsDead) { return; }
AddControllerPitchInput(Value * MouseSens * GetWorld()->GetDeltaSeconds());
}
void ACharacterBase::TurnRight(float Value)
{
if (GW_PlayerController && GW_PlayerController->bIsDead) { return; }
AddControllerYawInput(Value * MouseSens * GetWorld()->GetDeltaSeconds());
}