I have this code which is supposed to destroy certain actors in the level (in this case the character I’m playing as) and respawn them. It respawns fine but doesn’t destroy the old character . Any idea what could be causing it? Been debugging it all day but I can’t figure it out.
void UEssSubsystem::RestoreLevelData(TObjectPtr<ULevel> Level, const FEssLevelData* LevelData)
{
// Destroy runtime actors with a GUID since they have save data and will be respawned
for (int32 i = Level->Actors.Num() - 1; i > 0; i--)
{
if (!IsValid(Level->Actors[i]) || !Level->Actors[i]->GetClass()->ImplementsInterface(UEssSavableInterface::StaticClass()))
continue;
if (EssUtil::IsRuntimeActor(Level->Actors[i]))
{
if (EssUtil::GetGuid(Level->Actors[i]).IsValid())
Level->Actors[i]->Destroy();
}
}
// Respawn runtime actors with save data
for (auto& ActorData : LevelData->RuntimeActorsData)
{
RespawnRuntimeActor(ActorData, Level);
}
// Restore data of placed actors
for (auto Actor : Level->Actors)
{
if (!IsValid(Actor) || !Actor->GetClass()->ImplementsInterface(UEssSavableInterface::StaticClass()))
continue;
if (!EssUtil::IsRuntimeActor(Actor))
{
const FEssPlacedActorData* ActorData = LevelData->PlacedActorsData.Find(Actor->GetFName());
RestorePlacedActorData(*ActorData, Actor);
Cast<IEssSavableInterface>(Actor)->Execute_PostLoadGame(Actor);
}
}
}