What does Pass-By-Reference do?

I’ve been working with UE4 for 1-2 months now so I’m pretty familiar with most of the Blueprint/Event Graph stuff, however I am still a bit confused about the Pass-By-Reference checkbox on function/event inputs.

I found plenty of threads here talking about Pass-by-Ref problems, but none that actually explain what it is, so I looked online and found a pretty straightforward explanation here:

But I would really appreciate if someone could give me a quick example within the context of Blueprints to help me fully understand this and how it should be used properly.

Thanks!

1 Like

I found this question when I was looking for more information about this same subject. Since this was asked almost a year ago with no response I’ll try and answer it myself with what little I know.

Giving a function an input that has pass-by-ref enabled allows you to modify what ever variable that you plugged into that function when calling it. Normally, without using pass-by-ref you would have to know exactly what specific variable you want to assign a value to and assign it to that specific variable in the function, or return the value at the end of the function and assign it outside and after the function was finished. Pass-by-ref allows you to tell the function that whatever variable I plugged into this function, that’s the variable I want to change.

Lets say you have a function that doubles a character’s attribute score. You have 6 different attributes but you only want to double one attribute at a time. Now you could set up the function to receive the attribute score you want to change as a normal variable, process it inside the function to double the value, return the new score and then assign the value back to the attribute outside the function. You could also make 6 different functions that double only a specific attribute each. The best method perhaps is making one function and using pass-by-ref to calculate and assign it back to the plugged in variable in one function. That way you could use one function to double any attribute you plugged into it.

Checking the pass-by-ref box on the input variable for a function turns the variable to a diamond shape instead of a circle for easy identification. From what I can tell though, to use it properly you can not assign it to a local variable because the local variable does not retain this ability. To assign it back to the input variable you have to drag off the input node reference variable (the diamond) for it to work. To assign anything to it drag off the diamond and call “Set (by ref)”. I’ve only tried this with integers.

Sorry for the long winded post, that’s why I usually leave it to others who are better at explaining these things.

17 Likes

Appreciate the follow through!

thanks, I’m looking for the answer of a question “pass by reference”. thanks again.

For visual learners and as a supplement to BackedUp20’s answer:

4 Likes

So is there any way once I pass the variable as reference, can I make a local variable that takes that reference and retains it’s pointer ability?
If not this way of working with references is very impractical (in terms that makes everything visually more cluttered).

I would like to know this as well. Though after testing it just now it doesn’t seem to work. Updating the local variable doesn’t update the original reference. Hoping there is some clever workaround as it does make it difficult to keep large function graphs nice and tidy.

This is only for Blueprints.

You don’t necessarily need the local variable, if what you’re trying to avoid is the long wires off the reference. In a function, you can “get” any of the inputs as a variable node just by right-clicking and typing the name of the input (It’s usually found at the very bottom). When doing so, the variable retains its reference status.