Change menu and switch script

Hi everyone !

I'm trying to find a way to change the script of MENU B (containing three values X, Y, Z) based on the selected value of the MENU A (containing three values A, B, C. Each of them sending messages) then once the script of MENU B is changed, it will also change the values and the messages of the three switch.

To find a solution I was thinking of applying the following script only on the MENU A.
It's totally wrong but it's just an attempt for now:

if (value === "A") return
{
send ('midi:my_device', '/control', 1, 1, 1)

	//First value of MENU B
	if (get('MENU_B') === "X") {
  	set("switch_1", 1)
  	set("switch_2", 1)
  	set("switch_3", 1)
    }

    	//script for switch_1
  		if (value == "1") 
		{
		send("midi:my_device","/control", 3, 3, 80)
		}
  	
	//Second value of MENU B	
	if (get('MENU_B') === "Y") {
  	set("switch_1", 2)
  	set("switch_2", 2)
  	set("switch_3", 2)
	}

    	//script for switch_2
		if (value == "2")
		{
		send("midi:my_device","/control", 3, 4, 80)
		}

	//Third value of MENU B
	if (get('MENU_B') === "Z") {
  	set("switch_1", 3)
  	set("switch_2", 3)
  	set("switch_3", 3)
	}

    	//script for switch_3
		if (value == "3")
		{
		send("midi:my_device","/control", 3, 5, 80)
		}
}


//Second value of MENU A
if (value === "B") return
{
send ('midi:my_device', '/control', 1, 1, 1)

	//First value of MENU B
	if (get('MENU_B') === "X") {
  	set("switch_1", 2)
  	set("switch_2", 2)
  	set("switch_3", 2)
  	}

    	//script for switch_1
    	//changed to channel 4
  		if (value == "1") 
		{
		send("midi:my_device","/control", 4, 3, 80)
		}

	//Second value of MENU B
	if (get('MENU_B') === "Y") {
  	set("switch_1", 3)
  	set("switch_2", 3)
  	set("switch_3", 3)
	}

		//script for switch_2
		//changed to channel 4
		if (value == "2")
		{
		send("midi:my_device","/control", 4, 4, 80)
		}

	//Third value of MENU B
	if (get('MENU_B') === "Z") {
  	set("switch_1", 1)
  	set("switch_2", 1)
  	set("switch_3", 1)
	}

		//script for switch_3
		//changed to channel 4
		if (value == "3")
		{
		send("midi:my_device","/control", 4, 5, 80)
		}
}


//Third value of MENU A
if (value === "C") return
{
send ('midi:my_device', '/control', 1, 1, 1)


	//First value of MENU B
	if (get('MENU_B') === "X") {
  	set("switch_1", 3)
  	set("switch_2", 3)
  	set("switch_3", 3)
  	}

    	//script for switch_1
    	//changed to channel 5
  		if (value == "1") 
		{
		send("midi:my_device","/control", 5, 3, 80)
		}

	//Second value of MENU B
	if (get('MENU_B') === "Y") {
  	set("switch_1", 1)
  	set("switch_2", 1)
  	set("switch_3", 1)
	}

		//script for switch_2
		//changed to channel 5
		if (value == "2")
		{
		send("midi:my_device","/control", 5, 4, 80)
		}

	//Third value of MENU B
	if (get('MENU_B') === "Z") {
  	set("switch_1", 2)
  	set("switch_2", 2)
  	set("switch_3", 2)
	}

		//script for switch_3
		//changed to channel 5
		if (value == "3")
		{
		send("midi:my_device","/control", 5, 5, 80)
		}
}

In the end, if let's say you select the value B from MENU A, and the value X from MENU B it would not put all the three switch to their first value but to their second value and the three switch will also have different messages since the MENU B would have a different script thanks to the MENU A.

Probably quite complex to understand but i feel like the solution is not that far.

I attached the session below for you to have a better look at it:
Change menu and switch script.json (11.1 KB)

