Download

MAP and SET containers explanation? (new in 4.15)

Hello everyone.

Could someone please explain me where and how can i use MAP and SET variables/containers? Im trying to get it, also i read some c++ articles too but i dont understand it…
Thanks!

Have a look at these two:

https://www.youtube.com/watch?v=cf25ekO-AFs

https://youtube.com/watch?v=8tLZWdc2b6k

I generally find it easier to learn from docs than videos but these two worked for me.

Set and Map are implemented in BP since 4.15 (in 4.14 they where visible, but not editable).
To create a set, click the Variable Icon. Next to the Type. (There where you usually set the variable to be an array).
There you can set the Variable to be a map or a Set. A Map needs an additional Key Type for the access of a value which needs to be set separately.
Keep in mind that both Maps and Sets don’t allow duplicate values.

I think i got it probably:
TSet is container like array but same value can be added only once, for example 1, 2, 3, 4, 5, in array i can have values like 5, 5, 5, 2, 1, 5…
TMap has unique only KEY, so if i add key like “weapon1”, “weapon2”, “weapon3”, i can add to them “rocket”, “pistol”, “knife”… If i try to add same key, like weapon2->chainsaw, then it won’t create another key+value set, but only overwrite weapon2 from pistol to chainsaw…

am i right?

HOWEVER, can someone give me a good example where can i use this? because i really dont have idea…

You might use a map where you need a key and value pair. This is similar to the Dictionary in C#. Say you have a character with different armor types (helmet, chest, legs, arms). Those can be your keys. Then for the values you can have the actual armor assets. So if your character is wearing no helmet, the value of key “helmet” is null. The character picks up a bicycle helmet and puts it on, so now the value of “helmet” will be set to the bicycle helmet blueprint actor. That’s just an example, there are many reasons for wanting a map. Really it’s any time you have two values that must go together.

You would use a Set in cases where you want to use an array but the order doesn’t matter and you won’t need duplicate entries. Sets are supposedly faster to index than arrays but I don’t know the actual speed difference. I doubt it’s very much and in most situations won’t make any perceptible difference.

The thing about these containers is that they are unordered. While an array is guaranteed to have a numbered structure, items in a map or set do not, so you can’t grab an item at a specific index since an ordered table of indices does not exist. This is why they are faster to grab from at a low level, but something that may make them undesirable in some workflows.

If you want an ordered version of the Map, a “type” doesn’t exist but you can make an array of structures with the types you need as a workaround.

So in short:
-Sets are basically arrays with only unique entries and no guaranteed order but are faster on a low level than arrays, good for specific speed-critical situations where order and duplicate entries are not necessary.
-Maps are kind of like an array of structures but only take two types at once and, like sets, have no guaranteed order and keys must be unique (values can be duplicated across multiple keys). Again, they are faster on a low level than an array of structures and take less time to set up (as a structure need not be created first).

Any time you want to associate a unique key with some data, a map is useful.
For example, I am working on a 3D coordinate system. So if I were to use a 3d Array, which I did for a while, it would be an array of struct A (x axis) holding array of struct B (y axis) holding an array of struct (z axis) with actual data.
Where as a map just needs a coord key struct with the data struct as value. Hundred times easier to work with, especially considering the “set array element inside struct” ordeal.

A set if useful if you want to store unique keys but with no data associated. Such as “does this mech have a left arm”, Have the player unlocked blueberries, does player have perk x

That said, I too have a question related to maps.
As mentioned I have a coordinate system, I’m calling each coord a cell. A cell becomes an instanced actor and each cell can communicate with adjacent ones, so I need to know which direction adjacent cells are.
So each cell have a direction enum - cell ref kvp map. So I can tell a cell to send a message in a direction.
On the other hand, I might want to know which direction a cell got a message from. So I know the cell ref, but not the direction. So I need another map with Cell ref - Direction kvp.

So for every directional-reference map I end up with an opposite map. So not only do each cell have two maps of adjacent cells, each cell can also have local doors in any direction meaning 2 door maps. And there may be other things I want to add later on.
So is opposite kvp maps a good way to handle that sort of thing? Is there another method I should be using?