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.
-
@d-healey, I think @Giuseppe is adding properties manually to component's JSON (probably by pressing J key)
This works nicely when you read it from script, and seems secure enough. Though I haven't tested with exported plugin, which should be the first thing to check... -
@d-healey Yes, as @ustk said I'm manually opening the JSON of the component and just adding another line with an integer index, which ranges from 0 to number of components for each component. It keeps track of which component triggered a callback that is associated to all of said components.
-
@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.