Looking forward to hearing from you,

Swayrian

I think I made a tiny bit of progress on this problem but I feel like I'm missing an extra script to make those two menus work together.

Here is a simplified version:

Simplified version.json (7.3 KB)

What I'm trying to have is when MENU A is set to its value A, MENU B is configured as follow:

if (value == "X") 
{
  set("switch_1", 1)
  set("switch_2", 1)
}

if (value == "Y") 
{
  set("switch_1", 2)
  set("switch_2", 2)
}

And when MENU A is set to B, I would like the MENU B to keep its same value X and Y but now they would be configured as follow:

//X is now set to value 2 of the two switch
if (value == "X") 
{
  set("switch_1", 2)
  set("switch_2", 2)
}

//Y is now set to value 1 of the two switch
if (value == "Y") 
{
  set("switch_1", 1)
  set("switch_2", 1)
}

Would that be possible to achieve ?

Swayrian

Many ways to do it, here is an example with ternary conditions in menu b's script. I added a line in menu a's script as well to update menu b when menu a changes.
Simplified version.json (7.5 KB)

That's pretty cool, never used ternary conditions before !

But now in the case where we have more than two values on the MENU_A and a switch (for instance 3 values each), how to insert it into the script ?

I was trying the following but it's not working:

var menu_a = get('menu_A') 

if (value == "X") 
{
set("switch_1", ( (menu_a == 'A' ? 1 : 2) || (menu_a == 'C' ? 3 : 0) ))
}

Is there a better way of writing it ?
Maybe using conditional statements instead of ternary conditions ? I currently have no clue how to write it.

Swayrian

You can chain ternary conditions, like so:

menu_a == "A" ? 1 : menu_a == "B" ? 2 : 3

or in separated lines

menu_a == "A" ? 1 :
menu_a == "B" ? 2 :
3

Note that this considers menu_a has 3 options (A,B,C) and one of them is always selected.

Hope it helps.
P.S: I know I own you an answer about custom module, I'll do it when I have more time ^.^

1 Like

Work like a charm, thank you so much @ClelsonLopes !

By the way, do you know how I could define the default property of MENU_B to let's say "Y" when loading the session ?
This case is quite tricky to me because as you could see its values are triggered by MENU_A.

Looking forward to also read your answer about custom module, many thanks :pray:

Swayrian

If using the default property doesn't work you can create a script with event set to once and call set("MENU_B", "Y") with it.

1 Like

Thank you for your response @jean-emmanuel !

Is this what you meant by create a script ?

Because I'm still not able to define the default property of MENU_B to "Y" with the script widget when loading the session. MENU_B is undefined.

Do we need to add anything else on the script in order to call set("MENU_B", "Y") ?
(Changing Y to X in this case is working but first I would need to select a value on MENU_A
so I'm not sure what is going wrong now)

Swayrian

Since you need MENU_A to have a value, call set("MENU_A", "A") before set("MENU_B", "Y") .

1 Like

Thank you so much for your help @jean-emmanuel !
Now I have a better understanding of what is possible with a script widget - You truly rock :pray:

Swayrian

Hello Swayrian,

Would you mind please send me the "ultimate code" ? I'd like to understand it, trying it. Reading the whole thread, i don't get it... :upside_down_face: :thinking:

Thank you

Hi @Sylvain,

Sure ! Here is the ultimate session:
Change menu script.json (7.0 KB)

Additionally, I added the following on the default property of MENU_B so that whenever you load the session, "Y" will show up on the menu.

JS{{
return Object.values(@{this.values})[1]
}}

Swayrian

Thank you very much :wink:

Hi @jean-emmanuel,

Not sure if I'm doing something wrong or it's a bug but when I tried:

JS{{
return Object.values(@{this.values})[1]
}}

the value Y is showing up whenever I load the session as expected (on computer) but whenever I start opening my web browser on my touchscreen it's not showing up anymore on the menu.

Any ideas on how to fix it ?

Swayrian