Setting ControlCallback w/ Loops ?
-
Can someone please advise on this....
im trying to learn how to set control callbacks with loops for obvious reasons when dealing with large amounts of components.
I know i've seen an example of this before somewhere but I cant remember where, so I've been trying to do it from scratch but i've been stuck. Can you please advise?
Be nice, this is my first time doing this.

const var SimpleGain1 = Synth.getEffect("Simple Gain1"); const var SimpleGain2 = Synth.getEffect("Simple Gain2"); const var SimpleGain3 = Synth.getEffect("Simple Gain3"); const var SimpleGain4 = Synth.getEffect("Simple Gain4"); const var SimpleGain5 = Synth.getEffect("Simple Gain5"); const var SimpleGain6 = Synth.getEffect("Simple Gain6"); const var SimpleGain7 = Synth.getEffect("Simple Gain7"); const var SimpleGain8 = Synth.getEffect("Simple Gain8"); const var samplers = [ AudioLoopPlayer1Sampler, AudioLoopPlayer2Sampler, AudioLoopPlayer3Sampler, AudioLoopPlayer4Sampler, AudioLoopPlayer5Sampler, AudioLoopPlayer6Sampler, AudioLoopPlayer7Sampler, AudioLoopPlayer8Sampler ]; const var SampleVolumeKnobs = [Content.getComponent("knbVolume1"), Content.getComponent("knbVolume2"), Content.getComponent("knbVolume3"), Content.getComponent("knbVolume4"), Content.getComponent("knbVolume5"), Content.getComponent("knbVolume6"), Content.getComponent("knbVolume7"), Content.getComponent("knbVolume8")]; inline function onSampleVolumeKnobsControl(component, value) { for (i = 0; i < samplers.length; i++) // used samplers.length to be difficult. im using this length somewhere else also so it was convenient { i.setAttribute(0, value); } }; inline function allSampleVolumeKnobs(component, value) { for (i = 0; i < 7; i++) { if (value) { SimpleGain[i].setControlCallback(onSampleVolumeKnobsControl); } } }I have a good feeling that I goofed this. lmao.
-
Is that the full script? You don't seem to ever be calling allSampleVolumeKnobs ???
-
@Orvillain sheet. lol. See.. I told you im goofing it. all the loops got me in a loop. lol. Im not exactly sure how to go about that at the moment. Im definitely mixed up a little bit but still trying to figure it out.
-
const TheGains = []; const SampleVolumeKnobs = []; const NUM_COMPONENTS = 8; for(i = 0; i< NUM_COMPONENTS; i++) { TheGains[i] = Synth.getEffect("Simple Gain" + (i+1)); SampleVolumeKnobs[i] = Content.getComponent("knbVolume" + (i+1)); SampleVolumeKnobs[i].setControlCallback(onSampleVolumeKnobsControl); } -
@Chazrox
Or even shorter...const TheGains = Synth.getAllEffects("Simple Gain"); const SampleVolumeKnobs = Content.getAllComponents("knbVolume"); for(k in SampleVolumeKnobs) k.setControlCallback(onSampleVolumeKnobsControl); -
Or even shorter...
// Don't do this, I'm being silly for (k in Content.getAllComponents("knbVolume")) k.setControlCallback(onSampleVolumeKnobsControl); -
@d-healey
MASTER OF THE UNIVERSE LEVEL! 
-
@Lindon @Oli-Ullmann @d-healey

Thank Yous! ha.

-
@d-healey said in Setting ControlCallback w/ Loops ?:
Or even shorter...
// Don't do this, I'm being silly for (k in Content.getAllComponents("knbVolume")) k.setControlCallback(onSampleVolumeKnobsControl);I always thought there was "no guarantee of ordering" in getAllComponents.....
-
@Lindon I think it grabs them in the order they are in the component list. I never really thought about it though but I haven't ran into any issues.
-
@d-healey yeah..maybe then , however your suggestion makes the callback difficult in that there is no indexOf(component) to show you which one is being called on...unless theres some fu I dont know about...oh I just noticed your "dont do this comment..>"....
-
@Lindon I did put a disclaimer not to use it. :)
-
@Lindon said in Setting ControlCallback w/ Loops ?:
const TheGains = []; const SampleVolumeKnobs = []; const NUM_COMPONENTS = 8; for(i = 0; i< NUM_COMPONENTS; i++) { TheGains[i] = Synth.getEffect("Simple Gain" + (i+1)); SampleVolumeKnobs[i] = Content.getComponent("knbVolume" + (i+1)); SampleVolumeKnobs[i].setControlCallback(onSampleVolumeKnobsControl); }@Chazrox ...and now your call back looks like this:
inline function onSampleVolumeKnobsControl(component, value) { pos = SampleVolumeKnobs.indexOf(component); //tells us which control is being called... TheGains[pos].setAttribute(TheGains[pos].Gain, value); }; -
@Lindon Thank You
Im gonna try this in a bit and get back to you. Appreciate it! Love the energy. 
-
@Lindon
Yes, the components will be added in the order specified in the component list.As for the index, I use a function that I call first in the callback and that returns the index. Something like this:
inline function getIndexAsInt(component) { local index = component.getId().replace("knbVolume", "").getIntValue(); return index; } inline function getIndexAsString(component) { local index = component.getId().replace("knbVolume", ""); return index; }The return value can then be used in the callback to determine the correct component....
-
@Oli-Ullmann said in Setting ControlCallback w/ Loops ?:
local index = component.getId().replace("knbVolume", "").getIntValue();
If you are storing all your components in an array you can get the index directly using
indexOf -
@d-healey
Yes, that's right! Thanks for the tip! :-) -
C Chazrox has marked this topic as solved