[Bug Report?] UFunction return of references

Hello there,

im not sure if this should be considered as a bug report, but i experience some problems when trying to
return a const Reference of a variable instead of a copy as soon as it is declared as a UFUNCTION().

Example

const TArray< SomeThing >& GetAllOfSomething()

complains about wrong direction for the output pin in blueprint.

I believe this might be related to the opportunity for blueprint nodes to have more than one return values, since
if you add references as variable types as params, you´ll get output-pins of those params in the blueprint node.

Example:

void ToastMeSandwich(FSandwich &Sandwich)

Will this be possible some other time? or do we have to live with returning unsafe references?

best regards

Hey -

If you are trying to write code to use as a blueprint then the syntax for returning an array is a little different than expected. Using the “Get All Actors of Class” node as an example the return type for the source code of this node is actually void:

void UGameplayStatics::GetAllActorsOfClass(UObject* WorldContextObject, TSubclassOf<AActor> ActorClass, TArray<AActor*>& OutActors)
{
	OutActors.Empty();

	UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);

	// We do nothing if not class provided, rather than giving ALL actors!
	if(ActorClass != NULL && World != nullptr)
	{
		for(TActorIterator<AActor> It(World, ActorClass); It; ++It)
		{
			AActor* Actor = *It;
			if(!Actor->IsPendingKill())
			{
				OutActors.Add(Actor);
			}
		}
	}
}

The important part of the syntax is TArray< AActor>& OutActors as this is what controls the output pin in blueprints. With this in mind you can simplify the parameter to TArray& VARIABLE to use a different name or different datatype.

Ex: The following will produce a node with an input target of Self and output an array of floats when called in blueprints:

UFUNCTION(BlueprintCallable, Category = Test)
		void GetAllOfSomething(TArray< float >& OutFloat);

Cheers

Hello there,

thanks for the information and the fast reply.

This can be marked as solved then :wink:

Btw. everytime i post something i am not able to respond with comments, i only am able to “answer”. Did i miss some setting for that?

best regards

When you click the “Post you answer” button you should be prompted if your response is an answer or a comment. You should also see an “add new comment” button under the display name for each post.

yes, thats exactly what im looking for. In other posts i can do it easily but not in my own. There is also no reply under your comment visible for me, thats why “this” is an answer again.

Hello!
This works indeed, but if I want to pass an

TArray& val

as input?

Hey, i guess you would need to pass in a pointer instead.

So TArray* val should work to give you an input pin in blueprint.