HISE Logo Forum
    • Categories
    • Register
    • Login

    Reg - how many?!

    Scheduled Pinned Locked Moved General Questions
    19 Posts 6 Posters 515 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • d.healeyD
      d.healey @DanH
      last edited by d.healey

      @DanH Vars should always be a last resort.

      Read this - https://forum.hise.audio/topic/79/scripting-best-practices
      And this - https://docs.hise.audio/scripting/scripting-in-hise/hise-script-coding-standards.html#variables (scroll down to the "Types" section).

      Why don't you use a single array to store your global cable values? Is this something you could use a broadcaster for instead of a timer?

      Libre Wave - Freedom respecting instruments and effects
      My Patreon - HISE tutorials
      YouTube Channel - Public HISE tutorials

      DanHD 1 Reply Last reply Reply Quote 0
      • DanHD
        DanH @d.healey
        last edited by

        @d-healey haven't used broadcasters before, could be a possibility I guess, I've been following @Christoph-Hart 's guide on how to use global cables thus far.

        https://docs.hise.audio/scripting/scripting-api/globalroutingmanager/index.html
        https://docs.hise.audio/scripting/scripting-api/globalcable/index.html

        DHPlugins / DC Breaks | Artist / Producer / DJ / Developer
        https://dhplugins.com/ | https://dcbreaks.com/
        London, UK

        Christoph HartC 1 Reply Last reply Reply Quote 0
        • Christoph HartC
          Christoph Hart @DanH
          last edited by

          @DanH yeah store them in an array.

          DanHD 1 Reply Last reply Reply Quote 0
          • DanHD
            DanH @Christoph Hart
            last edited by

            @d-healey @Christoph-Hart Argh arrays! 😆

            I might make a small snippet if you fancy helping me out!

            DHPlugins / DC Breaks | Artist / Producer / DJ / Developer
            https://dhplugins.com/ | https://dcbreaks.com/
            London, UK

            d.healeyD 1 Reply Last reply Reply Quote 0
            • d.healeyD
              d.healey @DanH
              last edited by d.healey

              @DanH

              An array is just like lots of normal variables living in one house. So you only need to go to one address and ask for the person you want, instead of going to multiple addresses :p.

              I hope that analogy works...

              Let's imagine you are using reg variables like this

              reg myGlobalCableData0;
              reg myGlobalCableData1;
              reg myGlobalCableData2;
              reg myGlobalCableData3;
              reg myGlobalCableData4;
              
              function myTimerCallback()
              {
                  myGlobalCableData0 = someValue;
                  myGlobalCableData1 = someValue;
                  myGlobalCableData2 = someValue;
                  myGlobalCableData3 = someValue;
                  myGlobalCableData4 = someValue;
              }
              

              You can do it with an array like this

              const myGlobalCableData = []; // Arrays should pretty much always be const
              
              function myTimerCallback() 
              {
                  myGlobalCableData[0] = someValue;
                  myGlobalCableData[1] = someValue;
                  myGlobalCableData[2] = someValue;
                  myGlobalCableData[3] = someValue;
                  myGlobalCableData[4] = someValue;
              }
              

              You might find this useful too:
              https://youtu.be/Y8sraa5ig-M

              Libre Wave - Freedom respecting instruments and effects
              My Patreon - HISE tutorials
              YouTube Channel - Public HISE tutorials

              ? 1 Reply Last reply Reply Quote 2
              • ?
                A Former User @d.healey
                last edited by

                @d-healey said in Reg - how many?!:

                // Arrays should pretty much always be const
                

                Why is this again?

                d.healeyD 1 Reply Last reply Reply Quote 0
                • d.healeyD
                  d.healey @A Former User
                  last edited by d.healey

                  @iamlamprey I don't think there is any benefit to them being var or reg.

                  So unless they're in an inline function, in which case they should be local like everything else, const seems to be the best option, unless you can think of a reason I've missed?

                  Oh and of course inside a regular function they should be var because that's all we have there.

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  ? 1 Reply Last reply Reply Quote 0
                  • ?
                    A Former User @d.healey
                    last edited by

                    @d-healey If there's no performance overhead then I guess it's just good coding practice to make them const since the actual array isn't changing, just what's in it

                    I guess var / reg would only be used if you're pointing it to another array or something :man_shrugging:

                    Christoph HartC 1 Reply Last reply Reply Quote 0
                    • Christoph HartC
                      Christoph Hart @A Former User
                      last edited by

                      @iamlamprey The engine can resolve a dot operator for a const object at compile time, so it improves the performance of function calls.

                      var slow = [1, 2, 3, 4, 5];
                      
                      slow.indexOf(2); // this requires two table lookups to get the function to call
                      
                      const var fast = [1, 2, 3, 4, 5];
                      
                      fast.indexOf(2); // this function call can be resolved at compile time.
                      
                      1 Reply Last reply Reply Quote 2
                      • Matt_SFM
                        Matt_SF
                        last edited by Matt_SF

                        @DanH
                        A const array will always be an array whereas a reg could be something else later :

                        reg myArray = [] ;
                        reg myArray = 42;
                        // this will work. 
                        
                        const myArray = [] ;
                        const myArray = 42;
                        // this won't
                        

                        If I'm not mistaken, it has something to do with memory allocation. That's why it's always a good practice to reserve the arrays's slots number if you know it.

                        const myArray = [] ;
                        myArray.reserve(4);
                        // push 4 variables into the array later
                        

                        Edit : ah, didn't see @Christoph-Hart 's answer

                        Develop branch
                        Win10 & VS17 / Ventura & Xcode 14. 3

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post

                        10

                        Online

                        1.7k

                        Users

                        11.8k

                        Topics

                        102.4k

                        Posts