[UE4]Pass Parameters before triggering BeginPlay (SpawnActorDeferred)
Two types API to modify Actor’s parameters before BeginPlay
SpawnActorDeferred
1st way:
FTransform SpawnTransform(SpawnRot, SpawnLoc);
AMyCharacter* Character = world->SpawnActorDeferred<AMyCharacter>(ActorClass, SpawnTransform);
if (Character)
{
Character->SetupFunction(... params ...);
Character->FinishSpawning(SpawnTransform);
}
2nd way:
FTransform SpawnTransform(SpawnRot, SpawnLoc);
AMyCharacter* Character = Cast<AMyCharacter>(UGameplayStatics::BeginDeferredActorSpawnFromClass(this, DeferredActorClass, SpawnTransform));
if (Character != nullptr)
{
Character->SetupFunction(... params ...);
UGameplayStatics::FinishSpawningActor(Character, SpawnTransform);
}
3rd way:
FActorSpawnParameters SpawnParameter;
SpawnParameter.OverrideLevel = GetLevel();
SpawnParameter.bDeferConstruction = true; //it means BeginPlay() would be deferred, not constructor.
GetWorld()->SpawnActor<AMyActor>(ClassType, SpawnParameter);
These ways were verified in v4.21:
SpawnActor
would trigger both Constructor and BeginPlay;SpawnActorDeferred
would trigger Constructor only;BeginPlay
would be triggered afterFinishSpawning
;- There’s no way to spawn actor before Constructor.
If there any other ways to pass arguments to the Constructor of Actor? An informal way is:
define static variables, set values of these variables before SpawnActor
, then use these variables in the Constructor.
Misc
Pass pamameters in SpawnActor:
FActorSpawnParameters Param;
Param.Owner = MyActor01;
AActor MyActor02 = GetWorld()->SpawnActor<MyActor>(MyClass, Param);
//Owner is the Actor01
MyActor Owner= MyActor02->GetOwner();
从前的艺术家的风格,都是徐徐徐徐形成的,自然发育,有点受日月之精华的样子。地球大,人口少,光阴慢,物质和精神整个儿松松宽宽潇潇洒洒,所以:人格即风格。──木心