{ }Blueprint → C++

Unreal Engine 5 · Blueprint → C++

For Each Loop Over an Array in Unreal Engine 5 C++UE Docs

The Blueprint For Each Loop over an array maps to a C++ ranged-for loop such as for (int32 Number : Numbers), which iterates every element of a TArray in Unreal Engine 5.

Blueprint node & C++ equivalent

C++
for (int32 Number : Numbers)
{
  // ...
}

What does the For Each Loop node do?

For Each Loop walks through every element of an array in order, exposing the current element and its index on each iteration. It is the most common way to process all items in a TArray in Blueprint.

The C++ equivalent

A ranged-for loop, for (int32 Number : Numbers), gives you each element by value. To avoid copies for larger types use a reference: for (const FString& Name : Names). Use a non-const reference like for (int32& Number : Numbers) when you need to modify elements in place.

When you need the index

A ranged-for loop does not expose the index. If you need it, use an indexed loop: for (int32 i = 0; i < Numbers.Num(); ++i). Avoid adding or removing elements from the array while a ranged-for loop is running, as that invalidates the iteration.

Frequently asked questions

How do you loop through a TArray in UE5 C++?+

Use a ranged-for loop: for (int32 Item : MyArray) { ... }. Add a reference for non-trivial types to avoid copying each element.

How do you get the index in a TArray for loop?+

A ranged-for loop has no index, so use a classic indexed loop with for (int32 i = 0; i < MyArray.Num(); ++i) instead.

Related Arrays & Containers nodes

View all Arrays & Containers nodes →