Animation Blueprint, Implement Custom C++ Logic Via Tick Updates

From Epic Wiki
Jump to: navigation, search


Dear Community,

Extending AnimInstance is a wonderful way to create all sorts of custom animation systems and also efficiently utilize the existing UE4 anim blueprint functions.

It's very useful to know how you can do certain actions when the Anim Instance for a Character is first created, and how to do actions every tick while the anim instance exists!

Below are the two Anim Instance functions that are similar to PostInitializeComponents and Tick(DeltaTime) for Actors

virtual void NativeInitializeAnimation() OVERRIDE; 
virtual void NativeUpdateAnimation(float DeltaTimeX) OVERRIDE;

Example: An Anim-BP-Accessible Variable: IsMoving

Here is a complete example that obtains the Owning Pawn in the Init function (to avoid reobtaining the reference every tick).

Then, every tick / moment of the anim instance's existance, the boolean variable IsMoving is updated to reflect the velocity of the Owning pawn.

IsMoving can now be obtained from within the Anim Blueprint because of its UPROPERTY settings, and used in your animation blueprint logic.



  By Rama

#pragma once

#include "VictoryAnimInstance.generated.h"

UCLASS(transient, Blueprintable, hideCategories=AnimInstance, BlueprintType)
class UVictoryAnimInstance : public UAnimInstance
        /** Is Moving */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Movement)
	bool IsMoving;

//init and tick
	APawn * OwningPawn;
	virtual void NativeInitializeAnimation() override;
	virtual void NativeUpdateAnimation(float DeltaTimeX) override;


#include "VictoryGame.h"
#include "VictoryAnimInstance.h"

// UVictoryAnimInstance

//This function is like PostInitAnimtree in UE3
void UVictoryAnimInstance::NativeInitializeAnimation()
	//Very Important Line
	//Cache the owning pawn for use in Tick
	OwningPawn = TryGetPawnOwner();

void UVictoryAnimInstance::NativeUpdateAnimation(float DeltaTimeX)
	//Very Important Line
	//Always Check Pointers
	if (!OwningPawn) 
	//Set whether moving or not
	IsMoving = (OwningPawn->GetVelocity().SizeSquared() > 25);

Final Step: Reparenting your Character's Anim Blueprint

You need to make sure your Character is using your extended version of AnimInstance :)

For more information on reparenting Anim BPs, and making custom Animation Blueprint classes, see this code sample:

Animation Blueprints, Custom C++ Variables



Now you know what functions to override to create complicated Anim Blueprint logic with assistance from the C++ code,

or run your anim instance mostly from the C++ code (especially the event graph part)!


Rama (talk)