C++ - UactorSequenceComponent - player returns null

I’m writing a plugin that set things up after world has loaded (and all models are loaded).
I’m then setting up UactorSequenceComponent on some objects (as well as tracks and keyframes) through c++.

The problem I have is that when I then want to get the player to actually play the animation on the component - it always returns NULL when I want to access the player.

Here I have some test code, that creates a actor, adds ActorSequenceComponent to it, creates a sequence, track and keyframes through c++.
In the editor it all shows up as expected- and when a mesh is put in the static mesh component - the animation works through the GUI as expected.
But then when I want to play the animation through code - I cannot access the player.

FVector Location(0.0f, 0.0f, 500.0f);
FRotator Rotation(0.0f, 0.0f, 0.0f);
FActorSpawnParameters dummyActorSpawnInfo;

AActor* dummyComponentActor = NULL;
dummyComponentActor = World->GetWorld()->SpawnActor<AActor>(Location, Rotation, dummyActorSpawnInfo);
dummyComponentActor->SetActorLabel("dummyActor");

class USceneComponent* dummyUSceneComponent = NewObject<USceneComponent>(dummyComponentActor, FName(TEXT("dummy USceneComponent")));

dummyComponentActor->SetRootComponent(dummyUSceneComponent);
dummyComponentActor->AddInstanceComponent(dummyUSceneComponent);

class UStaticMeshComponent* dummyMeshComponent =
	NewObject<UStaticMeshComponent>(dummyUSceneComponent, FName(TEXT("right mesh")));

class UStaticMeshComponent* dummyMeshComponent2 =
	NewObject<UStaticMeshComponent>(dummyUSceneComponent, FName(TEXT("left mesh2")));

dummyComponentActor->AddInstanceComponent(dummyMeshComponent);
dummyComponentActor->AddInstanceComponent(dummyMeshComponent2);

dummyMeshComponent->AttachToComponent(dummyUSceneComponent, FAttachmentTransformRules::KeepWorldTransform);
dummyMeshComponent2->AttachToComponent(dummyUSceneComponent, FAttachmentTransformRules::KeepWorldTransform);

class UActorSequenceComponent* uactorSequenceDummyComponent =
	NewObject<UActorSequenceComponent>(dummyComponentActor, FName(TEXT("dummy actorSequence")));

dummyComponentActor->AddInstanceComponent(uactorSequenceDummyComponent);

TObjectPtr<UActorSequence> sequence;
sequence = uactorSequenceDummyComponent->GetSequence();
UMovieScene* movieScene = sequence->GetMovieScene();

auto dummyUSceneComponentGUID = movieScene->AddPossessable(dummyUSceneComponent->GetName(), dummyUSceneComponent->GetClass());

sequence->BindPossessableObject(dummyUSceneComponentGUID, *dummyUSceneComponent, dummyComponentActor);
sequence->SetSequenceFlags(EMovieSceneSequenceFlags::Volatile | EMovieSceneSequenceFlags::BlockingEvaluation);

UMovieSceneTrack* track = movieScene->AddTrack<UMovieScene3DTransformTrack>(dummyUSceneComponentGUID);
UMovieSceneSection* section = track->CreateNewSection();

section->SetRange(TRange<FFrameNumber>::Exclusive(0, 3*24000));
track->AddSection(*section);

TArrayView<FMovieSceneDoubleChannel*> channels = section->GetChannelProxy().GetChannels<FMovieSceneDoubleChannel>();

int channelValueIndex = 0;
for (FMovieSceneDoubleChannel* channel : channels)
{
	// Create a transform key
	if (channelValueIndex == 0)
	{
		channel->SetShowCurve(true);
		channel->AddLinearKey(0, 0);
		channel->AddLinearKey(3 * 24000, 1000);
	}
	channelValueIndex++;

	channel->PostEditChange();
	channel->AutoSetTangents(1.0f);
	channel->ComputeEffectiveRange();
}

I then want to play a animation through code, but as mentioned initially - I cannot get access to ‘player’, meaning I cannot play the animation.
Here I simply loop through all - find the actorsequence component and sequence, but not player:

for (TActorIterator<AActor> ActorItr(World); ActorItr; ++ActorItr)
{
	auto actorSequenceComponent = Cast<UActorSequenceComponent>(ActorItr->GetComponentByClass(UActorSequenceComponent::StaticClass()));

	if (actorSequenceComponent)
	{
		UE_LOG(LogTemp, Warning, TEXT("Getting sequence"));
		auto actorSequence = actorSequenceComponent->GetSequence();
			
		if (actorSequence)
		{
			UE_LOG(LogTemp, Warning, TEXT("Found a sequence in actor named: %s "), *actorSequence->GetName());

			auto sequencePlayer = actorSequenceComponent->GetSequencePlayer();
			
			if (sequencePlayer)
			{
				UE_LOG(LogTemp, Warning, TEXT("Got access to player. "));
				sequencePlayer->PlayLooping();
			}
			else
			{
				UE_LOG(LogTemp, Warning, TEXT("Player returned NULL. "));
			}
		}
	}
}

I’m most likely missing something here - but I’m unable to see what, do anyone of you know how I can access the player and play the animation on the component?

So, I figured it out.
I had to call this after creating the component:

uactorSequenceDummyComponent->RegisterComponent();
uactorSequenceDummyComponent->BeginPlay();

This then fixed the issue with player returning null, and I could then access the player and play the animation as needed.

How could you include ActorSequenceComponent.h?

1 Like

Bumping up this question, in UE 5.1 I can’t include UActorSequenceComponent for some reason, it tells that

Unable to find ‘class’, ‘delegate’, ‘enum’, or ‘struct’ with name ‘UActorSequenceComponent’ [UnrealHeaderTool Error].

despite the plugin is enabled and fully functional inside the editor via blueprints.