Adding non-unique values to a map.

Hi all,

How can I add all values to a map, even if some are not unique? From the documentation it seems that any duplicate values are overwritten.

TiA

You can’t. That’s the whole point of a map :wink:

Hi!

If you need this in C++, use TMultiMap.
If you need this in Blueprints, then there is no way to do so, AFAIK.

Why do you need this, by the way?

Seems like you want an array which element is a pair <key, value>, am I right?

So I’m getting the “Hit Time” and “Hit Location” out of a “Line Trace” and I want to sort the “Hit Locations” by the shortest to longest “Hit Time”.

Do you need to do that in Blueprint?

I would create custom structure with field “HitTime” and “HitLocation”, then store all hit data in the array.

But now there are some problems. If I remember correclty, there are no “Sort” node for arrays with custom element type in Blueprints. So you either need to create logic to sort it yourself (it’s not that hard though) or do it in C++ (it’s much easier there).

So I’ve currently got this setup and the float is being sorted by lowest to highest but I’m unsure how I’d sort the Vectors that were related to the hit times to now be sorted in the same order.

TiA

As I can see, you use two separate arrays here. I think, you should use one array, which element is structure and sort it.

I guess the “sort it” aspect is where I’m struggling to work out how to do it.

Maybe this could help.

Create structure ST_HitInfo that consist of “HitTime” and “HitLocation”.

In the blueprint that you want to sort your hits create variable “HitInfos” - array of ST_HitInfo structs.
image

In that blueprint create function “SortHitInfos” which accepts array of ST_HitInfo and returns array of ST_HitInfo.
image

Go inside that function.

Create local variables:

  1. HitTimes - array of floats
  2. Index - integer
  3. MaxHitTimeValue - float
  4. ResultHitInfos - array of ST_HitInfo

Do not set default values.

image

Go through our initial unsorted array of HitInfos and add their HitTime values into separate array HItTimes

When it is done find max HitTime value and save it into local variable MaxHitTimeValue
image

Then go through WhileLoop node while variable Index is less then HitInfos length
image

At the WhileLoop find min value in array HitTimes and use found item’s index to get according hit info from HitInfos, then add found hit info into ResultHitInfos array. After that you need to add MaxHitTimeValue to the found min value (so it no longer min) and increase Index

FInally, when WhileLoop finished, return ResultHitInfos
image

Overall function view

Hope that helps.

1 Like

Thanks for the lengthy reply! I haven’t had chance to implement it yet but I will be doing soon.

Thanks again

1 Like

Had some free time to come back to this. Got it set up and working so thank you!

1 Like