HISE Logo Forum
    • Categories
    • Register
    • Login

    Issues with for loop?

    Scheduled Pinned Locked Moved Scripting
    29 Posts 6 Posters 1.1k 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 @VirtualVirgin
      last edited by

      @VirtualVirgin said in Issues with for loop?:

      Not in this case, just an array.

      In that case instead of using 128 you can use noteOnOffState.length and then the loop will never exceed the number of elements in the array.

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

      VirtualVirginV 1 Reply Last reply Reply Quote 0
      • VirtualVirginV
        VirtualVirgin @d.healey
        last edited by

        @d-healey said in Issues with for loop?:

        @VirtualVirgin said in Issues with for loop?:

        Not in this case, just an array.

        In that case instead of using 128 you can use noteOnOffState.length and then the loop will never exceed the number of elements in the array.

        I just tried that while you were tying this, but I still get an error:
        Screenshot 2024-11-07 at 11.37.01 AM.png

        Here is the array declaration:
        Screenshot 2024-11-07 at 11.36.22 AM.png

        You can listen to my orchestral mockups here:
        https://www.virtualvirgin.net/

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

          @VirtualVirgin Don't use global, use const.

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

          VirtualVirginV 1 Reply Last reply Reply Quote 0
          • VirtualVirginV
            VirtualVirgin @Lindon
            last edited by

            @Lindon said in Issues with for loop?:

            @VirtualVirgin try doing this instead:

            function onController()
            {
                 Console.print(trace(noteOnOffState));
            
            }
            

            but it may well be that noteOnOffState isnt defined or visible to your call back.

            I did use that earlier, but I don't know how to append it to use strings in between each returned index value.

            You can listen to my orchestral mockups here:
            https://www.virtualvirgin.net/

            1 Reply Last reply Reply Quote 0
            • VirtualVirginV
              VirtualVirgin @d.healey
              last edited by VirtualVirgin

              @d-healey said in Issues with for loop?:

              @VirtualVirgin Don't use global, use const.

              Well somehow it works now once I added the
              i + ": "
              to the print command.
              Screenshot 2024-11-07 at 11.45.59 AM.png

              I didn't change the variable type yet.

              const variable will be available across the different callback scripts?

              You can listen to my orchestral mockups here:
              https://www.virtualvirgin.net/

              d.healeyD 2 Replies Last reply Reply Quote 0
              • d.healeyD
                d.healey @VirtualVirgin
                last edited by

                @VirtualVirgin A const will be available throughout the MIDI processor in which it is declared. It won't be accessible from other MIDI processors.

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

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

                  @VirtualVirgin It's still saying undefined for that variable. You haven't solved the problem.

                  Where are you populating the array?

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

                  VirtualVirginV 1 Reply Last reply Reply Quote 0
                  • VirtualVirginV
                    VirtualVirgin @d.healey
                    last edited by

                    @d-healey said in Issues with for loop?:

                    @VirtualVirgin A const will be available throughout the MIDI processor in which it is declared. It won't be accessible from other MIDI processors.

                    And does the scope of a global include the other MIDI processors (out of curiosity)?

                    You can listen to my orchestral mockups here:
                    https://www.virtualvirgin.net/

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

                      @VirtualVirgin said in Issues with for loop?:

                      And does the scope of a global include the other MIDI processors (out of curiosity)?

                      Yes, but there is usually a better method. Global variables lead to less modular code, which is more difficult to maintain and debug. I have yet to use them in any project.

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

                      1 Reply Last reply Reply Quote 1
                      • VirtualVirginV
                        VirtualVirgin @d.healey
                        last edited by

                        @d-healey said in Issues with for loop?:

                        @VirtualVirgin It's still saying undefined for that variable. You haven't solved the problem.

                        Where are you populating the array?

                        No, no! It is working properly.
                        Those "undefined" indexes are not written to yet.
                        Only those which have been written to have a value (1 or 0 here).

                        If you see, index 60 has a value of 1, meaning that middle C has note on.

                        If it matters later, I could initialize the array with all 0s, but I don't know that I need that data yet.

                        You can listen to my orchestral mockups here:
                        https://www.virtualvirgin.net/

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

                          @VirtualVirgin said in Issues with for loop?:

                          Those "undefined" indexes are not written to yet.

                          The error you created this thread for is caused by the values being undefined

                          98ed9d22-c4b6-439a-8a73-cd005108c445-image.png

                          In this situation you need to filter out undefined values, this can be done with a simple if inside the loop.

                          if (!isDefined(noteOnOffState[i])
                              continue;
                          

                          If you're just storing a 1 or 0 in the array then you should probably use a MIDI list.

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

                          VirtualVirginV 1 Reply Last reply Reply Quote 0
                          • VirtualVirginV
                            VirtualVirgin @d.healey
                            last edited by

                            @d-healey said in Issues with for loop?:

                            @VirtualVirgin said in Issues with for loop?:

                            Those "undefined" indexes are not written to yet.

                            The error you created this thread for is caused by the values being undefined

                            98ed9d22-c4b6-439a-8a73-cd005108c445-image.png

                            In this situation you need to filter out undefined values, this can be done with a simple if inside the loop.

                            if (!isDefined(noteOnOffState[i])
                                continue;
                            

                            If you're just storing a 1 or 0 in the array then you should probably use a MIDI list.

                            That error was coming up for trying to print from the loop.
                            That error disappeared when I added
                            i + ": "
                            to the Console.print()

                            The console is printing the loop and the error is no longer in the console.

                            You can listen to my orchestral mockups here:
                            https://www.virtualvirgin.net/

                            1 Reply Last reply Reply Quote 0
                            • VirtualVirginV
                              VirtualVirgin
                              last edited by

                              Without
                              i + ": " +

                              Screenshot 2024-11-07 at 12.11.57 PM.png

                              With

                              Screenshot 2024-11-07 at 12.14.09 PM.png

                              You can listen to my orchestral mockups here:
                              https://www.virtualvirgin.net/

                              Oli UllmannO 1 Reply Last reply Reply Quote 0
                              • d.healeyD
                                d.healey
                                last edited by

                                The error is telling you that the thing you are trying to print is undefined. When you add i + ": " + then you are no longer printing something that is entirely undefined. - However as you can see from the console output the array part of it is still undefined.

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

                                VirtualVirginV 1 Reply Last reply Reply Quote 0
                                • Oli UllmannO
                                  Oli Ullmann @VirtualVirgin
                                  last edited by

                                  @VirtualVirgin
                                  If you do not want to initialize your array with 0 or 1, you could also initialize it with -1.

                                  1 Reply Last reply Reply Quote 0
                                  • VirtualVirginV
                                    VirtualVirgin @d.healey
                                    last edited by

                                    @d-healey said in Issues with for loop?:

                                    The error is telling you that the thing you are trying to print is undefined. When you add i + ": " + then you are no longer printing something that is entirely undefined. - However as you can see from the console output the array part of it is still undefined.

                                    Right, but the
                                    [i]
                                    is the original source of the error

                                    If the array is populated with anything, then it has a length
                                    so that variable should not be undefined.

                                    The "undefined" that is now printing from those indexes are different "undefined" because they are are saying that that particular index is undefined (not written to).

                                    The "undefined" referred to in the original error returns how many indexes are written to, which in essence was saying "0", which was incorrect.

                                    You can listen to my orchestral mockups here:
                                    https://www.virtualvirgin.net/

                                    Oli UllmannO d.healeyD 2 Replies Last reply Reply Quote 0
                                    • Oli UllmannO
                                      Oli Ullmann @VirtualVirgin
                                      last edited by Oli Ullmann

                                      @VirtualVirgin
                                      You probably fill your array according to the note numbers. So 60 for C3. If you now press C3, array element 60 is filled and the array has a length of 61, which means that elements 0 to 59 remain undefined. This is probably where the error comes from. It therefore makes sense to initialize the array with initial values.

                                      VirtualVirginV 1 Reply Last reply Reply Quote 1
                                      • d.healeyD
                                        d.healey @VirtualVirgin
                                        last edited by d.healey

                                        @VirtualVirgin

                                        I'm not really sure what you mean. i is just a number that is being incremented in the loop, it has no knowledge of your array, it is completely separate.

                                        Any array element that doesn't have a value assigned to it is undefined. If you try to print an undefined value you will get an error. When you concatenate the string at the beginning, that doesn't change anything, except now you are printing that string in addition to the undefined value. So you don't get an error because the entire thing you are printing is not undefined, but that array element still is.

                                        const myArr = [];
                                        
                                        Console.print(myArray[0]); // Undefined error
                                        
                                        const myArr = [0, 1, 2];
                                        
                                        Console.print(myArr[0]); // 0
                                        Console.print(myArr[1]); // 1
                                        Console.print(myArr[2]); // 3
                                        Console.print(myArr[3]); // Undefined error
                                        Console.print("Some string" + myArr[3]); // No error
                                        

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

                                        1 Reply Last reply Reply Quote 0
                                        • VirtualVirginV
                                          VirtualVirgin @Oli Ullmann
                                          last edited by

                                          @Oli-Ullmann said in Issues with for loop?:

                                          @VirtualVirgin
                                          You probably fill your array according to the note numbers. So 60 for C3. If you now press C3, array element 60 is filled and the array has a length of 61, which means that elements 0 to 59 remain undefined. This is probably where the error comes from. It therefore makes sense to initialize the array with initial values.

                                          I am going to initialize it with 0s given that this array is just for keeping track of note on/ note off, so it makes sense that it would initialize to read as all notes off.

                                          It is curious however that the length parameter would work and not work based on the conditions I posted above. In either case the [i] variable should be identical.

                                          You can listen to my orchestral mockups here:
                                          https://www.virtualvirgin.net/

                                          d.healeyD Oli UllmannO 2 Replies Last reply Reply Quote 0
                                          • d.healeyD
                                            d.healey @VirtualVirgin
                                            last edited by

                                            @VirtualVirgin The length of the array is how many elements are in the array, not how many values are in it. If you add something at element 60 then the length will be 61.

                                            You can keep track of this stuff in the script watch table:

                                            1ab70f49-323c-4121-91ad-03991a79b72b-image.png

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

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

                                            19

                                            Online

                                            1.8k

                                            Users

                                            12.1k

                                            Topics

                                            104.9k

                                            Posts