Course contentsShow
← Unreal Engine Game Development
Lesson 208 of 8803. C++ and UObject ArchitectureFree lesson

Choosing Optimal Unreal Engine Container Types

Evaluate the performance trade-offs between TArray, TMap, and TSet to select the most efficient data structure for specific gameplay requirements.

In this lesson, you will learn how to select the right container—TArray, TMap, or TSet—to keep your gameplay logic performant and memory-efficient.

Choosing the right container is about understanding the "Big O" performance of your most frequent operations.

TArray: The Default Choice

TArray is a contiguous block of memory. It is your go-to for most tasks because it is extremely fast to iterate over (CPU cache-friendly) and supports easy sorting.

  • Best for: Small-to-medium lists of items, spawning actors in a row, or anything where you frequently loop through the entire collection.
  • Trade-off: Finding an item by value requires searching the whole array (O(n)). Inserting or removing elements in the middle is expensive because all subsequent elements must be shifted.

TMap: The Key-Value Specialist

TMap stores data as pairs of Key and Value. It uses a hash table, making lookups nearly instantaneous regardless of how many elements are in the map.

  • Best for: Inventory systems (Item ID to Item Data) or character attribute lookups.
  • Trade-off: It consumes more memory than a TArray because it must store hashes and pointers to maintain the map structure. Avoid using it if you only need to iterate over the data occasionally.

TSet: The Unique Collection

TSet ensures every element is unique. Like TMap, it uses hashing to find, add, or remove elements in constant time (O(1)).

  • Best for: Tracking "Currently Active Overlapping Actors" or ensuring a player doesn't have duplicate status effects.
  • Trade-off: Like TMap, TSet has a higher memory overhead than TArray and does not maintain a specific order.

Quick Comparison

// Fast iteration
TArray<AActor*> MyActors; 

// Fast lookup by ID (e.g., FName)
TMap<FName, float> SkillCooldowns; 

// Ensure no duplicate status effects
TSet<EStatusEffect> ActiveEffects; 

When building systems, ask yourself: "Do I need to iterate over this constantly, or do I need to find specific items by ID?" If you need order and iteration, reach for TArray. If you need lightning-fast lookups or forced uniqueness, reach for TMap or TSet.

Key Takeaway: Use TArray for iteration and order, TMap for key-based lookups, and TSet for ensuring uniqueness, keeping performance in mind based on the frequency of your data access patterns.