TMap operator[] vs Find vs FindOrAdd in Unreal Engine

Table of Contents

Works in Unreal Engine 4.27 and 5.0 to 5.8. Updated October 2026.

Quick answer: in Unreal, Map[Key] does not add a missing key the way std::map does. If the key isn’t there, it asserts and the editor stops. Use instead:

  1. Read if it exists: Find returns a pointer, or nullptr if the key is missing.
  2. Read with a fallback: FindRef returns a copy, or a default value.
  3. Read or create: FindOrAdd returns a reference and adds the key if it’s missing.
  4. Only check: Contains returns true or false.

Only use operator[] when you’re certain the key is in the map.

Why operator[] crashes

TMap::operator[] looks up the key and expects to find it. Epic’s documentation puts it plainly: if the map doesn’t contain the key, it asserts. In the editor and Development builds you get an assertion failure. In a Shipping build the check is compiled out and the game crashes instead.

That’s the opposite of the C++ standard library, where std::map[Key] silently inserts a default value. Code ported from std::map is the most common way to hit this.

Which function to use

You want to…UseReturnsAdds a missing key?
Read a value only if it existsFind(Key)Pointer, or nullptrNo
Read a value or get a defaultFindRef(Key)Copy of the valueNo
Read or create the valueFindOrAdd(Key)ReferenceYes, default-constructed
Check whether a key existsContains(Key)boolNo
Set a value (insert or overwrite)Add(Key, Value)Reference to the stored valueYes
Read a key you know existsMap[Key]ReferenceNo, asserts if missing

Read safely with Find

Find does the check and the read in one lookup:

				
					int32* Score = Scores.Find(PlayerName);

if (Score)
{
	*Score += 10;
}
				
			

Contains followed by Map[Key] works too, but it searches the map twice.

Write without crashing

Use Add to set a value. It inserts the key, or replaces the value if the key is already there:

				
					Scores.Add(PlayerName, 0);
				
			

For counters, FindOrAdd reads or creates in one call:

				
					Scores.FindOrAdd(PlayerName) += 10;
				
			

Map[Key] = Value; only works when the key already exists. It doesn’t add new keys.

Gotchas

Don’t keep pointers or references across Add or Remove. The pointer from Find and the reference from FindOrAdd or operator[] can become invalid when the map adds or removes elements. Look the value up again after changing the map.

  • FindOrAdd isn’t available on a const map. Use Find or FindRef there.
  • FindRef returns a copy, so changing the result doesn’t change the map.
  • In Blueprints, the Map Find node never crashes. It returns the value and a Found boolean.

FAQ

How do I check if a key exists in a TMap?

Map.Contains(Key) returns a bool. If you also need the value, Map.Find(Key) returns a pointer you can test for nullptr.

Does FindRef add the key?

No. It returns a copy of the value, or a default-constructed value if the key is missing, and leaves the map unchanged.

Is operator[] faster than Find?

No. Both do one lookup. operator[] just skips the null check and asserts instead.

Why does my map crash only in a packaged game?

A Map[Key] with a missing key asserts in the editor. If that code path only runs in the packaged game, you only see the crash there. Replace it with Find or FindOrAdd.

Related tutorials