Download

Animation using C++

Hi,

Is there a way to define the animation state machines/events and attach animations using C++ and foregoing Animation Blueprints? I saw there are UAnimBlueprint and UAnimInstance classes in the docs. How can I use them?

Stay safe,

AamirM

You can override UAnimInstance and make variables you need in it, then make Animation Blueprint in editor with your class as a parent, those varbales you made will be there as they been inherited and also accessible thru your class (from instance in skeletal mesh component) so you can control the animation from C++

I understood inheritance part already. Possibly even overriding the BlueprintUpdateAnimation() function with my own.

This is what I don’t want to do. I want to do this programmatically. How can I attach my custom UAnimInstance class to some skeletal mesh component using C++? Also, how can I define the state machine using C++? (for example, Idle->Run transition)

AnimInstance is created when you set anim type to blueprint anim and you set animation blueprint class.
Also, you can set only animation blueprint for skeletal mesh it was created for.
Also, i have tryed to change it on-fly, and Mesh->SetAnimClass() do not work for me :frowning:

So, for other you asked about, it is preaty simple to do.
You define states in .h, make them blueprintable, then you mkae state machine use your states for transition to other states.
For example:

  1. your states in .h

UENUM(BlueprintType)
namespace ECLASSState
{
	enum EState
	{
		Idle,
		Rise,
		Impose,
	};
}

class AYOURCLASS : public AActor
{
	GENERATED_UCLASS_BODY()

	UFUNCTION(BlueprintCallable, Category = State)
	ECLASSState::EState GetState();

        ECLASSState::EState myState;
}


also define your variable which will store your current state.

  1. Then in your anim blueprint in BlueprintUpdateAnimation() you can get this state and set it to variable which will be used to anim state transition
    c7010294c165ec1722a171d546ae435b4c4a8c07.jpeg
    5d8571c8e0e0a6916af5fb0fdef78028f8e6dcfd.jpeg

So then you can only set myState = ECLASSState::somestate; in code and whis will change animation state in animation blueprint also.

That is not what I am asking. I should rephrase…

How to do assign animations without touching the UE4 editor? (except importing them into the project) :stuck_out_tongue:

It is not much convinient to do all this things without editor.
You can initialize mesh component in class constructor, then find asset with static ConstructorHelpers::FObjectFinder and store reference, then in postinitcomponent set mesh and set animclass.
Also, same way you can set anim montages and play them from code. It is the simplest way i see for you,

From other side, if you wanna same anim state machine like editor created, then you need to write your own UAnimBlueprint class.
And really it will require a lot of work for you.
I do not understand why not to use editor and cool tools provided to do it like i wrote above.

I find C++ to be easier than all of this blueprint stuff. I can’t be the only one! :slight_smile: