Slider and touch variable in a script : touch is sent before value?

I’m trying to use a slider to trigger MIDI note : the value of the slider controls velocity
touching the slider triggers the note

it seems to work with this script :

if (touch !== undefined) {
send(’/note’, 1, 62, touchvalue127)
}

…but something goes wrong :
When I touch the slider a note is triggered, but the velocity used is the previous position of the slider (before the slider was touched), not the current value of the slider. (yes my slider has “snap”: “true” )

When I release the slider, it sends a note off message (velocity = 0) (= OK)

Looks like the script is triggered by the touch variable before the value is evaluated…
Is it the expected behavior ?
What would be the solution to make this work is the expected order ?

(this script cannot be triggered by a change in value, … because value doesn"t necessary changes when user touches, releases and touches again the slider…)

( I have the same issue with a XY widget)

Mathieu

Yes, the touch information may be needed before any value change. The good news is that you can hack it your way:

if (touch !== undefined) {
  setTimeout(function(){
    send("/note", 1, 62, get("this"))
  })
}

When no delay is specified, setTimeout runs the provided function when current tasks are done.

wonderful :slight_smile:

and to make it generate a note-off on release, this seems to work :

if (touch !== undefined) {
  setTimeout(function(){
    send("/note", 1, 62, get("this")*touch)
  })
}

(otherwise a note-on is sent when touch == 1, and another one is sent when touch == 0 )

Thanks

Mathieu

Nice indeed !