Does anyone know how to start animations from code in c ++?

I am new to c ++ and unreal and wanted to know how to start animations from C ++.

I need to know how to find the animation or pass it to the script.,for example, I have the IDLE animation for my character called “ANIM_MOUNTAIN_DRAGON_idleBreathe” and I want to start that animation from my Boss_Character.cpp script.
What do I have to do in the code of Boss_Character.cpp and Boss_Character.h so that the script knows what animation I am referring to and start it.

Thank you!

Hey there. That’s pretty complex question you got here.

First of all to start any animation you should get UAnimInstance from your character’s mesh. From that class you can start animations and montages to play. In your cast you can use UAnimInstance::PlaySlotAnimationAsDynamicMontage
To load you animation in code there are bunch of ways, most of which are wrong, and the rest is long and requires much more code and preparation.
The fast way to load any blueprint class in code is to create a variable of TSoftObjectPtr in code, then in blueprint define this variable as an object you want (in your case ANIM_MOUNTAIN_DRAGON_idleBreathe), then in code create an object with that class and finally use it.

The proper way would be to create data assets or data tables and reference all nedded assets there.

But to be fair, I have to ask, why are you trying to load animation in code? It’s much more convenient to use animblueprint and change animation as a reaction on external variables change, then directly setting it.

Thank you very much for the answer!!
First I will answer your question, this project is homework, I don’t know how to say it in English so I don’t know if you’ll understand me but it’s for the “final grade work”. What we have been taught in class is unity which is clearly easier than unreal but personally I am much more a fan of unreal and i’m a fan of learn new things tho, the teacher forces me to use c++ instead of blueprints because they have to look at my “programming level”, that is why I need to do it from code. But I totally agree with you, blueprints is much easier.

Once your question is resolved I would like to ask you a new one, as I said I am very new to unreal so I am very sorry if this is absurd but i did this:


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Animation)
class UAnimMontage* IDLEAnimation;
void PlayIDLE();
FORCEINLINE class USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }

MeshDragon = CreateDefaultSubobject(TEXT(“CharacterMesh1P”));

void ABoss_Character::PlayIDLE()
if (IDLEAnimation != NULL) {
UAnimInstance* AnimInstance = MeshDragon->GetAnimInstance();
if (AnimInstance != NULL)
AnimInstance->Montage_Play(IDLEAnimation, 1.f);

Could you tell me if it’s okay, is it wrong or if I’m missing something?

Again thank you so much for the help !!

Oh, now I see, then I think there’s a better solution, you could use simple UAnimationAsset class.
Just modify your code with the following lines:

header file:

UPROPERTY(EditAnywhere, Category = Animation) 
class UAnimationAsset* IDLEAnimation;

cpp file:


The rest seems correct the way you are doing it!

In this case you open your blueprint, set the IDLEAnimation variable to your animation and it’ll be available for your mesh to play.
And don’t worry we all started at some point, so there are no stupid questions! :slight_smile: