User defined Component properties via JSON
-
Hello,
I have added a user defined Component property (an integer to be used as an index for a series of toggling buttons) via the Components JSON.
I was wondering if this can be considered good practice? I find it very handy as said property is predefined for a series of buttons in an array, so once one is clicked the integer returns which one was.Regards,
Giuseppe -
@Giuseppe I'm not sure what you mean, please post a snippet demonstrating it.
-
-
-
@Giuseppe That's not considered good practice. You could store all your components in an array and use the array index, there are other methods of organising your controls also depending on the specifics of the project.
-
@d-healey Thanks David. I was watching your Scripting Best Practices videos about toggling buttons, and I see you use the "continue" command to keep track of which button triggered the callback. As far as I understand it, that gives the string name of the triggering component, but how could I store an index associated to said component? Maybe using an object?
-
@Giuseppe create an array of all the components of a given "type" (your choice), now us array.indexOf(component) to tell you which component is being activated...
e.g. lets say we have 5 buttons...called Button1 to Button5:
const myComponentArray = []; const NUM_COMPONENTS = 5; for(i=0,i<NUM_COMPONENTS ; i++) { myComponentArray[i] = Content.getComponent("Button" + (i+1)); myComponentArray[i].setControlCallback("mycallback"); } inline function mycallback(component, value) { local pos = myComponentArray.indexOf(component); Console.print("Pressed button:" + (pos+1); };
-
@Lindon right, thanks Lindon. I missed the indexOf command, much appreciated !
-
@Giuseppe said in User defined Component properties via JSON:
"continue" command to keep track of which button triggered the callback
continue
is used in loops to skip the current cycle.