UFunction return TArray


so I am fairly new to C++ coding in general, not just when it comes to Unreal. I have some good knowledge in C#, Java and other languages however C++ is a different level to me.

I am trying to return a TArray from a void (UFunction) into BluePrint but I can not get it to work. I saw you can do it without using “return” but by putting it into the void parameters. That has worked but not with TArray for some reason and I don’t really understand why it’s not working.

Could anyone explain how it has to be done and show an up-to-date example that is actually working (in UE5)? That would be really helpful!

You can get a TArray from function without return if you pass an array parameter as non-constant reference. For example:

void GetArray(TArray<AActor*>& Array); // in BP returns an array of Actors

I might be wrong, but as far as my understanding is right this would not work since in a UFunction(BlueprintCallable) ‘&’ is not allowed or am I wrong?

I want it to work in a UFunction, C++ alone would not “help” me. ^^

But thank you so much, I am gonna experiment a little with the code you gave me.

It is allowed, but in BP it will be treated as an output parameter, so you can’t use it when calling the function like you would in C++;
You can use two array parameters, one as input, the other as output:

void ChangeArray(TArray<int32> InArray, TArray<int32> &OutArray);

And then you can use it like this:

1 Like

Looks really promising and is working. Thanks a lot. Now, does this work with any Type of TArrays? For example TArray<TSharedPtr>? How would I do that? This is what I couldn’t get to work and I thought I had to use ‘*’ as the compiler said but I didn’t know where nor how.

IDK, you need to test it. Not all types of pointers are supported by blueprints.

Yea, that seems to be the case for what I was trying to do but thats good to know, thanks a lot to both of you. That helped my understand a little more and I think I might be able to solve what I was trying to do!
Thanks again!

To avoid unnecessary array copies you should pass input arrays by const-ref:

void ChangeArray(const TArray<int32>& InArray, TArray<int32> &OutArray);