Announcement

Collapse
No announcement yet.

Is Blueprint automatically optimized with local variables ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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 ?

    Click image for larger version

Name:	2017-03-19 13_32_46-Chrystal_01_Pooled_BP_.jpg
Views:	1
Size:	223.8 KB
ID:	1213318


    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?

    #2
    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:
    Click image for larger version

Name:	puref.png
Views:	1
Size:	32.2 KB
ID:	1124914
    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.
    Last edited by ste1nar; 03-19-2017, 10:27 AM.

    Comment


      #3
      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 ?

      Click image for larger version

Name:	2017-03-19 15_58_28-Prototype01 - Unreal Editor.jpg
Views:	1
Size:	106.9 KB
ID:	1124916

      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 !

      Click image for larger version

Name:	2017-03-19 16_00_48-Prototype01 - Unreal Editor.jpg
Views:	1
Size:	113.4 KB
ID:	1124917
      Last edited by FrederickD; 03-19-2017, 11:18 AM.

      Comment


        #4
        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 ?

        Comment


          #5
          Originally posted by FrederickD View Post
          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.

          Comment


            #6
            Originally posted by FrederickD View Post
            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...
            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.

            Originally posted by FrederickD View Post
            Is setting an array to a variable making a copy of it or a reference ?
            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.

            Click image for larger version

Name:	arr.png
Views:	1
Size:	114.3 KB
ID:	1124920

            This picture shows which array is being modified.
            Last edited by ste1nar; 03-19-2017, 12:11 PM.

            Comment


              #7
              Originally posted by ste1nar View Post
              [...]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.
              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:

              Click image for larger version

Name:	modify.PNG
Views:	1
Size:	112.1 KB
ID:	1124921

              Comment

              Working...
              X