# UFunction return TArray

**URL:** <https://forums.unrealengine.com/t/ufunction-return-tarray/588530>\
**Category:** C++\
**Tags:** ufunction, unreal-engine, UE5-0, CPP, question\
**Created:** [June 23, 2022, 11:26am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530 "2022-06-23T11:26:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![swrcVendetta](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@swrcVendetta](https://forums.unrealengine.com/u/swrcVendetta)\
**Post date:** [June 23, 2022, 11:26am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/1 "2022-06-23T11:26:33Z")

</div>

Hey,

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!

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [June 23, 2022, 11:31am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/2 "2022-06-23T11:31:24Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![swrcVendetta](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@swrcVendetta](https://forums.unrealengine.com/u/swrcVendetta)\
**Post date:** [June 23, 2022, 11:36am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/3 "2022-06-23T11:36:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [June 23, 2022, 11:41am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/4 "2022-06-23T11:41:50Z")

</div>

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:

```auto
UFUNCTION(BlueprintCallable)
void ChangeArray(TArray<int32> InArray, TArray<int32> &OutArray);

```

And then you can use it like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/0/a/e0a7667ed213cc41e76a2638d4f14186628a0fc4.png)

---

<div class="post-metadata">

**Author:** ![swrcVendetta](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@swrcVendetta](https://forums.unrealengine.com/u/swrcVendetta)\
**Post date:** [June 23, 2022, 11:51am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/5 "2022-06-23T11:51:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [June 23, 2022, 11:55am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/6 "2022-06-23T11:55:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swrcVendetta](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@swrcVendetta](https://forums.unrealengine.com/u/swrcVendetta)\
**Post date:** [June 23, 2022, 12:00pm UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/7 "2022-06-23T12:00:53Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4aa5607e1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4aa5607e1](https://forums.unrealengine.com/u/anonymous_user_4aa5607e1)\
**Post date:** [June 23, 2022, 2:38pm UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/8 "2022-06-23T14:38:23Z")

</div>

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

```auto
UFUNCTION(BlueprintCallable)
void ChangeArray(const TArray<int32>& InArray, TArray<int32> &OutArray);

```

---

<div class="post-metadata">

**Author:** ![MrHardo1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MrHardo1](https://forums.unrealengine.com/u/MrHardo1)\
**Post date:** [June 2, 2023, 5:08am UTC](https://forums.unrealengine.com/t/ufunction-return-tarray/588530/9 "2023-06-02T05:08:51Z")

</div>

For whatever reason I couldn’t get any of the above solutions to work. I’m also quite new to C++. I was trying to return a TArray of Enums. Here’s the solution that ended up working for me:

```auto
//.h
UENUM(BlueprintType)
enum class EWeekday : uint8
{
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday
};

UFUNCTION(BlueprintCallable, Category = "NPCScheduleNode")
TArray<EWeekday> GetWeekdays();

#if WITH_EDITORONLY_DATA

UPROPERTY(EditDefaultsOnly, Category = "NPCScheduleNode")
TArray<EWeekday> Weekdays = { EWeekday::Monday, EWeekday::Tuesday };

#endif

//.cpp
TArray<EWeekday> UNPCScheduleNode::GetWeekdays()
{
	TArray<EWeekday> copyArray = Weekdays;
	return copyArray;
}

```

And the result in BP:  
 ![Capture](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/b/b/5bbefc528f7ca293eb89300e339a32f185bb519c.png)

Maybe not the best way since I’m making a copy but it’s not my top concern right now and couldn’t get the other methods to work
