How do you create an array of gui elements?
-
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?
-
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 ArpGateLabelsIf 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.
-
I thought the point of
localredeclaration is to not clutter the namespace. Also, we don't need to efficiently create labels on init. -
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...