How to create a map or set property?

A map, at its most superficial and basic level, is just an array where you can define your own index. It is defined by a key and the data, the key being your custom index to search by.

C++ Array (TArray)

TArray<FString> YourArray;
YourArray.Add(TEXT("Mary has a little lamb"));
YourArray.Add(TEXT("The brown fox jumped over the lazy dog."));
UE_LOG(LogTemp, Warning, TEXT("YourArray's 2nd element is: %s"), *YourArray[1]);

This should return “The brown fox jumped over the lazy dog.”

C++ Map (TMap)

TMap<FName, FString> YourMap;
YourMap.Add("Mary", "Mary had a little lamb.");\
YourMap.Add("Fox", "The brown fox jumped over the lazy dog.");
UE_LOG(LogTemp, Warning, TEXT("YourMap's 'Mary' returns: %s"), *YourMap["Mary"]);

This should return “Mary had a little lamb.”

We can replace FName with any datatype we want, although you want to have a key that is unique (i.e probably a bad idea to make a TMap using a bool as a key).

Using Map in Blueprints

Maps came in 4.15 to Blueprints. To use it you can simply create a variable, and head over to your Details panel. Right next to where you select datatype is an odd - symbol colored like your datatype. Selecting it will bring a dropdown:

129335-ue4examplevariabledetails.jpg

Selecting the bottom two will change things up.

129340-ue4examplevariabledetailsmap.jpg

Unlike Arrays, there is no ‘Make Array’ functionality that quite works the same. Instead, if you want to define its values you do this from the Details panel again, this time under Default valuesNote: I changed my variable to FName, FString for this picture.

129342-ue4examplevariabledetailsdefaultvals.jpg

If you have any questions about this, you’re free to comment, or create your own thread. I suggest if you want me to update to this to comment here or message me. :slight_smile:

Note:

[There is a bug relating to UFUNCTION() and TMap.][4] If you run into problems compiling your C++ code with a TMap as an argument of your UFUNCTION, this is why. Please do say if you find a way to bypass this problem! :slight_smile: