How to check if a value exists in a TMap?

I’m looking for a way to change a value in a TMap and then check if anything else has that same value.
I’ve tried use a Value function to the TMap and checked Array it with IsContain, but it always returns true. Even if the Value does not contain anything…
Does anyone else know of a better way?

Hello there.

I’m not sure if I understood what you would like to do, but if you want to check whether all values of a TMap are the same, you could generate an array of values for that TMap and then just iterate that array. Something like this: (didn’t test the code though)

bool AreValuesAreTheSame(TMap<FName, FName> Map)
{
	TArray<FName> Values;
	Map.GenerateValueArray(Values);
	if (Values.Num() == 0) return true; // Or false, depending what you want.
	const FName FirstValue = Values[0];
	for (FName Value : Values)
	{
		if (Value != FirstValue) return false;
	}
	return true;
}

Hello Rhomita.

Sorry, my english is poor.
I’m using blueprint. And i already tried similar method.
That’s the TMap’s Contain and Value nodes.
But Contains is return true even when it not contain. I checked by For Each Loop.
So I thought this was the wrong way to go about it.

You can use Contains and Find, and both of these work quite fine. I use them in my game. Both will return false if no key is found. If you want to search for double values, you can always get array of values, and then search for duplicates in array.

I tried this.


Unfortunately this doesn’t work. I don’t know the cause.

The output of Values is an array, so you need to loop through it and test each value directly.

Iterate / debug through it and see what’s going on there. I cannot really tell what is going on by looking at images of the blueprint :(.

No worries, I thought you needed it for C++.

This is the same but using blueprints:

(btw I didn’t test it, but I think it’s correct)

Finally I figured out why the function always returns True.
TMap has a default Empty value for the key and set, so when comparing with Empty it returns True for “Empty found”.
The Value array is not literally Empty. It’s contains multiple Empty values.
I completely overlooked this beginner’s mistake.
Thank you everyone for so many ideas.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.