Pass array reference as function parameter

Hello, guys! I stucked with next problem. Lets suppose i have the class:

class REFSTEST_API ATestActor : public AActor
{
	...
	UPROPERTY(BlueprintReadWrite)
	TArray<int32> Data;

	UFUNCTION (BlueprintPure)
	TArray<int32>& GetData()
	{
		return Data;
	}
};

Now i want to get Data field as reference and change some its elements in other class (actually i need to track this array in other class and want to change elements only for testing purproses now)

UCLASS(BlueprintType)
class REFSTEST_API UTestObject : public UObject
{
	...

public:
	UFUNCTION(BlueprintCallable)
	void ChangeData(UPARAM(ref) TArray<int32>& Data)
	{
		Data[0] = 44; //Won't change original Data array 
	}
};

As you can see i want to call ChangeData and pass Data from ATestActor as its param. Let’s try to make it with Blueprints

I initialize my array with [1, 1, 1] values and start the game. I construct UTestObject and call ChangeData on it. ChangeData gets Data array from ATestActor over GetData() function as reference. And it’s not working as i intended. Initial Data array in ATestActor is still [1, 1, 1], not [44, 1, 1] as i expected :frowning: But, the most interesting thing is as i pass Data array directly to ChangeData(), not over GetData() it’s working! (Data node on my screenshot).
And finally i tested this code without blueprints and it’s working like a charm! I tested GetData() only with C++. I wrote temporary function in UTestObject

UFUNCTION(BlueprintCallable)
	void ChangeDataOverActor(ATestActor* TargetActor)
	{
		ChangeData(TargetActor->GetData());
	}

And it’s working! So, finally, if i call GetData() from C++ it’s working as i exptected. When i call it from Blueprints my reference loses somewhere in GetData(). It’s also working when i pass Data array directly to ChangeData, but still not working when i pass it over GetData() function. I have no more ideas about this. Help me if you can, guys and thank you in advance!

1 Like