How to create a map or set property?

They have just introduced the new feature of editable map and set properties, but have not shown how to create them.
It is impossible to find any result because of the poorly chosen names have serious ambiguity. Has anyone found out how to create them?

All I know is from the live-stream:

Just give them time, they’ll update the docs very soon

For those following this, you have to go to Edit>Editor Preferences>Blueprint>Experiemental and then tick off the option that says “Enable support for TMap and TSets”

They don’t mention it in their live-stream. I can’t imagine why, but its here! Hopefully they can bring it out of experimental soon though.

Any update on this? I see how to create them, but I can’t find any docs that explain their usage.

Didn’t notice they didn’t have their docs up, but they’re quite simple. You can find out which functions they use by dragging out a pin and expanding the “utilities” header, which can give you an idea. These are also exactly the same as the ones in C++ so you can look at how they work in there.

A TMap is basically an array, but with one difference. With an array, you get the element at the certain index via a number. TMap's are similar, but instead of a number, you can use anyother type, except for booleans. Reason why is because you can’t use the same type twice.

So, imagine you want to grab a sprite via a string name, you can have a map set up where the key (similar he index in an array) is a string and the value (similar to the element in an array) is a sprite.

So you can then do TMap.Find("dog") and that will return the sprite you have associated with that string name. Its incredibly useful, especially when the key is an enum.

A set, from the looks of it, is a grouping where you can’t have the same element twice, but, looking at the functions in UE4, there seems to be a lot more to it than that, so it might be best to Google how the work in C++. You can convert this to an array, so sets are more similar than maps are.

Both maps and sets can only have any given value once, and they are also orderless, so you can’t use a foreach loop on them.

Hope this helps!

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:

So if you’ve got 4.15 in front of you, and the dropdown has Set and Map disabled, what’s gone wrong?

The option mentioned above:

Edit>Editor Preferences>Blueprint>Experiemental and then tick off the option that says “Enable support for TMap and TSets”

no longer exists in the build I’ve got in front of me, but Map and Set are both just greyed out. The responsible code seems to enable/disable the options based on function:

ContainerRequiresGetTypeHash(PinContainerType)

which is just excluding the types I was hoping for:

static bool ContainerRequiresGetTypeHash(EPinContainerType InType)
{
return InType == EPinContainerType::Set || InType == EPinContainerType::Map;
}

You can’t use a bool for a Map or Set, because both require that the same element can’t be used twice. Seeing as bool has only two states, it wouldn’t make sense to use them as keys, so they don’t allow it. Everything else should work, like int, string, enum, etc.

That’s exactly what it was: I didn’t know the syntax for Map had to lead with the key. Works great now, thanks!

Welcome! Glad to know everything is working out!