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
+34 -25
View File
@@ -244,15 +244,18 @@ void UCogSubsystem::Tick(UWorld* InTickedWorld, ELevelTick InTickType, float InD
FCogImGuiContextScope ImGuiContextScope(Context);
UpdateServerPlayerControllers(*World);
UpdatePlayerControllers(*World);
if (UPlayerInput* PlayerInput = (LocalPlayerController != nullptr) ? LocalPlayerController->PlayerInput : nullptr)
if (auto* LocalPlayerControllerPtr = LocalPlayerController.Get())
{
//------------------------------------------------------------------
// We must wait for the player input to be valid to disable
// DebugExecBindings conflicting with our shortcuts.
//------------------------------------------------------------------
TryDisableCommandsConflictingWithShortcuts(PlayerInput);
if (UPlayerInput* PlayerInput = LocalPlayerControllerPtr->PlayerInput)
{
//------------------------------------------------------------------
// We must wait for the player input to be valid to disable
// DebugExecBindings conflicting with our shortcuts.
//------------------------------------------------------------------
TryDisableCommandsConflictingWithShortcuts(PlayerInput);
}
}
if (LayoutToLoad != -1)
@@ -284,32 +287,39 @@ void UCogSubsystem::RequestDisableCommandsConflictingWithShortcuts()
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogSubsystem::SetLocalPlayerController(APlayerController* PlayerController)
void UCogSubsystem::SetLocalPlayerController(APlayerController& PlayerController)
{
if (LocalPlayerController == PlayerController)
if (LocalPlayerController.Get() == &PlayerController)
{ return; }
LocalPlayerController = PlayerController;
LocalPlayerController = &PlayerController;
InputComponent.Reset();
if (LocalPlayerController == nullptr)
{ return; }
UInputComponent* InputComponentPtr = NewObject<UInputComponent>(PlayerController, TEXT("Cog_Input"));
InputComponent = InputComponentPtr;
if (InputComponentPtr)
if (UInputComponent* InputComponentPtr = NewObject<UInputComponent>(&PlayerController, TEXT("Cog_Input")))
{
PlayerController->PushInputComponent(InputComponentPtr);
InputComponent = InputComponentPtr;
PlayerController.PushInputComponent(InputComponentPtr);
}
for (FCogShortcut& Shortcut : Shortcuts)
{
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;
ServerPlayerControllers.RemoveAll([] (TWeakObjectPtr<APlayerController> PlayerController)
@@ -323,11 +333,6 @@ void UCogSubsystem::UpdateServerPlayerControllers(UWorld& World)
if (PlayerController == nullptr)
{ continue; }
if (PlayerController->IsLocalController())
{
SetLocalPlayerController(PlayerController);
}
if (World.GetNetMode() != NM_Client)
{
if (ServerPlayerControllers.Contains(PlayerController))
@@ -1062,7 +1067,11 @@ bool UCogSubsystem::BindShortcut(FCogShortcut& InShortcut) const
FInputKeyBinding InputBinding(*InputChord, IE_Pressed);
InputBinding.KeyDelegate.GetDelegateForManualSet() = InShortcut.Delegate;
InputComponent.Get()->KeyBindings.Add(InputBinding);
if (UInputComponent* InputComponentPtr = InputComponent.Get())
{
InputComponentPtr->KeyBindings.Add(InputBinding);
}
InShortcut.InputChord = *InputChord;
@@ -1083,7 +1092,7 @@ void UCogSubsystem::RebindShortcut(const UCogCommonConfig& InConfig, const FProp
{
KeyBinding.KeyDelegate.Unbind();
}
InputComponent.Get()->KeyBindings.Empty();
InputComponentPtr->KeyBindings.Empty();
for (FCogShortcut& Shortcut : Shortcuts)
{
+2 -2
View File
@@ -117,7 +117,7 @@ protected:
virtual void TryInitialize(UWorld& World);
virtual void UpdateServerPlayerControllers(UWorld& World);
virtual void UpdatePlayerControllers(UWorld& World);
virtual void InitializeWindow(FCogWindow* Window);
@@ -133,7 +133,7 @@ protected:
virtual void RenderMenuItemHelp(FCogWindow& Window);
void SetLocalPlayerController(APlayerController* PlayerController);
void SetLocalPlayerController(APlayerController& PlayerController);
virtual void ToggleInputMode();