Why ever promote to variable, when you can just use the pin output?

Hi, say you call a function which outputs some variable, which you want to modify the value of, or just read.
Why is it a good idea, or not, to promote it to a (local) variable?
Thanks.

Hey @AllGamesSuck!

So the first point is cleanliness and keeping your graph organized. Especially a variable you’d use over and over again in different spots - spaghetti code is to be avoided, readable code is more important than functional code, not only to a team but even when you’re a solo dev. 6 months down the road you need to be able to read it, too!

Another point would be that sometimes things need to be saved, this is primarily involving ref and copy GET nodes for Arrays.

Lastly, and most importantly, variables may need to be called upon from outside the blueprint. In order to do so, it needs a variable to be set in order to reference it.

Hope that helps!

3 Likes

I’ll just add that function inputs are already local variables.

2 Likes

Even if they’re passed by reference?

Yes, but then you’d set the value like so:

Whatever float you pipe in will be increased by 5. And no additional local variables were created here. You can, obv:

Now we’re adding 6 to the provided float. Merely wanted to highlight that:

1 Like

So it seems like it depends on use case? I was previously led to believe it might crash the engine or break the code somehow, if not promoting to variable.

I have different view on it:
Why NOT to promote to local variables?

So you thinking why it is required to promote. But why? First you must stop and think about references variables etc. to decide you must promote or not. While promoting everything to local variable, can be done out of habit, and almost never hurts your code.

  • Now performance reasons. Really in blueprints it does not matter, let me explain. Blueprints >> blueprints processor/virtual machine >> C++ >> machine language. Each of that transfers adds 1.5 up to 10-100 times slowdown. So adding that single step of promoting to local variable is nothing compared to running that code in blueprints. You want squeeze performance, then translate that function into C++, blueprintable one.
  • Maybe memory, well i bet you have multiple plugins running that you do not need, and nobody cares. So few more bytes even kb will not change much.
  • clean code, well having everything promoted to local variables in function, makes code look better.

So i kind of do not see any reason to not promote variables to local.

1 Like

I’d say so.

  • function inputs are already local variables, no need to duplicate them
  • if you want to take something apart inside a function, reassemble it and return it, I’d definitely go out of my way and create local variables. Safer and more convenient. This is often critical and necessary because BPs will create a lot of copies:

If you think you’ve modified the incoming array :point_up: , think again. The Loop creates a bunch of copies and we’ve done squat.

As opposed to:

Especially important to remember when you work with maps, structs, and arrays - lots of gotchas! Creating local variables prevents you from falling into the BP trap because you know you’ve made a copy and must handle it.

  • if I need to perform some calculations many times, I’d definitely create a local var and cache a value
1 Like

not to help but to point out that

This is not true.
I actually often forgot what I did last night :joy:
if you can remember 6 months you actually are legendary.
so readability and arrangement is far more important then anything else.

1 Like

++1 to this.

There is always point in project when you just do not remember what this code does, and where it was used. Worse is when you made function, tested it, decided it is good, but then moved to another piece of code. And forgot to implement/use that function. Then you remember it took you 2 days to make it neat with correct results, but you do not remember why and how to use it. :smiley:

That is reason i always maake stupidly big comments explaining whole things, all together with any stupid or silly MNEMONIC i think of. Sometimes remembering that you nicknamed piece of code “A bloated belly” does that zap to your memory. So i write “this is a bloated belly of code”

2 Likes