GetSkeletalMeshComponent()->GetAnimInstance() returns NULL

Hello! I am creating a Bow and Arrow for VR. The bow was created in C++ and was inherited from the SkeletalMeshActor. The actual mesh is populated when its instance is dropped in the level. I have a bone on the bow string so my player can pull it back with the motion controllers. To accomplish this, I am using a custom AnimInstance class to set custom variables for that bone’s location. The tutorial below helped me accomplish this:

Rama’s Tutorial

I followed the step-by-step instructions and produced the following custom AnimInstance class:

.h

#pragma once

#include "CoreMinimal.h"
#include "BowAnimInstance.generated.h"

UCLASS(transient, Blueprintable, hideCategories = AnimInstance, BlueprintType)
class UBowAnimInstance : public UAnimInstance
{
	GENERATED_UCLASS_BODY()
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = StringPlacement)
		FVector SkelControl_StringPos;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = StringPlacement)
		FRotator SkelControl_StringRotation;
};

.cpp

#include "Test_Game.h"
#include "BowAnimInstance.h"


UBowAnimInstance::UBowAnimInstance(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	// default values
	SkelControl_StringPos = FVector(0, 0, 0);
	SkelControl_StringRotation = FRotator(0, 0, 0);
}

On the BP side, I created the bow’s Animation BP and reparented it to my custom AnimInstance class. I can successfully access those variables in the Animation BP.

In the level, my bow instance has the skeletal mesh attached and notes to use the Animation BP and the Anim Class:

I added some code to test the AnimInstance out, but the test for its existence came out as NULL.

.cpp

#include "Test_Game.h"
#include "Bow.h"

ABow::ABow()
{
	// Set this actor to call Tick() every frame.
	PrimaryActorTick.bCanEverTick = true;

	// initialize components for bow
	GetSkeletalMeshComponent()->SetSimulatePhysics(true);
	stringGrabbed = false;

	// Setup collision
	GetSkeletalMeshComponent()->SetCollisionProfileName(FName("BlockAllDynamic"));
	GetSkeletalMeshComponent()->bGenerateOverlapEvents = true;
}


void ABow::BeginPlay()
{
	Super::BeginPlay();
	
	if (GetSkeletalMeshComponent()->GetAnimInstance() == NULL)
	{
		UE_LOG(LogTemp, Warning, TEXT("No existing animation."));
	}
}

Is there something obvious I have forgotten to do to access the AnimationInstance I have created?

UPDATE:

In addition, this is my Animation Blueprint for the bow. I’m fairly new to using this system, so be kind if the problem is obvious! :stuck_out_tongue:

Hi, this is most likely because actors are not guaranteed to exist at construction time (and they most likely will not, the world might not exist at that point). All actors will be initialised by the time BeginPlay is called, which I believe is the most suitable for what you are trying to achieve (use BeginPlay instead of the constructor).

Hi Lupix. Thanks for your response. In my code above, I’m already checking the “GetAnimInstance()” in BeginPlay with the same results.

I also placed this check in the function when I grab the string in-game, but the instance still returns NULL.

Oh, my bad. I did not see the lower part of your code block. Having said that, I also fail to see what would be causing this. I’ve looked around and it seems quite some people are having a similar issue. I may be able to get back to you with more information when I get the time to check how I’ve done it in my projects.