Refactor tangible construction to use a pre-spawn initializer. This makes it so that the tangible is usable during ActorComponent initialization.

This commit is contained in:
2025-10-15 18:41:16 -04:00
parent 23d8e8684d
commit 0a3a7b9a62

View File

@@ -91,29 +91,27 @@ void UlxTangible::SetActorBlueprint(const FString &XName) {
// Spawn the actor even if it collides with something. // Spawn the actor even if it collides with something.
params.bNoFail = true; params.bNoFail = true;
params.bDeferConstruction = true; params.bDeferConstruction = false;
// This pre-spawn initialization will inject the tangible component into the actor. // This pre-spawn initialization will finish setting up the actor before
UlxTangible *ThisTangible = this; // the beginplay entry points run. This is called by 'SpawnActor', below.
params.CustomPreSpawnInitalization = [ThisTangible](AActor* A) //
params.CustomPreSpawnInitalization = [&](AActor* A)
{ {
// Inject the tangible component into the actor.
UActorComponent* ac = A->AddComponentByClass(UlxTangibleComponent::StaticClass(), false, FTransform::Identity, false); UActorComponent* ac = A->AddComponentByClass(UlxTangibleComponent::StaticClass(), false, FTransform::Identity, false);
UlxTangibleComponent* tc = Cast<UlxTangibleComponent>(ac); UlxTangibleComponent* tc = Cast<UlxTangibleComponent>(ac);
check(tc != nullptr); check(tc != nullptr);
tc->Tangible = ThisTangible; tc->Tangible = this;
// Make sure the actor label matches the actor name.
A->SetActorLabel(params.Name.ToString());
}; };
UE_LOG(LogLuprexIntegration, Display, TEXT("Creating Actor: %s"), *params.Name.ToString()); UE_LOG(LogLuprexIntegration, Display, TEXT("Creating Actor: %s"), *params.Name.ToString());
AActor* a = w->SpawnActor(blueprint, &transform, params); AActor* a = w->SpawnActor(blueprint, &transform, params);
check(a != nullptr); check(a != nullptr);
CurrentActor = a; CurrentActor = a;
// Make sure the label and the name are the same.
a->SetActorLabel(params.Name.ToString());
// This executes the BeginPlay entry point. We have to do this here
// because we deferred it in SpawnActor.
a->FinishSpawning(transform, true);
} }
void UlxTangible::UpdateAnimationQueue(std::string_view aq) { void UlxTangible::UpdateAnimationQueue(std::string_view aq) {