Hi guys! I have spawned an actor during my actor class’ construction, and saved the spawned actor’s reference as a pointer like below:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Assets", Meta = (MakeEditWidget = true))
ATargetPoint *currentBar;
Spawn actor and save it’s pointer.
m_world = GetWorld();
if (m_world)
{
FActorSpawnParameters SpawnParms;
SpawnParms.Owner = GetOwner();
UE_LOG(LogTemp, Warning, TEXT("Creating currentBar..."));
currentBar = m_world->SpawnActor<ATargetPoint>(ATargetPoint::StaticClass(), SpawnParms);
UE_LOG(LogTemp, Warning, TEXT("currentBar created:%p"),currentBar);
}
When I delete my actor in editor, I want to spawned actor deleted as well, so I overrided Destroyed(), delete actor in Destroyed():
void MyActor::Destroyed()
{
if (m_world && currentBar)
{
m_world->DestroyActor(currentBar);
currentBar = nullptr;
}
}
This worked fine until I saved level and reloaded level, then I lost currentBar refrence.
So when I delete my actor after save/load in editor, currentBar won’t be deleted since it’s a NULL pointer.
Am I missing someting? Is it even possible?
Thanks forums!