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.
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.
For visual learners and as a supplement to BackedUp20's answer:
answered Nov 18 '16 at 02:24 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here