x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

When creating a SkeletalMeshComponent in Actor Constructor, why is it null in BeginPlay?

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 );
     
     }
    
    
    
    
    
    
Product Version: UE 4.14
Tags:
more ▼

asked Jan 14 '17 at 04:55 AM in C++ Programming

avatar image

Pteronix
1 1 1 2

avatar image Sean L ♦♦ STAFF Jan 17 '17 at 08:11 PM

Hey Pteronix,

Instead of FirstPersonMesh = NewObject(this, TEXT("BaseActor"));

Let's try using:

 FirstPersonMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("BaseActor));

New Object is typically used outside of the constructor, meaning if you wanted to create a component at runtime.

Let me know if that makes a difference.

(comments are locked)
10|2000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question