How to make a custom Blueprint node (K2 node) pin return by reference?

What is the syntax for creating a return by reference pin on a custom K2 node?

Writing a node that returns a regular reference compiles into a circular return pin, and calling by-ref nodes on the output of that pin doesn’t change the original value.

For example,

static bool ChangeInt(UPARAM(Ref) int32& TargetInt, int32& ChangedInt);

returns ChangedInt by value after I define

DECLARE_FUNCTION(execChangeInt)

I realize the UPARAM(Ref) macro makes an input pin by-ref - is there something that would do the same for the output pin?

Similar to the output pins of these nodes:

293815-capture.png

If you see something in editor then look it up in source code, array nodes are normal function bindings, you can find them here:

https://github.com/EpicGames/UnrealEngine/blob/7d9919ac7bfd80b7483012eab342cb427d60e8c9/Engine/Source/Runtime/Engine/Classes/Kismet/KismetArrayLibrary.h

AutoCreateRefTerm="ArgumentName" meta specifier on UFUNCTION seems to be what you looking for

In case of Set members it is custom K2Node and those can set pin to whatever freely

Why would you return a reference? You already have the value.

What am I missing here?

Edit: Serious question. I have seen the syntax: int&, but I’ve never used it in practice. Neither have my coworkers. They haven’t even seen that syntax.

This article does offer some insight:
https://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/

What use case?

Apparently, it’s faster than return-by-value, so that’s an interesting optimization.

TIL: Return-by-Reference can be used to acquire a reference to a variable. This is more efficient than passing around an entire array.

Big discussion at the whiteboard here.

The int& syntax I am using is needed to creating a K2 node (a custom BP node). The editor expects specific declarations for the node to work with blueprints. There are also pure C++ use cases for returning a reference, which (as you linked) there is quite a bit of info about, but it’s largely irrelevant to my question. It’s more about the “glue code” for the UE editor to recognize an output as a reference, which there is virtually no documentation/discussion about.

Thank you for the answer! I read a fair bit of the source code trying to find an answer, specifically in the UK2Node_GetArrayItem UK2Node class, and its AllocateDefaultPins().
It’s still not 100% clear, and my attempts at creating a node that does what I need still fail, because reading the source is not the same as verbose documentation.
For example, the AutoCreateRefTerm enum value for metadata literally has no comments above it in ObjectMacros.h, meaning there’s no easy way to deduct its use case and implications.