It's often pretty useful to be able to access UMG-created animations from C++. Especially as you move more and more logic from Blueprints into C++, UMG animations can seem like the last hold-out.
New Method
UPDATE: The method I described in an old version of this tutorial is now not needed! There is now a meta
property that does all the work for us!
BindWidget
and BindWidgetOptional
are undocumented meta
properties that we can use to access our animations from C++.
Add variables to the header of your Blueprint's parent C++ class, as shown in the code below.
This works in very much the same way as the BindWidget
meta property that we can use to hook up C++ to Blueprint widgets.
As of Unreal Engine 5.0 you must mark your properties as Transient
.
Old Method
NOTE: This method is deprecated, you just need to do the BindWidget
method above instead!
With a custom subclass of UUserWidget
, we can create a map of FName
to UWidgetAnimation*
and access the animations by name from C++.
Let's see how to do this:
Notes:
- Our
PlayAnimationByName
isn't entirely needed, but it can be a helpful wrapper for the regularPlayAnimation
function