Download

serialization and TMap: why does this compile?

This page claims: “Caveat: Unlike TArray, TSets (and TMaps) are not directly supported as UPROPERTYs and so cannot be automatically replicated, serialized, etc…” and “Like the TSet, the TMap cannot be declared as a UPROPERTY.”

Thus I was surprised to write this line of code inside a UCLASS for a faction system and discover that it compiles just fine:



   UPROPERTY()
   TMap<const UPlayerFaction*, float> FactionAttitude;

Is that documentation page out of date? Or is it that I’ll suffer horrible attacks from subtle save/load bugs later on if I try to use that for game serialization?

Certainly I could go write my own serializer for the map data, but I’d rather not if I don’t have to!

Yes it’s out of date, it’s been in since 4.8.
You still can’t expose it properly in the editor, but serialization and garbage collection are handled.

Sweet! Thanks Kamrann.

I don’t believe that serialization is handled correctly just yet - I have experimented in 4.9.1 with saving a level containing an actor with a TMap UPROPERTY, and on re-load the TMap was empty. Switched to a TArray and it was all dandy.

Hmm okay, that’s surprising. I haven’t tested but I assumed serialization was done, since they didn’t mention it was missing. Anyway cheers, will look into it.

I’m not far enough along in my project to easily say whether it works, but at the least, I just looked and they have added an operator<<() in Runtime/Core/Public/Containers/Map.h. I’ll get around to trying it out in time I hope. Seems like it shouldn’t be all that hard to serialize a map or set, so I guess it either works or will be easy enough to fix.