So i have been following a ton of tuts, and making it to a T. And im finally being able to implement it fully in C++.
Thanks @Predalien and @Auran13
Could you pls just confirm if im not doing something wrong here, it stil doesnt seem to be able to restore the previous references?
In this case I need the AMyUnitActor that has a AMyUnitActor->CurrentRegion, to restore its reference to the CurrentRegion (AMyRegionActor*)
This is how im saving:
void AUnitManager::SaveData_Implementation(UMySaveGame* SaveGameRef)
{
if (!IsValid(SaveGameRef))
return;
SaveGameRef->SerializedUnitActors.Empty();
for (AMyUnitActor* Unit : All_UnitActors)
{
if (!IsValid(Unit)) continue;
FSerializedActorData Entry;
FMemoryWriter Writer(Entry.Data, true);
FObjectAndNameAsStringProxyArchive Ar(Writer, false);
Ar.ArIsSaveGame = true;
Unit->Serialize(Ar);
SaveGameRef->SerializedUnitActors.Add(Entry);
}
SaveGameRef->SerializedRegions.Empty();
for (AMyRegionActor* Region : All_RegionActors)
{
if (!IsValid(Region)) continue;
FSerializedActorData Entry;
FMemoryWriter Writer(Entry.Data, true);
FObjectAndNameAsStringProxyArchive Ar(Writer, false);
Ar.ArIsSaveGame = true;
Region->Serialize(Ar);
SaveGameRef->SerializedRegions.Add(Entry);
}
}
So here im saving, both the Units and the Regions. It works
Then to load:
void AUnitManager::LoadData_Implementation(UMySaveGame* SaveGameRef)
{
if (!IsValid(SaveGameRef))
return;
All_UnitActors.Empty();
TMap<FString, AMyUnitActor*> ID_Unit_Map;
TMap<FString, AMyRegionActor*> ID_Region_Map;
for (const FSerializedActorData& Entry : SaveGameRef->SerializedUnitActors)
{
AMyUnitActor* Unit = GetWorld()->SpawnActor<AMyUnitActor>(UnitClass);
if (!Unit) continue;
FMemoryReader Reader(Entry.Data, true);
FObjectAndNameAsStringProxyArchive Ar(Reader, true);
Ar.ArIsSaveGame = true;
Unit->Serialize(Ar);
ID_Unit_Map.Add(Unit->GetPathName(), Unit);
Unit->SetActorTransform(Unit->LastTransform);
All_UnitActors.Add(Unit);
}
All_RegionActors.Empty();
for (const FSerializedActorData& Entry : SaveGameRef->SerializedRegions)
{
AMyRegionActor* Region = GetWorld()->SpawnActor<AMyRegionActor>(RegionClass);
if (!Region) continue;
FMemoryReader Reader(Entry.Data, true);
FObjectAndNameAsStringProxyArchive Ar(Reader, true);
Ar.ArIsSaveGame = true;
Region->Serialize(Ar);
ID_Region_Map.Add(Region->GetPathName(), Region);
Region->SetActorTransform(Region->LastTransform);
All_RegionActors.Add(Region);
}
for (int32 i = 0; i < SaveGameRef->SerializedUnitActors.Num(); ++i) {
AMyUnitActor* Unit = All_UnitActors[i];
FSerializedActorData& Entry = SaveGameRef->SerializedUnitActors[i];
//here should be able to find the Region by the PathName, though its not that simple.
Unit->CurrentRegion = *ID_Region_Map.Find();
}
}
So when loading I respawn the actors, and the regions, then to get reconnect the references, I should GetPathName() that from the serialized Unit->CurrentRegion . Though that appears to be nullptr. Is there something im missing?
When we save/serialize a reference in this case Unit->CurrentRegion (AMyRegionActor*), isn’t there a way to serialize its path, or it needs an extra variable for that?