Is Blueprint automatically optimized with local variables ?

I’m wondering about something for some time now…

When you create wires between your value pins, do they get re-evaluated each time some node acces it ?

Or does the c++ equivalent of the nodes see what you intend to do, if several nodes use a value with the same wire, that it somehow caches this result in a variable ?

For example this node setup:

Is the lerp node and getAttractPointonCore function reevaluated for each node that accesses it? So then i better put those values in a variable right ?

1729da5d2bea90021a01fbe750c9462d4120219b.jpeg

On a similar note, does it make a difference execution wise if you connect the same get node value with multiple wires or just using another node?

From what I understand, using a regular function like those in the screenshot will “save” the output, so the first one is only executed once, even though three other functions gets its output data.
However, if that was a pure const function, and you can try this with the random float node, every new function or every new variable that gets its data, it executes. So in this example, you’d end up with different random values.

So in your lerp example, the lerp executes twice, but “getAttractPointonCore function” only once.

So, what about a node like “get overlapping actors” which returns an array, and then foreachloop? Does that execute every loop? I was going to say that I don’t know but then I decided to just make a pure function and try it - It does. I made an array of 3 members to output and interestingly, it executes 4 times.

Then something like this:
puref.png
The new function only executes once, whereas if you were to save the first int to var A and the seconds to var B, it would execute twice.
So with pure functions with multiple outputs it may be a good idea to create a struct to store that data so it only executes once.

Good to know. It isn’t always obvious is it.

Concerning the Array example… it reevaluates the function that resulted in the array for each loop body? That is odd. So, it is better to first store the resulting array i suppose.

For example this would execute the search for each loop body and once for the completed exec ?

So, like this it would just execute once… gett all instances overlapping sphere sounds like an expensive function to call every time… This should speed things up a lot !

Actually testing previous example … It’s not really faster. I suppose setting an array has it’s costs too. That node setup is run 8 times per second…

Is setting an array to a variable making a copy of it or a reference ?

Do correct me if i’m wrong, but I believe it depends on the variable type. Setting an array of objects sets by reference. Setting an array of floats will create a copy. There’s also set-by-ref node.

It may be faster than you think it is, computers are fast and 8/s is nothing.
I also think it depends on the amount of instances you have. If you have 100, it iterates over 100 instances to test if they are in range of the location.
If you have a million or more…
Again, 8/s isn’t much, change it to 100 or 1000 times a frame and you will probably notice a difference between setting the array variable and executing the function foreach loop.

There’s one thing I want to add to Everynone’s reply and that is that if you pass an array to function, then the array you get inside that function is a reference to the array.

This picture shows which array is being modified.

Whenever I think I’ve figured out how arrays and structs work, there’s always something extra that catches me by surprise. For example, there’s a difference between how CustomEvents and Functions handle things: