Hey guys, I am working on my first implementation of UI and running into an issue. I have it set up right now so whatever button is bound to my “inventoryButtonPressed” function will of course open up the players menu, Originally I had set up the logic so that if the menu is closed it adds it to the viewport, and then sets up our input to use FInputModeUIOnly. The issue now arises that we cannot get back to this funciton again to run the close out inventory logic because that funciton exists in the gameonly input.
I’ve seen people getting around this with blueprints using keydown events, I do not want to use bp unless absolutely necessary as there will be many keybinds and for obvious reason i would rather stick to C++ as often as possible with the potentially heavy UI logic I may be placing in the future.
I attempted to override a keydown event Inside of my widget, and although I thought the idea was sound in theory it has the same issue, that code still cannot be hit. I found some really old thread saying there hadn’t been a good solution to his from the engine yet but that was back in 2015. There’s gotta be something more reasonable for it now right? Even if the Keydown event would work, it seems messy and bad practice to implement it this way rather than how my typical control bindings which eventually the player could set and are rather ActionBindings than Keybindings. Anyone have any ideas?
Well should anyone else need this information I did not find any sort of optimal solution, however for my use case it appears acceptable to use the InputModeGameAndUI
void APlayerCharacter::InventoryButtonPressed()
{
//log inventory button pressed
UE_LOG(LogTemp, Warning, TEXT("Inventory Button Pressed"));
if (InventoryWidget)
{
APlayerController* PlayerController = Cast<APlayerController>(GetController());
if (InventoryWidget->IsInViewport())
{
//log yeah boi
UE_LOG(LogTemp, Warning, TEXT("Inventory Widget is in Viewport"));
InventoryWidget->RemoveFromParent();
// Set the input mode back to game only
if (PlayerController)
{
FInputModeGameOnly InputModeData;
PlayerController->SetInputMode(InputModeData);
PlayerController->bShowMouseCursor = false;
}
}
else
{
InventoryWidget->AddToViewport();
// Set the input mode to UI only
if (PlayerController)
{
FInputModeGameAndUI InputModeData;
InputModeData.SetWidgetToFocus(InventoryWidget->TakeWidget());
InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
PlayerController->SetInputMode(InputModeData);
PlayerController->bShowMouseCursor = true;
}
}
}
}
Now of course this does mean that I will probably need to double map all of my keybinds, so when the function for the button press occurs i will first need to check whether the viewport is open and then run the necessary command based off of that, but it’s far superior in my eyes than placing all of this inside a BP system.