Hello everyone,
After countless hours spent trying to fix this issue, I come here looking for some help.
For as far I can remember, I have a bug in my game which causes my player character to stop moving or start moving sluggishly when interacting with a Common User Widget. Today, I found a workaround, but still not fully fixed.
I’m using enhanced input actions for my character movement which I bind as follows:
HWInputComponent->BindNativeAction(InputConfig, GameplayTags.InputTag_Move, ETriggerEvent::Triggered, this, &ThisClass::Input_Move, /*bLogIfNotFound=*/ false);
And then I have some common widget classes for a spell UI, where I load some custom List View entries and these have different actions when I press them with the left mouse button.
Now, when I’m running the game in a viewport. If I’m moving with my character by pressing W, and I press the Widget the list view entry specifically (nothing else in the widget causes this) then the character starts moving “slower”. It’s like it stops on a tick and moves on the next one, on a loop. The only thing that fixes this is if I release the key and press it again, or click on the viewport (outside the widget).
After different debuggings, I came to the conclusion that for some reason, some of my keyboard input gets consumed by the widget after focusing it when clicking it, but a ghost or some keyboard input still reaches the Input_Move action. Which causes this weird movement.
I found this cause I’m working in a 2d game, where I need my character to accelerate and desaccelerate as soon as I press/release the keys, and found that my Input_Move function, which I undestand that runs every tick, was still being called with the particularity that one time the function was called the velocity was max (350) and on the next call the velocity was 0, on a loop.
I’ve tried everything I could think of, and everything I found on the internet related to this, but nothing worked or got it even worse. I tried:
- Testing Set Input Mode (the 3 options, Game Only, UI Only, Game and UI Only)
- DefaultInput - bShouldFlushPressedKeysOnViewportFocusLost true/false
- Overriding OnFocusReceived in every widget (and childs) to set focus back to viewport
- Created custom classes to override common user widget native functions and return FReplay of Unhandled (or Handled). Did it in blueprints aswell.
- Set widget and all childer Is Focusable to false
- Couldn’t find any option to disable completely keyboard input in widgets (I just need to click on them)
For example, added these overrides in all my widgets:
FReply UHWCommonSpellBookWidget::NativeOnPreviewKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent)
{
// Immediately return unhandled to avoid consuming any key event
UE_LOG(LogTemp, Warning, TEXT("UHWCommonSpellBookWidget - NativeOnPreviewKeyDown - Called"));
return FReply::Unhandled();
}
FReply UHWCommonSpellBookWidget::NativeOnKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent)
{
UE_LOG(LogTemp, Warning, TEXT("UHWCommonSpellBookWidget - NativeOnKeyDown - Called"));
return FReply::Unhandled();
}
FReply UHWCommonSpellBookWidget::NativeOnKeyUp(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent)
{
UE_LOG(LogTemp, Warning, TEXT("UHWCommonSpellBookWidget - NativeOnKeyUp - Called"));
return FReply::Unhandled();
}
bool UHWCommonSpellBookWidget::NativeSupportsKeyboardFocus() const
{
return false;
}
FReply UHWCommonSpellBookWidget::NativeOnFocusReceived(const FGeometry& InGeometry, const FFocusEvent& InFocusEvent)
{
UE_LOG(LogTemp, Warning, TEXT("UHWCommonSpellBookWidget - NativeOnFocusReceived - Called"));
return FReply::Unhandled();
}
And as soon as I press the list view entry, all this gets dumped in the logs until i release the key:
LogTemp: Warning: UHWSkillEntry - NativeConstruct - Called
LogTemp: Warning: UHWSkillEntry - NativeConstruct - Called
LogTemp: Warning: UHWSkillEntry - NativeOnFocusReceived - Called
LogBlueprintUserMessages: [BP_ThirdPersonCharacter_C_0] Server: Giving Ability
LogBlueprintUserMessages: [BP_ThirdPersonCharacter_C_0] Server: Hello
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnPreviewKeyDown - Called
LogTemp: Warning: UHWSkillEntry - NativeOnPreviewKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnPreviewKeyDown - Called
LogTemp: Warning: UHWSkillEntry - NativeOnPreviewKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnPreviewKeyDown - Called
LogTemp: Warning: UHWSkillEntry - NativeOnPreviewKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnKeyDown - Called
LogTemp: Warning: UHWCommonSpellBookWidget - NativeOnKeyDown - Called
Tried creating a custom InputProssesor (UE4 detecting which input method was last used by each player · GitHub) to try to avoid consuming these key down inputs but nothing helps.
The ListViewBase takes focus over the viewport, and keyboard inputs get consumed by the widget no matter what I do.
In order to prevent this from happening, I had to reenable deceleration so my character doesn’t slow down when pressing the widget. (for gameplay, I need to press my inventory while I keep moving). But some of the issues still persists as:
If I click on the widget while moving up (holding W), which causes the issue and I press another key (D) while still holding, then my character only goes right and if I release D, it fully stops.
Hope anyone gets an idea on this, and please feel free to suggest or ask anything. There’s probably a lot of more information I may have cut out.
Thank you for taking the time to look at this!
And tried several other things I cannot remember at the moment.