I have followed this tutorial from Rama:
A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums,Read%26_Write_Any_Data_to_Compressed_Binary_Files
I want to override the operator << in a class (MainCharacter) to store a public Int32 value that AMainCharacter has, called TestIntValue;
With trial and error, I got it working, but I don’t know what did I change that it doesn’t work any more.
My code is simple:
MyPlayerController.cpp
....
void AMyPlayerController::SaveLoadData(FArchive& Ar, int32& SaveDataInt32, FVector& SaveDataVector, AMainCharacter*& MainCharacter)
{
Ar << SaveDataInt32;
Ar << SaveDataVector;
Ar << MainCharacter;
}
...
SaveLoadLib.h Full code:
#pragma once
#include "Object.h"
#include "Characters/MainCharacter.h"
FORCEINLINE FArchive& operator<<(FArchive& Ar, AMainCharacter*& MC)
{
Ar << MC->TestIntValue;
return Ar;
}
MyProject.h
#include "Engine.h"
#include "SaveLoadLib.h"
#include "Kismet/GameplayStatics.h"
It doesn’t compile. The error is:
*1>c:\unreal projects\MyProject\source\myproject\SaveLoadLib.h(8): error C2084: function 'FArchive &operator <<(FArchive &,AMainCharacter &)’ already has a body
1> c:\unreal projects\MyProject\source\myproject\Characters/MainCharacter.h(15): note: see previous definition of '<<'
I am more interested in understanding how this works than just getting it working or having a walkaround. I think I undestand the following points:
- Unreal uses the operator << to read
and write. - The order is important, so loading and saving must be executed in the same order.
- FORCEINLINE is a requirement for operator overloads if you want to be able to easily define them in a way that the UE4 compiler will like even when you have code split across multiple .cpp chunks (from Rama’s tutorial)
I want to serialize and save to disk some of the Actors and Structs that are in the Level.
Should I instead implement a function in the actors that I’m interested to save like:
//Class that Implements Interface MySaveLoad
void MyObject::SaveLoad(FArchive& Ar)
{
Super:SaveLoad(Ar);
Ar << Int32Value;
Ar << AnotherPrimitiveValue;
ChildObject->SaveLoad(Ar);//this might lead to circular dependencies
}
Is there a better/more standardize way to do this? Is there any Design Patterns/Best Practice that I can follow? What happens with the private members of Unreal Class (Like AActor) that I’m extending from?
Please Help!!
Cheers,
Bullyproof