Focus management with UMG transitions in UE4
One little gotcha that isn't really well documented with manual keyboard/controller focus in UMG is that if a UI element is not visible when you attempt to set focus to it, that call to SetUserFocus silently fails.
What you're going to need to do is either set visibility first (easy enough) or if there's an animated transition that changes Visibility
, you're going to want to wait until that transition is complete before doing so.
I ended up making the following Blueprint macro to do this, it has a rather unwieldy name but I wanted to distinguish it from other macros I created for subwidget navigation that didn't require animation transitions :
Here's the actual implementation of the macro:
The Play Animation with Finished event
node is from 4.23 onwards, but its a simple enough cherry-pick to backport it if you're on an earlier version (GN was on 4.22 at the time).
The actual macro should be pretty self explanatory - you pass it the animation you want to play plus the direction and speed it should play back at, and a widget to focus after the animation is completed.
I've added a second exec output so that we can handle both the start of the animation and when it completes, after the focus call is made.
The Navigation Focus Widget
macro looks like the following:
Again it should be pretty self-explanatory, we just check if the widget supports focus and call SetUserFocus on it if it does.
Overall these simple macros make it much easier to use animation transitions when swapping between subscreens of a menu, as you can now guarantee that focus will only be set once the animation is complete (and a side benefit of this is that a player can't accidentally trigger the new button mid-transition, potentially before they've even seen it).