I’m learning Unreal C++ programming (and Blueprints) and really enjoyed implementing a HUD, menu, inventory system and so on, but I don’t think that I fully grok how to best write a neat and efficient save/load system.
I love the fact that << can be used to both save and load data to a FArchive, but as my code below illustrates I don’t understand how to take advantage of that when dealing with arrays of objects that need to be spawned on loading. When saving, everything is straight forward, but I have to keep track of if I’m loading and then do special handling to spawn the objects. Perhaps this is the only way, but I would be very happy to learn of a better/smarter/neater way to do this as I anticipate that there will be a lot more objects than just inventories.
The code below is based on Rama’s “Custom Save System To Binary Files” tutorial (Thanks!!!) and on saving FArchive& Ar takes a FBufferArchive and saves it using FFileHelper, while on loading it uses FFileHelper to load and passes in the FMemoryReader to the FArchive& AR parameter.
void ACustomController::SaveLoadData(bool bLoading, FArchive& Ar, FVector& playerLocation, FRotator& playerRotation, TArray<AInventoryObject*>& inventoryObjects)
{
// Saving/Loading player location and rotation
Ar << playerLocation;
Ar << playerRotation;
// Saving/Loading number of inventory objects
int32 InventoryCount = inventoryObjects.Num();
Ar << InventoryCount;
// Can't really write code that is ambivalent about saving or loading an array of objects... so this cludge is used.
if (bLoading)
{
UWorld* const World = GetWorld();
if (!World)
return;
for (int i = 0; i < InventoryCount; i++)
{
int Width, Height;
Ar << Width;
Ar << Height;
AInventoryObject * invObject = World->SpawnActor<AInventoryObject>(AInventoryObject::StaticClass());
if (invObject)
{
invObject->Init(0, Width, Height);
invObject->m_inventorySlots.SetNum(invObject->m_nInvHeightCount * invObject->m_nInvWidthCount);
inventoryObjects.Add(invObject);
}
}
}
else
{
for (int i = 0; i < InventoryCount; i++)
{
Ar << inventoryObjects*->m_nInvWidthCount;
Ar << inventoryObjects*->m_nInvHeightCount;
}
}
}
I also tried to use Serialize for objects, but I couldn’t get that working for loading.
Thanks for reading!