HISE Logo Forum
    • Categories
    • Register
    • Login

    Is there a way to limit the scope of variables declared in a loop?

    Scheduled Pinned Locked Moved Scripting
    7 Posts 3 Posters 264 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.
    • VirtualVirginV
      VirtualVirgin
      last edited by

      Here, the variable in question is "columnID":

      // --- inside an inline function ---
      	// init the filterData storage in the container as an object
      	dataTableContainer.data.filterData = {};
      	for (i = 0; i < numVisibleCols; i++)
      	{
      	    local columnID = columnNames[i];
      	    dataTableContainer.data.filterData[columnID] =
      	    {
      	        "searchValue" : undefined,
      	        "selectedFilterValues": [],
      	        "anyAll" : undefined,
      	        "minValue" : undefined,
      	        "maxValue" : undefined
      	    };
      	}
      

      Any use of "columnID" later on outside the loop will try to use that declared version, so throwing errors in callbacks and paint routines.
      I can, of course just rename the variable to "columnID1" or some other variant,
      but wondering if there is a more elegant solution to limiting the scope of the variable to just the loop, so I can reuse "columnID".

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

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

        @VirtualVirgin

        Wrap whatever you're doing in an inline function.

        inline function test()
        {
        	for (i = 0; i < 10; i++)
        	{
        		local count;
        		count = i;
        	}
        }
        
        test();
        
        Console.print(count);
        

        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 It's already inside an inline function though.

          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 And that's the only variable with the name columnID that you've declared in your script?

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

            1 Reply Last reply Reply Quote 0
            • ChazroxC
              Chazrox @VirtualVirgin
              last edited by Chazrox

              @VirtualVirgin said in Is there a way to limit the scope of variables declared in a loop?:

              solution to limiting the scope of the variable to just the loop

              'local' means that its only local to that scope and should be able to use it again within another scope separately. If its being called outside of this inline function, you might have it declared somewhere else.

              When something like this is happening to me I use this and 'find all occurrences' of whatever you searching for. Its good just to be sure. (right click in the code editor to show this list)
              Screenshot 2025-05-01 at 8.32.49 PM.png

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

                I think my question here is being misinterpreted or misunderstood.

                The question again is "Is there a way to limit the scope of variables declared in a loop?".

                In Javascript, there are options for declaring a variable that is limited to the scope of the loop,
                such as "let".

                I understand HISE is not Javascript, but I was just asking if there was something similar in HISE in order to do this
                because "local" and "var" are not limited to the scope of a loop:

                Screenshot 2025-05-02 at 10.41.49 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 said in Is there a way to limit the scope of variables declared in a loop?:

                  Is there a way to limit the scope of variables declared in a loop?"

                  Nope.

                  Local variable scope is limited to the inline function.

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

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

                  31

                  Online

                  1.7k

                  Users

                  11.9k

                  Topics

                  103.4k

                  Posts