Matrix : send both triggered button and previous triggered button values

Hi !

Hope you're having a good day !

Let's say i trigger Button A in a matrix.
I need the matrix to retrigger Button A when i trigger Button B...

Button's are used to send midi CC. CC number depends on button's index.

So here i am, trying to use locals in a matrix :

JS{
  var props={}
  props.id="Global_"+$
  var Index=parseInt($)
  
  props.target="midi:OSC"
  props.script= " "
+"if(locals.Indexprecedent !== undefined)"
+"{"
+"send('/control',10,"+(69+$)+",127)"                            // button's CC
+"send('/control',10,(69+locals.Indexprecedent),127)" // previous triggered button's CC
+"}"
+"locals.Indexprecedent = "+Index+"; "
+"send('/control',10,"+(69+$)+",127)"  

  return props
}

Obviously... little bug... Where am i wrong ? Do locals have to be set inside the props.script field, or outside ?

Would it be more appropriate to use stateGet / stateSet ?

Thank you

"if(locals.Indexprecedent !== undefined)"

Each widget has its own locals object, you want to have a shared variable between all your buttons so that's not gonna work, you could try using a custom variables stored in the matrix widget itself:


props.onValue = `
var lastIndex = getVar('parent', 'index')
if (lastIndex !== undefined) {
  // send stuff...
}
// update index
setVar('parent', getIndex('this'))
`

There is a function called getIndex that could simplify your code, you don't need all these string concatenations in order to insert the button's index in the code.

props.script doesn't exist anymore by the way, it still works for compatibility's sake, but you'd better use onValue from now on.

Thank you very much, i have been searching around all this :slight_smile:

Btw, why do i have trouble with backticks ?

props.onValue = `
var lastIndex = getVar('parent', 'Index')
if (lastIndex !== undefined)
{send('/control',10,69+"+$+",127);
send('/control',10,(69+lastIndex),127);}
setVar('parent','Index',getIndex('this'))
`

Returns only the first CC, and this :
bug

while this works perfectly :

props.onValue = ""
+"var lastIndex = getVar('parent', 'Index');"
+"if (lastIndex !== undefined) {"
+"send('/control',10,69+"+$+",127);"
+"send('/control',10,(69+lastIndex),127);"
+"};"
+"setVar('parent','Index',getIndex('this'))"

Because of this line

{send('/control',10,69+"+$+",127);

The "+$+" part doesn't make sense:

69+"+$+"  // returns the string "69+$+"

You could write 69 + getIndex('this') instead

Ok, crystal clear now, thank you :+1: