[Request] <~* Joyful Code Recommendation *~> ArchiveBase: Please make a >> operator for Archive Reading

This may seem like a minor syntactic
suggestion, but it is actually a Joyful Recommendation because the lack of what I am suggesting caused me (made me predisposed^1) to have to do
about 48 hours of unnecessary R&D on
binary conversions only to find the
functionality I needed was already
built into the Archive system, I just
did not get it due to this one
seemingly small syntactic detail

-Rama

^1 = Google: “make someone liable or inclined to a specified attitude, action, or condition.”

Dear Friends at Epic,

Hi there!

I am loooving C++ UE4! Thank you so much!

You folks are awesome!

I have a very strong recommendation based on spending about 48 hours reading through ArchiveBase, Archive, and writing my own binary conversions, and then getting stumped on converting FString back from uint8 after using ar << FString.

You can review this thread for more complete picture of my struggles:
https://rocket.unrealengine.com/questions/5503/c-equivalent-to-basic-save-object.html

joyfulrecommendation.png

My Joyful Code Recommendation (Joyful code for Joyful People)

Please please make a >> operator to compliment the << operator

So that visually the code makes more sense as to when you are extracting byte data from the archive and converting back to data types, and when you are putting it in.

Example of what works right now:

FBufferArchive RV_Vibes;

//data is going in and being converted to uint8
RV_Vibes << FVector;
RV_Vibes << FRotator;	
RV_Vibes << FString;

//vars to receive previously serialized data
FVector outV;
FRotator outR;
FString outS;

//reader
FMemoryReader Ar = FMemoryReader(RV_Vibes, true);
ar << outV;
ar << outR;
ar << outS;

//the out vars now have the data

Example of What I Would Absolutely Find Very Lovely

FBufferArchive RV_Vibes;

//data is going in and being converted to uint8
RV_Vibes << FVector;
RV_Vibes << FRotator;	
RV_Vibes << FString;

//vars to receive previously serialized data
FVector outV;
FRotator outR;
FString outS;

//reader
FMemoryReader Ar = FMemoryReader(RV_Vibes, true);
ar >> outV;
ar >> outR;
ar >> outS;    
//isnt it more obvious now that the out vars are receiving data FROM the archive

and perhaps:

could the >> operator be made to work within a writer’s context, allowing extraction of data without using a Reader at all

especially when using a FBufferArchive which is already its own data source?

These my very highly energetic recommendations for your already awesome streamlined C++ wonderfulness

:slight_smile:

Summary of :

joyfulrecommendation.png

If I had seen the >> operator, if it had existed, even just in the header file, I would have immediately had a much easier time reconstructing the proper usage of the Archive system for my own use in a custom savegame system.

:heart: :heart: :heart:

Rama

Considering about 80% of the time << mean load OR save, the proposal doesn’t make a lot of sense. In many other cases, we aren’t actually even serializing anything. Maybe we are finding references or counting memory.

I agree that this is pretty crazy (and close to 15 years old). I most dislike that const does not work. I am open to ideas, but since << is used to both load and save, it would only make it more confusing to add >>, which I guess would only load and, what assert if this achive is incapable of loading?

Very nice to hear from you Gil!

A really good comment in the .h file by the << operator in the ArchiveBase.h file would have alleviated most of the many hours I spent trying to figure out this syntactic oddity of the archive system

Perhaps a “sample usage” for both loading and saving, really short, could be put in the comment, similar to the very helpful comment by FObjectAndNameAsStringProxyArchive

/**
 * Implements a proxy archive that serializes UObjects and FNames as string data.
 *
 * Expected use is:
 *    FArchive* SomeAr = CreateAnAr();
 *    FObjectAndNameAsStringProxyArchive Ar(*SomeAr);
 *    SomeObject->Serialize(Ar);
 *    FinalizeAr(SomeAr);
 * 
 * @param InInnerArchive The actual FArchive object to serialize normal data types (FStrings, INTs, etc)
 */
struct FObjectAndNameAsStringProxyArchive : public FNameAsStringProxyArchive
{

Because I saw this comment I was able to very easily figure out usage of archive proxies :slight_smile:

Short comment even like that by << would have been veery nice for example:

archivebase.h

    /*
    saving to archive
    (code)
    
    loading from archive to variables
    (code) 
    */
    The mighty << operator (maybe by the int32 version)

I’m just trying to express here that a good comment by the << would really help out the uninitiated newcomers to UE4 c++ :slight_smile:

Personally I think the >> operator could be a great addition without changing the << functionality, and then its usage would be only a matter of personal preference, an aesthetic visual preference,

And yes it would only load, so visually that would immediately tell the casual code reader (oh hey this is guaranteed to only be a loading data from archive to variables)

I dont know enough about assertions to comment on that part, I’ll leave that up to you :slight_smile:

I understand what James Golding was saying about being able to write 1 function to save or load data, but that convenience / power could use a little documentation int he code, and perhaps the >> alternative for those who want to reduce the visual ambiguity

Just a thought of mine really, hope you Epic Devs know how much I am loving UE4 engine :slight_smile:

Hey Nathan,

Sounds like a great idea, I’ve submitted to our team for them to consider.

Best Regards,

Ryan

Thansk Ryan! :slight_smile: