CogSubsystem: prevent creating multiple input component when having multiple player controller.

Typically when entering debug camera.
This commit is contained in:
Arnaud Jamin
2025-03-30 23:24:31 -04:00
parent bebc05d4fa
commit ac000a37c6
2 changed files with 42 additions and 33 deletions
+39 -30
View File
@@ -244,15 +244,18 @@ void UCogSubsystem::Tick(UWorld* InTickedWorld, ELevelTick InTickType, float InD
FCogImGuiContextScope ImGuiContextScope(Context); FCogImGuiContextScope ImGuiContextScope(Context);
UpdateServerPlayerControllers(*World); UpdatePlayerControllers(*World);
if (UPlayerInput* PlayerInput = (LocalPlayerController != nullptr) ? LocalPlayerController->PlayerInput : nullptr) if (auto* LocalPlayerControllerPtr = LocalPlayerController.Get())
{ {
//------------------------------------------------------------------ if (UPlayerInput* PlayerInput = LocalPlayerControllerPtr->PlayerInput)
// We must wait for the player input to be valid to disable {
// DebugExecBindings conflicting with our shortcuts. //------------------------------------------------------------------
//------------------------------------------------------------------ // We must wait for the player input to be valid to disable
TryDisableCommandsConflictingWithShortcuts(PlayerInput); // DebugExecBindings conflicting with our shortcuts.
//------------------------------------------------------------------
TryDisableCommandsConflictingWithShortcuts(PlayerInput);
}
} }
if (LayoutToLoad != -1) if (LayoutToLoad != -1)
@@ -284,50 +287,52 @@ void UCogSubsystem::RequestDisableCommandsConflictingWithShortcuts()
} }
//-------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------
void UCogSubsystem::SetLocalPlayerController(APlayerController* PlayerController) void UCogSubsystem::SetLocalPlayerController(APlayerController& PlayerController)
{ {
if (LocalPlayerController == PlayerController) if (LocalPlayerController.Get() == &PlayerController)
{ return; } { return; }
LocalPlayerController = &PlayerController;
InputComponent.Reset();
LocalPlayerController = PlayerController; if (UInputComponent* InputComponentPtr = NewObject<UInputComponent>(&PlayerController, TEXT("Cog_Input")))
if (LocalPlayerController == nullptr)
{ return; }
UInputComponent* InputComponentPtr = NewObject<UInputComponent>(PlayerController, TEXT("Cog_Input"));
InputComponent = InputComponentPtr;
if (InputComponentPtr)
{ {
PlayerController->PushInputComponent(InputComponentPtr); InputComponent = InputComponentPtr;
PlayerController.PushInputComponent(InputComponentPtr);
} }
for (FCogShortcut& Shortcut : Shortcuts) for (FCogShortcut& Shortcut : Shortcuts)
{ {
BindShortcut(Shortcut); BindShortcut(Shortcut);
} }
if (LocalPlayerController->PlayerInput != nullptr)
{
FCogImguiInputHelper::DisableCommandsConflictingWithShortcuts(*LocalPlayerController->PlayerInput);
}
} }
//-------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------
void UCogSubsystem::UpdateServerPlayerControllers(UWorld& World) void UCogSubsystem::UpdatePlayerControllers(UWorld& World)
{ {
if (APlayerController* PlayerController = GetGameInstance()->GetFirstLocalPlayerController())
{
SetLocalPlayerController(*PlayerController);
}
TArray<UCogDebugPluginSubsystem*> PluginSubsystems; TArray<UCogDebugPluginSubsystem*> PluginSubsystems;
ServerPlayerControllers.RemoveAll([] (TWeakObjectPtr<APlayerController> PlayerController) ServerPlayerControllers.RemoveAll([] (TWeakObjectPtr<APlayerController> PlayerController)
{ {
return PlayerController.IsValid() == false; return PlayerController.IsValid() == false;
}); });
for (FConstPlayerControllerIterator It = World.GetPlayerControllerIterator(); It; ++It) for (FConstPlayerControllerIterator It = World.GetPlayerControllerIterator(); It; ++It)
{ {
APlayerController* PlayerController = It->Get(); APlayerController* PlayerController = It->Get();
if (PlayerController == nullptr) if (PlayerController == nullptr)
{ continue; } { continue; }
if (PlayerController->IsLocalController())
{
SetLocalPlayerController(PlayerController);
}
if (World.GetNetMode() != NM_Client) if (World.GetNetMode() != NM_Client)
{ {
if (ServerPlayerControllers.Contains(PlayerController)) if (ServerPlayerControllers.Contains(PlayerController))
@@ -1062,7 +1067,11 @@ bool UCogSubsystem::BindShortcut(FCogShortcut& InShortcut) const
FInputKeyBinding InputBinding(*InputChord, IE_Pressed); FInputKeyBinding InputBinding(*InputChord, IE_Pressed);
InputBinding.KeyDelegate.GetDelegateForManualSet() = InShortcut.Delegate; InputBinding.KeyDelegate.GetDelegateForManualSet() = InShortcut.Delegate;
InputComponent.Get()->KeyBindings.Add(InputBinding);
if (UInputComponent* InputComponentPtr = InputComponent.Get())
{
InputComponentPtr->KeyBindings.Add(InputBinding);
}
InShortcut.InputChord = *InputChord; InShortcut.InputChord = *InputChord;
@@ -1078,13 +1087,13 @@ void UCogSubsystem::RebindShortcut(const UCogCommonConfig& InConfig, const FProp
UInputComponent* InputComponentPtr = InputComponent.Get(); UInputComponent* InputComponentPtr = InputComponent.Get();
if (InputComponentPtr == nullptr) if (InputComponentPtr == nullptr)
{ return; } { return; }
for (auto& KeyBinding : InputComponentPtr->KeyBindings) for (auto& KeyBinding : InputComponentPtr->KeyBindings)
{ {
KeyBinding.KeyDelegate.Unbind(); KeyBinding.KeyDelegate.Unbind();
} }
InputComponent.Get()->KeyBindings.Empty(); InputComponentPtr->KeyBindings.Empty();
for (FCogShortcut& Shortcut : Shortcuts) for (FCogShortcut& Shortcut : Shortcuts)
{ {
BindShortcut(Shortcut); BindShortcut(Shortcut);
+3 -3
View File
@@ -117,7 +117,7 @@ protected:
virtual void TryInitialize(UWorld& World); virtual void TryInitialize(UWorld& World);
virtual void UpdateServerPlayerControllers(UWorld& World); virtual void UpdatePlayerControllers(UWorld& World);
virtual void InitializeWindow(FCogWindow* Window); virtual void InitializeWindow(FCogWindow* Window);
@@ -133,7 +133,7 @@ protected:
virtual void RenderMenuItemHelp(FCogWindow& Window); virtual void RenderMenuItemHelp(FCogWindow& Window);
void SetLocalPlayerController(APlayerController* PlayerController); void SetLocalPlayerController(APlayerController& PlayerController);
virtual void ToggleInputMode(); virtual void ToggleInputMode();
@@ -183,7 +183,7 @@ protected:
mutable TArray<TObjectPtr<const UObject>> Assets; mutable TArray<TObjectPtr<const UObject>> Assets;
TArray<TWeakObjectPtr<APlayerController>> ServerPlayerControllers; TArray<TWeakObjectPtr<APlayerController>> ServerPlayerControllers;
TWeakObjectPtr<APlayerController> LocalPlayerController; TWeakObjectPtr<APlayerController> LocalPlayerController;
TWeakObjectPtr<UInputComponent> InputComponent; TWeakObjectPtr<UInputComponent> InputComponent;