$ use in matrix script field

Hello,

I am diving into matrix usage, but got stuck. I tried to use “$” in script field, but it’s not working. I wonder if it’s even possible to use it there.

To explain properly I have uploaded example of what I am trying to achieve.
On the left hand side I created fader, 3 buttons that change fader id (with local feedback - changing colors when button is pressed), 3 variables where fader values are saved and 3 text fields that read out values.
On the right hand side I tried to replicate the setup using matrices for buttons and variables. Also I have input field to define count of buttons and variables. Saving values in variable matrix works fine, but buttons I can’t figure out as they are executing script. Also I am not sure how to set up color feedback for matrix buttons. Any suggestions how to achieve it?
Additional question about matrix styling: How can I define button size when using matrix in a way that it goes over container boundries? I guess it has to be set in css field?

Hopefully you can point me into right direction how to solve this.
Camera Control v3.json (157.4 KB)

Hi
I don’t know if it’s can help you but this a script that I have in matrix button

   JS{{
    var props = {}
    var labels = ["mute", "solo"]
    props.mode = "toggle"
    props.label = labels[$]
    props.target = @{variable_midi_port}
    props.on = 127
    props.off = 0
    props.address =  "/control"
    props.preArgs = [@{parent.variables.mc}, $+43+@{this.variables.n}]
    return props
    }}

It’s not possible, $ is only available in the props property.

buttons I can’t figure out as they are executing script

The buttons do trigger the matrix’ script but you won’t be able to know which button was pressed here (in next release, the id variable will tell you which button was pressed).

Also I am not sure how to set up color feedback for matrix buttons. Any suggestions how to achieve it?

It seems you’re trying to build a switch widget out of multiple button widgets, I’d advise a much simpler solution that works since v1.6.0: use toggle buttons with ids that start with the same prefix (in case of a button matrix, that would be the matrix’s id since the buttons’ id are matrix_id/x by default), and set their script as follow:

if (value) set('matrix_id/*', 0, {sync: false})

In your props property it would look like this:

props.script = "if (value) set('matrix_id/*', 0)"

This will turn off all matching widget (the wildcard “*” tells O-S-C to target every widget whose id starts with “matrix_id/” except the widget that triggered the action.

1 Like

Switch this way in matrix works indeed, which is exactly what I was looking for. However I think there is a bug - I have a input field which defines how many buttons are in matrix, as soon as i change number lets say from 3 to 5 then I can only press either button 4 or 5 but not previous 3. If I save the session, close it and reopen, all 5 buttons work, until I change the input again. Example attached.

As for my previous question about $ usage in script field:
Is it possible to define script in matrix props? I tried writing this:

JS{{
var props = {}
props.mode = “toggle”
props.label = “Fader " + [] props.script = "set("fader_variable", [])”
return props
}}

But as soon as I add props.script all previous options are getting ignored and variable isn’t changed. Maybe something wrong in syntax?

Matrix Input.json (9.0 KB)

I think there is a bug

Thanks for the report, I’ll check this out. (edit: found and fixed for next release)

Maybe something wrong in syntax?

Yes, it’s a quote nesting issue, the most simple way to solve it is to use single quotes instead of double quotes either around fader_variable or around set(...).

Side note: you are writing [$] where $ would suffice, it happens to work here but may fail in many case (square brackets are used to define arrays in javascript)

So I ended up using single quotes:

JS{{
var props = {}
props.mode = “toggle”
props.label = "Fader " + [] props.script = "if (value) set('button_matrix/*', 0)", 'set(fader_variable, )’
return props
}}

but it still won’t change fader_variable

Also if I put quotes around fader_variable, it breaks all properties of matrix.
Is script executed in a different way in matrix buttons rather then on normal button?

I think you got a bit confused with the quotes here… plus you can’t put multiple statement after an if condition unless you use curly brackets : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else#Syntax
It should look like this:

props.script = "if (value) {set('button_matrix/*', 0); set('fader_variable', VALUE_NEEDED_HERE)}"

Also note the use of semi-colon between the two set() statements, it’s not needed when there’s a newline between the statements but since we are writing the script on a single it’s required.

This solves my problem.

Thank you very much for all the help!

2 posts were split to a new topic: Client sync issue