Why are actors not being destroyed during game but they are during debugging?

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);
        }
    }
}

nothing seems obvious though:

i’ll do a deep dive on that one, as well as GetGUID

also you can cache Level->Actors[i]

and i think you have a small mistake. the loop should be "; i>=0; " otherwise you’ll skip actor 0

When I step through the lines with a debugger, I can see the Destroy() being called and returning true. And when I return to the editor, the actors have been destroyed. But when I do it without a debugger, the actors (specifically the player character) doesn’t get destroyed.

So I’m wondering if it’s a timing thing. Maybe something somehow executes too quickly when playing without a debugger which prevents it from being destroyed?

that sounds logical to me.
by the name of the function “restore” maybe it’s being executed while the level actors are still being created. which in turn will make the IsValid(Level->Actors[i]) fail.
or maybe the guid is not assigned yet.

with stuff like this i would add plenty of verbose ue_logs at each step and analyze the output.

though ue doesn’t use multithread much, so i’m not sure if a debugger will, in fact, affect that or not. try the logs.