Can't get animation working via C++

Hi !

I am trying to import and play my animations entirely via C++ in order to understand a little bit about what happens behind the scenes when you use Blueprint animation. However, I can’t get it working, and I am going crazy ! Can anyone show me the light, please ? Here is my code:

================ MYCHARACTER.H =========================

/** SKELETAL MESH COMPONENT SETUP */

USkeletalMesh * P1SkeletalMesh;
UPhysicsAsset * P1PhysicsAsset;

/** BEGIN TO PLAY */
virtual void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;

/** ANIMATION SETTINGS */
TSubobjectPtr<UAnimSequence> Idle;
TSubobjectPtr<UAssetImportData> ImportData;

================ MYCHARACTER.CPP =========================

AMyCharacter::AMyCharacter(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{

/** ================== SKELETAL MESH SETUP ======== */
/** Asset, Reference Obtained Via Right Click in Editor */
static ConstructorHelpers::FObjectFinder<USkeletalMesh> P1SkeletalMeshSource(TEXT("SkeletalMesh'/Game/BPCharacter/HeroTPP.HeroTPP'"));
static ConstructorHelpers::FObjectFinder<USkeleton> P1Skeleton(TEXT("Skeleton'/Game/BPCharacter/HeroTPP_Skeleton.HeroTPP_Skeleton'"));
static ConstructorHelpers::FObjectFinder<UPhysicsAsset> P1PhysicsAssetSource(TEXT("PhysicsAsset'/Game/BPCharacter/HeroTPP_PhysicsAsset.HeroTPP_PhysicsAsset'"));
/** Set Mesh From Source */

P1SkeletalMesh = P1SkeletalMeshSource.Object;
P1SkeletalMesh->PhysicsAsset = P1PhysicsAssetSource.Object;
P1SkeletalMesh->Skeleton = P1Skeleton.Object;
Mesh->SetSkeletalMesh(P1SkeletalMesh);

/** ================== SKELETAL MESH SETUP ======== */


/** ANIMATION SETTINGS */
Idle = PCIP.CreateDefaultSubobject<UAnimSequence> (this, TEXT("Idle"));
ImportData = PCIP.CreateDefaultSubobject<UAssetImportData> (this, TEXT("ImportData"));
Mesh->SetAnimationMode(EAnimationMode::AnimationSingleNode);


Idle->SetSkeleton(P1Skeleton.Object);
ImportData->SourceFilePath = FString(TEXT("AnimSequence'/Game/BPCharacter/Animations/Idle.Idle'"));
Idle->AssetImportData = ImportData;
Mesh->(Idle);
Mesh->PlayAnimation(Idle, true);
Mesh->InitAnim(true);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT(Idle->AssetImportData));

}

void AMyCharacter::BeginPlay() {
Super::BeginPlay();
}

void AMyCharacter::Tick(float DeltaSeconds) {}

Thanks !

In case anyone stumbles upon this question, I was trying to play a simple animation sequence in C++ only; just something very simple, no blending or anything like that. I spent forever trying to figure out why my animation wasn’t playing. I realized that I needed to move all the animation setup into the BeginPlay function. The functions like PlayAnimation() won’t work in the constructor. Hope this helps someone. By the way, after setting the skeletal mesh with USkeletalMeshComponent::SetSkeletalMesh(), all i had to do was call USkeletalMeshComponent::PlayAnimation() in the BeginPlay() function.