Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

function 'FArchive &operator <<(FArchive &,AMainCharacter *&)' already has a body

I have followed this tutorial from Rama:


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:


     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;


 #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:

  1. Unreal uses the operator << to read and write.

  2. The order is important, so loading and saving must be executed in the same order.

  3. 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)
     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!!



Product Version: UE 4.10
more ▼

asked Nov 16 '15 at 03:20 AM in C++ Programming

avatar image

Bionic Ape
124 16 19 24

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Just in case anybody is interested, this is how I solved it:

 class TEST_API ASaveActor : public AActor
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Test)
     int32 TestIntValue = 12;
     friend FArchive& operator<<(FArchive& Ar, ASaveActor& SObj)
         Ar << SObj.TestIntValue;    
more ▼

answered Nov 18 '15 at 03:55 AM

avatar image

Bionic Ape
124 16 19 24

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question