Community Tutorial: Using Custom C++ Structs as TMap Keys in Unreal Engine

Tutorial on how to setup a USTRUCT to be used as a key in a TMAP.

https://dev.epicgames.com/community/learning/tutorials/GxZ9/using-custom-c-structs-as-tmap-keys-in-unreal-engine

2 Likes

Thank you SOOO much!
I was getting frustrated trying to figure out, on how to create Default-Values for a TMap.

UPROPERTY(BlueprintReadWrite, EditAnywhere, meta=(DisplayName = “Spotting Accuracy (Meters)”))
TMap<float,FSpottingAccuracy> SpottingAccuracyMeters
{
{1000,FSpottingAccuracy{
EVisionLevelOfIdentification::EquipmentDetailed,90,100}},
{2000,FSpottingAccuracy{
EVisionLevelOfIdentification::SuspectDetailed,50,100}},
{3000,FSpottingAccuracy{
EVisionLevelOfIdentification::UnknownBasic,10,100}}
};

Tried almost everything prior to your solution, so again: thank you very much for the effort!

1 Like

Copy/Paste
// (c) blurryroots innovation qanat OÜ – CC BY 4.0

blurryroots? Der Name kommt mir doch bekannt vor :smiley:

Schöne Grüße aus Göttingen,
RamĂłn Janousch

1 Like

Aloha Ramon, welch Adleraugen! :blush: Beste GrĂĽĂźe auch an dich.

1 Like

@DaveTheFreak Year it can get quite tricky. Really glad I could help you out with this.

It didn’t compile for me then I looked at the source code for examples and saw they were using “friend”, did the same and it passed compilation.

1 Like

This tutorial has a pretty serious mistake in it. The CRC over the bytes of FHashMeIfYouCan hashes the bytes of an FStringobject itself (definitely a pointer if the string gets long) rather than the content of the string! There will be instances of FHashMeIfYouCan that contain equal-valued FHashMeIfYouCan::Impersonation members but that produce different hashes!

The whole idea of a hash is that things producing different hashes cannot be equal. So TMaps using FHashMeIfYouCan will let multiple keys whose FHashMeIfYouCan::Impersonationis "some pretty long impersonation string”.

This might work for small strings anyway depending on how FString manages memory before strings get long.