I am using the following code to load a Skeletal Mesh into a Skeletal Mesh Component, then load an Animation Blueprint into it. But when I run this, BeginPlay prints “FirstPersonMesh is null” instead of loading the AnimInstance. I’m not sure why this would be the case, since it appears to be loaded just fine in the constructor, but then becomes null before BeginPlay is called.
-
Thanks
#include “RMC_Examples.h”
#include “BaseActor.h”// Sets default values
ABaseActor::ABaseActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;FirstPersonMesh = NewObject<USkeletalMeshComponent>(this, TEXT("BaseActor")); RootComponent = FirstPersonMesh; static ConstructorHelpers::FObjectFinder<USkeletalMesh> mesh(TEXT("SkeletalMesh'/Game/mobs/warriors/all_warrior.all_warrior'"));// ); USkeletalMesh* LoadedObject = mesh.Object;//load the object as per tutorial below or use static load object directly, or use ObjectFinder. //The critical part if (LoadedObject && FirstPersonMesh) { FirstPersonMesh->SetSkeletalMesh(LoadedObject); FirstPersonMesh->SetWorldScale3D(FVector( 100.0f, 100.0f, 100.0f)); FirstPersonMesh->SetAnimationMode(EAnimationMode::AnimationSingleNode); FirstPersonMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); //static ConstructorHelpers::FObjectFinder<UAnimBlueprintGeneratedClass> MeshAnimClass(TEXT("AnimBlueprintGeneratedClass'/Game/mobs/warriors/all_warrior_AnimBlueprint.all_warrior_AnimBlueprint'")); static ConstructorHelpers::FObjectFinder<UAnimBlueprint> FoundAnimBP(TEXT("/Game/mobs/warriors/all_warrior_AnimBlueprint.all_warrior_AnimBlueprint")); UAnimBlueprintGeneratedClass* PreviewBP = FoundAnimBP.Object->GetAnimBlueprintGeneratedClass(); FirstPersonMesh->SetAnimInstanceClass(PreviewBP); static ConstructorHelpers::FObjectFinder<UAnimSequence> anim(TEXT("AnimSequence'/Game/mobs/warriors/all_warrior_Anim.all_warrior_Anim'")); AnimSequenceBase = anim.Object; }
}
// Called when the game starts or when spawned
void ABaseActor::BeginPlay()
{
Super::BeginPlay();if (GEngine) { GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Code Begin Play")); } if (FirstPersonMesh) { UAnimInstance *AnimInst = FirstPersonMesh->GetAnimInstance(); if (AnimInst) { UE_LOG(LogTemp, Warning, TEXT("AnimInst exists")); AnimInst->PlaySlotAnimationAsDynamicMontage(AnimSequenceBase, TEXT("UpperBody"), 0.1f, 0.1f, 1.0f, 30.0f); } else { UE_LOG(LogTemp, Warning, TEXT("AnimInst does not exist")); } } else { UE_LOG(LogTemp, Warning, TEXT("FirstPersonMesh is null")); }
}
// Called every frame
void ABaseActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );}