# ForEachLoop behaviour with UFunction TArray return value

**URL:** <https://forums.unrealengine.com/t/foreachloop-behaviour-with-ufunction-tarray-return-value/271951>\
**Category:** Blueprint\
**Tags:** unreal-engine, question, UE4-27\
**Created:** [January 12, 2022, 3:35pm UTC](https://forums.unrealengine.com/t/foreachloop-behaviour-with-ufunction-tarray-return-value/271951 "2022-01-12T15:35:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joachim\_F.Meyer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Joachim\_F.Meyer](https://forums.unrealengine.com/u/Joachim_F.Meyer)\
**Post date:** [January 12, 2022, 3:35pm UTC](https://forums.unrealengine.com/t/foreachloop-behaviour-with-ufunction-tarray-return-value/271951/1 "2022-01-12T15:35:29Z")

</div>

Hi,

maybe someone can enlighten me on this behaviour:

I have a c++ UFunction (in an UBlueprintFunctionLibrary)

```auto
UFUNCTION(BlueprintPure)
static void GetArray(TArray<int32>& result);

```

with the code

```auto
void UTest::GetArray(TArray<int32>& result)
{
	UE_LOG(LogTemp, Warning, TEXT("Test Output Log"));

	for (int32 i = 0; i < 10; i++)
		result.Add(i);
}

```

within blueprints I call this node and use the output array in a ForEachLoop to iterate the array.

The output generated is this:

```auto
LogWorld: Bringing up level for play took: 0.000377
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log
LogTemp: Warning: Test Output Log

```

Why is the c++ UFunction called n+1 times while iterating with the ForEachLoop macro?  
It seems that for the first call of the ForEachLoop node the UFunction will be called and then for each iteration again.  
This does not make any sense, as I would normally asume the UFunction will be called once and the returned array will be used while inside the ForEachLoop.

Regards  
Bent

---

<div class="post-metadata">

**Author:** ![Joachim\_F.Meyer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Joachim\_F.Meyer](https://forums.unrealengine.com/u/Joachim_F.Meyer)\
**Post date:** [January 13, 2022, 3:58pm UTC](https://forums.unrealengine.com/t/foreachloop-behaviour-with-ufunction-tarray-return-value/271951/2 "2022-01-13T15:58:49Z")

</div>

Through testing it bit more, it seems that using the returned array length with a ForEachLoop  
 ![ForEachLoop](https://d3kjluh73b9h9o.cloudfront.net/original/3X/2/5/255f0ee2562c02f8dbe427fd090480a9c7ca2198.png)  
results in something similar to the following c++ code

```auto
for (int32 i = 0; i < GetArray().Num(); i++) {}

```

which would resolve why the UFunction is called for every iteration + 1, as the UFunction will be revaluated every iteration and the n + 1 iteration will break the loop.  
That would mean I am forced to use a local blueprint array variable as cache for the returned array and use this as the array input for the ForEachLoop.

Regards  
Bent
