I’ve been trying to figure out how to encapsulate some of the logic in my blueprints into reusable functions and macros.
One speedbump I’ve ran into is that much of my duplicate code is somewhat stateful (requiring to set local variables). I could encapsulate everything into functions if it was possible to pass my blueprint’s float variable into a function as a reference, then call get/set on it within the function.
Does anyone know if this or something like this is possible?
When you make inputs for Functions or Macros, if you expand the options for your Inputs there will be a check-box option for Pass-By-Reference–is that what you’re looking for?
Why can’t we set a variable as a reference or have pointers in blueprints? So objects/actors variable are references but i can’t have a float variable as a reference, it can only be a copy? Im working with umg, so let’s say i want a float in my player pawn to be on the screen, i have to get a reference of my pawn and then find my float, i can’t just say this float is a reference/pointer to the float in my pawn.
Set your variable to Pass-by-Reference. Inside your function, drag off of the passed variable pin on your Input node and search for “set by ref”. You should get the appropriate Set Variable (by ref) node as an option.
for me this is not enough, I’m trying to get a random stream from a blueprint I use for global objects, then I set it to a variable so I can easily use it without having to use multiple “get all actors of class” and “get” nodes. The problem is when I set the random stream to a variable it seems that a copy is created, so I have to always use the global random stream along with those two nodes I mentioned, otherwise every call will generate the same random numbers since it’s using a copy of the global variable that didn’t change state between calls.
I solved my problem by using a macro instead of a variable (to avoid using “get all actors of class” every time) and it works. I was going to make a huge post about this problem, asking for a way to set variables as references instead of creating copies but this might actually be more confusing, specially for non-programmers.