HISE Logo Forum
    • Categories
    • Register
    • Login

    How do you create an array of gui elements?

    Scheduled Pinned Locked Moved Scripting
    4 Posts 3 Posters 1.2k 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.
    • E
      elanhickler
      last edited by

      How do you... or how should you go about creating an array of GUI elements?

      Take for example this untested code:

      ArpGateLabels = [];
      for (i = 0; i < SliderPack.getNumSliders(); ++i)
      {
      	local var label = Content.addLabel("Label", 11, 422);
      	
      	Content.setPropertiesFromJSON("Label", 
      	{
      		"text": "127",
      		"width": 33,
      		"height": 29,
      		"fontName": "Tahoma",
      		"fontSize": 12,
      		"alignment": "centred",
      		"multiline": 0
      	});
      	
      	ArpGateLabels.push(label);
      }
      

      Now, we have 32 labels named "Label" right? All referring to the same GUI object. "Label" is then a global variable in a sense, it fills up the global namespace. Is there a way to prevent this and refer to labels by their object and not by their string id?

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

        There are a few ways but since you have a counter in the loop you could do ArpGateLabels[i] = label and then refer to them by their index.

        Actually looking at your example again it seems quite inefficient to declare a temp variable - label - each time you go around the loop just to put it in an array. I'd do it like this ArpGateLabels[i] = Content.addLabel("label" + i, x, y); And then set the properties of the label inside ArpGateLabels

        If you don't want to refer to them by index you could make ArpGateLabels an object instead of an array and use the label's name as the key.

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

        1 Reply Last reply Reply Quote 0
        • E
          elanhickler
          last edited by elanhickler

          I thought the point of local redeclaration is to not clutter the namespace. Also, we don't need to efficiently create labels on init.

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

            Actually using a temp variable doesn't make a difference performance wise because the variabe just stores a lightweight reference internally and doesn't duplicate the actual label.

            Using the new array.reserve() function before the loop would make things a bit faster though...

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

            19

            Online

            1.8k

            Users

            12.1k

            Topics

            105.2k

            Posts