"For Each" loop on for MAP variables

A TMAP is basically two Paired arrays, why can’t I For Each loop these arrays as a pair, with two outputs every loop (KEY / VALUE)??

Each Key/Value pair would be exposed during each loop.

Currently, I have to do:
Get TMAP - > Get all Keys → For Each (Keys) → Find (Key) in TMAP → Get Value (and also get key from For Each loop)

Should be:
Get TMAP → For Each (TMAP) → Get Key/Valuye

1 Like

Hey there, afaik currently there is no built-in functionality for that. You can get the keys array and the values array, but you can’t get them both in blueprints (only in c++).

Iterating through the pairs is only available in C++, it seems. Having maps in blueprints at all is pretty new tho.

1 Like

I’ve noticed that TMaps are new, they have been making a lot of changes to variables such as Set-by-Ref, fixes for structs, adding Maps and such.

Having simple and clean blueprints is good for our sanity, as it reduces the chance for bugs and allows for quicker understanding when skimming over bp’s

I love the direction UE is heading, I have about 10 months into my game, and so far I’ve only coded ONE tiny little function in c++, which I exposed to BP anyways (Get UClass from assets found with AssetManager (Purple Class node) by EvoPulseGaming · Pull Request #40 · EverNewJoy/VictoryPlugin · GitHub)

Its the little things that make life easier :slight_smile:

Please close the question and submit a feature request to epic :slight_smile:

TMap is not a UClass/struct – it can’t be used in blueprints. That means there’s a wrapper class somewhere.

Problem is, the class for the pairs isn’t going to work with blueprints either. Why can’t you just get the array of keys and the array of values? I would hope they’d be correlated so you can iterate through both with the same index.

You can do a for loop from 0 to the number of entries and use the index parameter to access both at the same time, but still not a perfect solution :stuck_out_tongue:

TMaps dont really follow the rule because they use TPairs which is not very supported with blueprints, you can’t even create normal variables of TPair. So you have to ask epic to add that support.

But it’s an array of an unsupported type, if they don’t add support then that would mean that they would have to make a use case just for tpair so that the for loop would have 2 outputs, one for the key and one for the value.

So you’d have a “For each pair Loop” that had three item outputs instead of two: an index, a key, and a value. You still need to specify the types for the key and value – not at runtime for the game, runtime for the editor (that was a little ambiguous, sorry).

The thing is, there is no support for this, so you either use what you have or you ask epic do add it. A few versions ago we didn’t even had TMap support, so it’s slowly getting there.

Or you add it yourself and do a pull request on Github. And the pairs are unsupported but it doesn’t matter because we are talking about C++ code that implements a blueprint library node (“ForEachPairLoop” wouldn’t provide the pair to the blueprint, it would provide the key and value).

I found the Select node code. I can see the “Autowire” stuff that configures the output pin types based on the input pins. For reference, the code is in “\Engine\Source\Editor\BlueprintGraph\Private\K2Node_Select.cpp”.

Not looking to have TPairs exposed, just a simple for loop that exposes the key and value separately.

I’m saying an array should have a For Loop, no matter the structure or content.

The Types are specified when you create a MAP variable?

That means you’d have to override the Map wrapper functionality – which means a different kind of difficulty and more trouble keeping the PR free of conflicts while you wait for approval. A BP library node (external to the Map wrapper class) would be much smoother. And with an external node, you can’t know the types til you have the input pin set. Heck, with the library node, you don’t even need to make a PR, you can just use it and share the function – or stick it in a plugin for others to use.

I like this idea, as it prevents having nodes stretched across each other. And visually simple if put in parallel.

Old threat, but just for people (like myself) coming across this now - you can get an array of keys from the map variable in blueprints as well now. So you can create a foreachloop with that and find the associated values for those keys.

3 Likes

This is the solution we all needed, thanks!

For sake of efficiency you would want to get the MAP size, and then iterate through it. Otherwise you access the data twice, once to get the array of keys, and again to access each value. If you do this a lot, this would be 2x the cost roughly.