Has anyone tried in 4.19? Right now still using UStructs in many places where TMap would just fit a lot nicer if properly supported with replication. As useful and amazing as structs can be in few places TMap would just be so much cleaner to use in my project.
I am posting this in case it might be useful as a simple starting place for someone. Tested in 4.19 only, and only for simple types (ie. FString). The TMAP is placed inside of a USTRUCT where NetSerialize moves key/values between TMAP and TARRAY’s before/after network transport.
USTRUCT(BlueprintType)
struct FStructWithMap
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TMap<FString, FString> StringMap;
TArray<FString> StringKeys;
TArray<FString> StringValues;
bool NetSerialize(FArchive& Ar, class UPackageMap* Map, bool& bOutSuccess)
{
if (Ar.IsLoading())
{
// Move data to Map
Ar << StringKeys;
Ar << StringValues;
for (auto It = StringKeys.CreateConstIterator(); It; ++It)
{
StringMap.Add(StringKeys[It.GetIndex()], StringValues[It.GetIndex()]);
}
} else {
// Move data to Arrays
StringMap.GenerateKeyArray(StringKeys);
StringMap.GenerateValueArray(StringValues);
Ar << StringKeys;
Ar << StringValues;
}
StringKeys.Empty();
StringValues.Empty();
bOutSuccess = true;
return true;
}
};
template<>
struct TStructOpsTypeTraits<FStructWithMap> : public TStructOpsTypeTraitsBase2<FStructWithMap>
{
enum
{
WithNetSerializer = true
};
};