Unreal Engine TArray Basics: A 5-Minute Guide for Beginners

Written by

in

Unreal Engine TArray Basics: A 5-Minute Guide for Beginners When programming in Unreal Engine with C++, you will quickly notice that you rarely use standard C++ vectors (std::vector). Instead, Unreal Engine relies on its own optimized container class: TArray.

TArray is the most commonly used container in Unreal Engine. It is a dynamic array, meaning it can grow and shrink in size automatically as you add or remove elements. It guarantees that all elements are stored in a contiguous memory block, making data access incredibly fast.

Here is everything you need to know to start using TArray in under five minutes. 1. Creating a TArray

To define a TArray, you need to specify the type of data it will hold inside angle brackets (<>).

// An empty array of integers TArray PrimeNumbers; // An empty array of AActor pointers TArray SpawnedEnemies; Use code with caution. 2. Adding Elements

Unreal Engine provides two primary ways to add data to a TArray: Add and Emplace. Add: Copies (or moves) an existing object into the array.

Emplace: Constructs the object directly inside the array’s memory space, avoiding unnecessary temporary copies. This is generally preferred for performance.

TArray NinjaNames; // Using Add NinjaNames.Add(TEXT(“Hanzo”)); // Using Emplace (Better performance) NinjaNames.Emplace(TEXT(“Genji”)); // AddUnique only adds the element if it doesn’t already exist NinjaNames.AddUnique(TEXT(“Hanzo”)); // Will not add, “Hanzo” already exists Use code with caution. 3. Accessing Elements

You can access elements in a TArray just like a standard C++ array using the 0-indexed bracket [] operator.

TArray Inventory = { TEXT(“Sword”), TEXT(“Shield”), TEXT(“Potion”) }; // Get the first item FString FirstItem = Inventory[0]; // “Sword” // Safely get an item using IsValidIndex to prevent crashes if (Inventory.IsValidIndex(5)) { FString SixthItem = Inventory[5]; } // Quick access to first and last elements FString CurrentWeapon = Inventory.Top(); // Returns the last element (“Potion”) FString StartingItem = Inventory.Last(); // Equivalent to Top() Use code with caution. 4. Iterating (Looping) Over an Array

The easiest way to loop through a TArray is using a range-based for loop.

TArray Scores = { 100, 250, 500 }; // Read-only loop for (const int32 Score : Scores) { UE_LOG(LogTemp, Warning, TEXT(“Score: %d”), Score); } // Modifying loop (by reference) for (int32& Score : Scores) { Score += 10; // Adds 10 to every score in the array } Use code with caution. 5. Removing Elements

TArray offers multiple ways to remove data depending on whether you want to preserve the memory order.

Remove: Removes all elements matching the value. It shifts remaining elements to fill the gap, preserving the array’s original order. RemoveAt: Removes an element at a specific index.

RemoveSingleSwap: Removes a single match and fills the gap with the last element of the array. This is incredibly fast because it doesn’t shift memory, but it changes the order of your array. Empty: Clears the entire array.

TArray Playlist = { TEXT(“Song A”), TEXT(“Song B”), TEXT(“Song C”), TEXT(“Song D”) }; // Removes “Song B” and shifts “Song C” and “Song D” left Playlist.Remove(TEXT(“Song B”)); // Removes element at index 0 without shifting memory (fastest) // “Song D” will be moved into the index 0 slot Playlist.RemoveAtSwap(0); // Clear everything Playlist.Empty(); Use code with caution. 6. Useful Utility Functions Here are a few quick utility functions you will use daily:

Num(): Returns the number of elements currently in the array. Never use .Num() > 0 to check if an array has elements; use IsEmpty() instead for cleaner code.

Contains(): Returns true if the array contains a specific element.

Find(): Looks for an element and returns its index via an output parameter.

TArray PartyMembers = { TEXT(“Warrior”), TEXT(“Mage”) }; int32 MemberCount = PartyMembers.Num(); // Returns 2 if (PartyMembers.Contains(TEXT(“Mage”))) { // Do something magic } int32 MageIndex; bool bFound = PartyMembers.Find(TEXT(“Mage”), MageIndex); // MageIndex will be 1 Use code with caution. Summary Cheat Sheet Add Element Emplace() Preferred over Add() for performance. Check Size Num() / IsEmpty() Use IsEmpty() for true/false checks. Safe Access IsValidIndex(i) Prevents out-of-bounds engine crashes. Fast Remove RemoveAtSwap() Fastest removal method; breaks array order.

With these basics under your belt, you are ready to handle lists of data safely and efficiently in Unreal Engine! If you want to dive deeper into Unreal Engine C++, tell me:

Are you interested in memory optimization tricks like Reserve()? Saved time Comprehensive Inappropriate Not working

A copy of this chat, including the images and video, will be included with your feedback A copy of this chat will be included with your feedback

Your feedback will include a copy of this chat and the image from your search

Your feedback will include a copy of this chat, any links you shared, and the image from your search.

Thanks for letting us know

Google may use account and system data to understand your feedback and improve our services, subject to our Privacy Policy and Terms of Service. For legal issues, make a legal removal request.