Optimization The Animation Trigger System | Tech Discussion


In a previous blog post I explained how I replicated Unity’s Animator.SetTrigger() functionality in UE4. The way I accomplished this was by creating an actor component attached to the player blueprint called AnimationTriggers which contained a publicly accessible boolean for each animation the player blueprint might wish to trigger (Attack/Roll/StepBack/etc.). The player’s animation blueprint then checked these booleans every frame to see if they had been set to true. If they had been, then the animation blueprint would respond to the trigger and try to play the desired animation using its animation state machine and also reset the trigger back to false. 

Here are some diagrams from showing the design of this system:

[From our tech document]

[From our tech document]

[From our tech document]

The problem with this design, however, is that the animation graph wastes a lot of CPU time by checking every frame if any animation triggers have been set. 

It would make much more sense if these triggers were only set when the actual triggering occurs! 

I learned about a feature in Unreal I was not previously aware of which provided an excellent solution to this problem: the blueprint interface. Blueprint interfaces can be created by right clicking in the content browse and choosing Blueprints/Blueprint Interface.

Blueprint interfaces only provide function declarations for child blueprints to implement. So I then created a blueprint interface BPI_AnimationTriggerable, and set my AnimBP_MythicismPlayer to implement this interface. This interface can then replace the AnimationTriggers component on the player, allowing triggers to be called directly rather than wasting time on if-checks every frame. 

Here’s an example of the refactor…


Before:

Trigger set from player blueprint

Trigger response from animation blueprint (run every frame)


After:

Trigger set from player blueprint

Trigger response in animation blueprint (only when called!)

So there you go: a simple optimization to triggering animations in UE4 that helps tidy up the implemenation. 


Author: Reilly da Silva

Files

Build_4-29-22.zip 863 MB
Apr 29, 2022

Get Mythicism

Leave a comment

Log in with itch.io to leave a comment.