1

I'm using the js code below to do the following:

  1. See if any objects with a .varname name exist
  2. Delete them if they do
  3. Create new ones with a certain .varname
  4. Connect the objects together

The code works for a single iteration, but I'd like to set a loop going so it repeats the function from a value of 1 up to and including the value of the global variable (g.channelcount).

Any help is welcomed!

function bang(){
  g.channelCount = buff.channelcount();//gets buffer channel count

  var deleteWaveform = this.patcher.getnamed("waveform");//delete waveform and fromsymbol objects
  var deleteSymbol = this.patcher.getnamed("symbol");
  this.patcher.remove(deleteWaveform);
  this.patcher.remove(deleteSymbol);

  var waveform = this.patcher.newdefault(96, 756, "waveform~");//creates new waveform and fromsymbol objects and gives them a scripting name
  var symbol = this.patcher.newdefault(186, 698, "fromsymbol");
  waveform.varname = "waveform";
  symbol.varname = "symbol";
  post(g.channelCount);

  var jsBox = this.patcher.getnamed("js_wave");//connects the objects together
  this.patcher.connect(jsBox, 0, waveform, 0);
  this.patcher.connect(jsBox, 1, symbol, 0);
  this.patcher.connect(symbol, 0, waveform, 0);

  outlet(1, "name loop");//loads the loop buffer into the waveform object
  }
}
JohnH
  • 2,713
  • 12
  • 21
  • 1
    Hi Sam, what you are looking for is a "for loop", so a quick Google search for "JavaScript for loop" should put you on the right path – FrostyZombi3 Jun 07 '20 at 21:32

1 Answers1

0

As @FrostyZombi3 mentioned, you can do this with a for loop:

var channelCount = buff.channelcount();

for (var channel = 1; channel <= channelCount; channel++) {
  post(channel, '\n');
  // do the delete/create work using the channel variable
}
swithinbank
  • 1,127
  • 1
  • 6
  • 15