How to load an object from binary without knowing its exact class?

That tutorial only explains how to manually serialize properties. To automatically serialize all UProperties on a class you can use:



Ar << ObjectClass;

if (Ar.ArIsLoading)
    Object = NewObject<UObject>(ObjectClass, ...);

if (Ar.WantBinaryPropertySerialization())
    ObjectClass->SerializeBin(Ar, Object);
else
    ObjectClass->SerializeTaggedProperties(Ar,Object,...);


These will iterate through the UProperties in the class and either write binary (fast but difficult or impossible to save delta properties during development) or tagged properties (slower but allows delta properties). So the idea is to first serialize the object class using Ar << ObjectClass; then spawn an instance of that class (if loading), then serialize the properties using the class and instance.

Be aware that UE4 packager won’t know about the properties you’re serializing here, so references to assets won’t be seen during packaging and will possibly be left out of the build altogether, you’ll have to force them to package somewhere else (e.g by using the “always package directory” settings). Also UObject has a bunch of properties that will be serialized but might cause problems if you load them back up again on top of an already constructed object, I have only ever used this strategy on UStructs.