Check if input connected to javascript patch

fl0's picture

Is there a way to check if an input has been connected to the javascript patch and act accordingly so that it doesn't crash?

I'm developing a javascript patch that mixes 3 structures together defined by a "map" structure. I use a switch to check if the map wants structure 1, 2, or 3 for that index. However, the patch doesn't run if I don't connect all three structures.

I tried checking if the input is 'defined' but that didn't seem to make a difference.

for(i=0;i<numChannels;i++)
      {
         var index = mixStructure[i];
 
         switch(index) {
            case 0 :
               result.outputStructure[i] = 0;
               break;
            case 1 :
               result.outputStructure[i] = inputStructure1[i];
               break;
            case 2 :
               result.outputStructure[i] = inputStructure2[i];
               break;
            case 3 :
               if (typeof window.inputStructure3 !== 'undefined') {
                  result.outputStructure[i] = inputStructure3[i];
               }
               break;
            default:
               result.outputStructure[i] = 0;
         }
      }

Any ideas?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

cwright's picture
Re: Check if input connected to javascript patch

'undefined' is the string constant "undefined" -- the "undefined" object is simply "undefined" (without single quoted, double quotes, etc.).

in QC, unattached inputs are usually not "undefined", but "null".

function (__number outputNumber) main (__structure inputNumber[2])
{
   var result = new Object();
   if(inputNumber[0] == null)
      Log("oh snap!");
   result.outputNumber = inputNumber[0] + inputNumber[1];
   return result;
}

The above prints "oh snap!" when no input structures are attached.

idlefon's picture
Re: Check if input connected to javascript patch

Can't you just use something like "if(inputStructure[i]"! I think it'll work.

idlefon's picture
Re: Check if input connected to javascript patch

Just checked and it works (attached a sample comp)

PreviewAttachmentSize
checks if structure is attatched to JS.qtz5.91 KB