diff --git a/Source/GameBaseFramework/Private/Characters/Components/GBFHeroComponent.cpp b/Source/GameBaseFramework/Private/Characters/Components/GBFHeroComponent.cpp index f1f0a831..22421000 100644 --- a/Source/GameBaseFramework/Private/Characters/Components/GBFHeroComponent.cpp +++ b/Source/GameBaseFramework/Private/Characters/Components/GBFHeroComponent.cpp @@ -117,11 +117,12 @@ void UGBFHeroComponent::HandleChangeInitState( UGameFrameworkComponentManager * pawn_ext_comp->InitializeAbilitySystem( player_state->GetGBFAbilitySystemComponent(), player_state ); } - if ( const auto * pc = GetController< AGBFPlayerController >() ) + if ( auto * pc = GetController< AGBFPlayerController >() ) { if ( pawn->InputComponent != nullptr ) { InitializePlayerInput( pawn->InputComponent ); + pc->OnPossessedPawnChanged.AddDynamic( this, &ThisClass::OnPossessedPawnChanged ); } // Hook up the delegate for all pawns, in case we spectate later @@ -255,6 +256,25 @@ void UGBFHeroComponent::OnRegister() } } +void UGBFHeroComponent::OnUnregister() +{ + Super::OnUnregister(); + + const auto * pawn = GetPawn< APawn >(); + if ( pawn == nullptr ) + { + return; + } + + auto * pc = pawn->GetController< APlayerController >(); + if ( pc == nullptr ) + { + return; + } + + pc->OnPossessedPawnChanged.RemoveDynamic( this, &ThisClass::OnPossessedPawnChanged ); +} + void UGBFHeroComponent::BindToRequiredOnActorInitStateChanged() { BindOnActorInitStateChanged( UGBFPawnExtensionComponent::NAME_ActorFeatureName, GBFTag_InitState_DataInitialized, false ); @@ -394,4 +414,31 @@ TSubclassOf< UGBFCameraMode > UGBFHeroComponent::DetermineCameraMode() const } return nullptr; +} + +void UGBFHeroComponent::OnPossessedPawnChanged( APawn * /*old_pawn*/, APawn * new_pawn ) +{ + if ( new_pawn == nullptr ) + { + BoundActionsByInputConfig.Empty(); + bReadyToBindInputs = false; + return; + } + + if ( new_pawn->InputComponent == nullptr ) + { + return; + } + + if ( bReadyToBindInputs ) + { + return; + } + + if ( !HasReachedInitState( GBFTag_InitState_DataInitialized ) ) + { + return; + } + + InitializePlayerInput( new_pawn->InputComponent ); } \ No newline at end of file diff --git a/Source/GameBaseFramework/Public/Characters/Components/GBFHeroComponent.h b/Source/GameBaseFramework/Public/Characters/Components/GBFHeroComponent.h index a59300e3..6d70ee99 100644 --- a/Source/GameBaseFramework/Public/Characters/Components/GBFHeroComponent.h +++ b/Source/GameBaseFramework/Public/Characters/Components/GBFHeroComponent.h @@ -62,6 +62,7 @@ class GAMEBASEFRAMEWORK_API UGBFHeroComponent : public UGBFPawnComponent protected: void OnRegister() override; + void OnUnregister() override; void BindToRequiredOnActorInitStateChanged() override; void InitializePlayerInput( UInputComponent * player_input_component ); void Input_AbilityInputTagPressed( FGameplayTag input_tag ); @@ -70,6 +71,9 @@ class GAMEBASEFRAMEWORK_API UGBFHeroComponent : public UGBFPawnComponent TSubclassOf< UGBFCameraMode > DetermineCameraMode() const; private: + UFUNCTION() + void OnPossessedPawnChanged( APawn * old_pawn, APawn * new_pawn ); + FSimpleMulticastDelegate::FDelegate OnPawnReadyToInitializeDelegate; // True when player input bindings have been applyed, will never be true for non-players