The location of the character’s bindings in the code
Hello everyone
I’ve decided to tidy up the player’s code and I’m not sure where all the bindings should go—in the Character class or in the PlayerController class (for single-player)
I've got a very strange implementation at the moment:
//MainCharacter.h
////////////
///Input
private:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* JumpAction;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* MoveAction;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
UInputAction* LookAction;
protected:
TObjectPtr<AMainPlayerController> PC;
//MainCharacter.cpp
void AMainCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
// Move
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMainCharacter::Move);
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Completed, this, &AMainCharacter::MoveCompleted);
// Look
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMainCharacter::Look);
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Completed, this, &AMainCharacter::LookCompleted);
// Jump
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &AMainCharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &AMainCharacter::StopJumping);
}
}
void AMainCharacter::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
PC = Cast<AMainPlayerController>(NewController);
}
void AMainCharacter::UnPossessed()
{
PC = nullptr;
Super::UnPossessed();
}
//MainPlayerController.cpp
void AMainPlayerController::BeginPlay()
{
Super::BeginPlay();
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
for (TObjectPtr<UInputMappingContext>& MappingContext: DefaultMappingContexts)
{
Subsystem->AddMappingContext(MappingContext, 0);
}
}
}
I'm completely confused😐
So I’d like to know how to organise everything properly and where, for example, I can create binds for my character’s abilities?
Thx.