Some code, in case the above wasn’t clear enough:
NameOf() is a helper function returning an FString,
LoadedLevelNames[] and SavedPawnList[] are TArrays loaded from SaveGame:
void UMySaveGame::LoadLevels(UWorld* TheWorld)
{
UE_LOG(GameInfo, Log, TEXT("Loading..."));
//load level
//UGameplayStatics::LoadGameFromSlot();
for (int i = 0; i < LoadedLevelNames.Num(); ++i)
{
UE_LOG(GameInfo, Log, TEXT("Level: %s"), *LoadedLevelNames[i]);
UGameplayStatics::OpenLevel(TheWorld, FName(*LoadedLevelNames[i]),false);
}
//loop through spawn things
TArray<AActor*> actorlist = TheWorld->GetCurrentLevel()->Actors;
for (int i = 0; i < actorlist.Num(); ++i)
{
APawn* ThePawn = Cast<APawn>(actorlist[i]);
if (ThePawn != nullptr)
{
UE_LOG(GameInfo, Log, TEXT("Loading pawn #%i. name: %s"), i, *NameOf(ThePawn));
UE_LOG(GameInfo, Log, TEXT("Saved pawn list has #%i entries."), SavedPawnList.Num());
for (int j = 0; j < SavedPawnList.Num(); ++j)
{
FSavedPawn sp = SavedPawnList[j];
UE_LOG(GameInfo, Log, TEXT("Checking against saved pawn with name: "), i, *NameOf(sp));
if (NameOf(ThePawn).Equals(NameOf(sp)))
{
UE_LOG(GameInfo, Log, TEXT("Pawn #%i, %s was found and will be loaded."), i, *NameOf(ThePawn));
ThePawn->SetActorLocationAndRotation(sp.Position, sp.Rotation, false);
//quick remove of saved pawn from list, so it will not turn up in future searches. Safely reorder because we're returning afterwards.
SavedPawnList.RemoveAtSwap(j, 1);
return;
}
}
}
}
//execute BP
Load_Event();
}