Recall values of 4 buttons

Hey hey...

mémoshift

Here's a group of 4 buttons.
When the Shift button is off, they behave as a switch.
When the Shift button is on, they behave as toggle buttons.

I need to be able to recall the values they had before shift is toggled, to give them back when shift goes to zero again...

I tried with some pieces of script and variables. I actually have two of those, MémoShiftOn and MémoShiftOff that store the values i need.

But now i'm stuck, i don't achieve to set the values back.

i tried with a script widget :
if( get('shift') == 1 ) set( ["b1_Qpistes", "b2_fQ", "b3_Eq", "b4_Midi"], get('MémoShiftOn') )

Unsuccessfully...

What could be a solution ? For some other stuff Jean-Emmanuel gave me a script using local.previousValue, but here i don't know how to apply that... if it's an option...

Thank you !

You can't pass an array as first argument to set(). To manage the state of multiple widgets you can use stateGet and stateSet:

// get state
var state = stateGet(["b1_Qpistes", "b2_fQ", "b3_Eq", "b4_Midi"])
// recall state
stateSet(state)
1 Like

Ok, thank you (one more time !)

@jean-emmanuel

Pfiouff.. je commence à ramer... je n'arrive pas à utiliser stateGet et stateSet... Aurais-tu la gentillesse de me transmettre un petit exemple avec 2 boutons et un shift ?...

Au passage, un énorme MERCI pour le travail que tu accomplis, et surtout ces étonnantes disponibilité et réactivité dont tu fais preuve !

Well... back again :slight_smile: I tried and tried...

Following what i'v read somewhere here, i've set two buttons, "get" and "set", both on tap mode.

In the script field of "get" :

var état = stateGet(["b1","b2","b3","b4"])

console.log(état)

In the script field of "set":

stateSet(état)

Well.... first of all, i'm glad to now know about console.log... one small piece of knowledge :crazy_face:

And then... my "set" button just do nothing...
As in the console i have the message

.... état is not defined

I tried a few things... adding " "... adding ' '... creating a variable "état" somewhere else... and so many other things...

But still nothing...

Btw, why does console.log(état) returns b3, b4, b1, b2 instead of b1, b2, b3, b4 ?

Thank you

HI @Sylvain,

I have tried

But can't see how to recall

image

recall.json (7.4 KB)

image

So if someone can help us to see the light :slight_smile: and explain "le pourquoi du comment"
Cheers

état is not defined / state is not defined are the key informations here, a javascript variable defined in one script is not available for the other widgets, because each script has its own execution scope (see Scope - MDN Web Docs Glossary: Definitions of Web-related terms | MDN and var - JavaScript | MDN, very important concepts when working with javascript).To share data between widgets, either

  • use a variable widget to hold the state
// save_button
var state = stateGet('...')
set('variable_widget_id', state)

// load_button
var state = get('variable_widget_id')
stateSet(state)
  • use setVar() and getVar() to store the value in the widget's custom variables
// save_button
var state = stateGet('...')
setVar('load_button', 'my_var', state)

// load_button
var state = getVar('this', 'my_var')
stateSet(state)
  • use the globals object that is shared between all scripts (not recommended)
// save_button
var state = stateGet('...')
globals.my_state = state

// load_button
var state = globals.my_state
stateSet(state)
2 Likes

Thank you @jean-emmanuel ! I have been turning around the two first methods for hours... now i get it. :slight_smile:

hi @jean-emmanuel

Thanks for your explanation.

setGetState

Here is an example of the two first solutions.
setGetState.json (13.3 KB)

Next step : use this concept with a matrix. which syntax to get the state of a button matrix.

Edit : for a matrix

state = get('matrix_1')


// save_button
var state = stateGet('matrix_1')
setVar('load', 'my_var', state)

@Greenman you don't need anything different for a matrix, the first line of you snipped is not necessary.

yes it's a typo, a rest of testing code. sorry

monQC

Yep...

I've set the whole thing in just one button. It works well, but now, i'm struggling to have the "recall" for both states "shift ON" and "shift OFF". Trying to have two variables "state ON" and "state OFF". Not successfull yet, but i'm on my way :wink:

ça avance :slight_smile: ...

monSHIFT

Here is my code (script in shift button's property) :

var Boutons_Shift_Off = ["c1","c2","c3"] // déclare Boutons comme l'ensemble des valeurs des 4 boutons
var Boutons_Shift_On = ["c1","c2","c3"] // déclare Boutons comme l'ensemble des valeurs des 4 boutons

if(get('this')==1)                                     // Quand Shift passe à 1
{
var Réattribue_valeur = get('Var_Mémo_Shift_On')       
var Quand_Shift_Off = stateGet(Boutons_Shift_Off)      
stateSet(Réattribue_valeur)                            
set('Var_Mémo_Shift_Off', Quand_Shift_Off)             
}

if(get('this')==0)                                     // Quand Shift passe à 0
{
var Réattribue_valeur = get('Var_Mémo_Shift_Off')    
var Quand_Shift_On = stateGet(Boutons_Shift_On)       
stateSet(Réattribue_valeur)                           
set('Var_Mémo_Shift_On', Quand_Shift_On) 
}
1 Like

@jean-emmanuel

Hello, Jean-Emmanuel

How to explain this (and have the whole thing working...):
yaquec4

As you can see :

  • Shift Off > buttons = kind of switch
  • Shift On > buttons = independant behaviour

Now, when i set my "switch" on button 4, everything is recalled well, "Shift On" and "Shift Off" button's values.

But when i set my "switch" on button 1 or 2 or 3, the "Shift Off" value is not recalled.

Each button has its own script : if(get('Shift')==0) set('c*',0,{sync:false});
I have two variables "out of the box" (how do you call this ? Global variables ?...) : Var_Mémo_Shift_On and Var_Mémo_Shift_Off
And the code i posted just below, in the Shift button's script field.

I'm getting closer to what i want... but each step is just a "Woohoo" one :slight_smile:

Thank you

I did a quick test and found the issue: when turning shift on, the state is recalled but the buttons' scripts are also triggered which ends up turning them off. Simple fix: change the buttons' scripts to if(get('Shift')==0 && value == 1) etc.

Bonus: shift.json (6.5 KB)

1 Like

@jean-emmanuel, thank you so much ! :+1:

Btw : in your script,

 locals.shiftOffState = stateGet(btns)

what does mean 'locals. ? what does it refer to ? And is 'shitOffState' the name you gave to a variable ? Could it have been locals.3_kilos_de_patates ?)

locals is another option I forgot to mention in my previous post, it's a bit like the globals object but scoped to the widget (each widget has a locals object).

Could it have been locals.3_kilos_de_patates

Yes

@jean-emmanuel OOOOk... then, what's the difference between using a variable "var" and "locals" ?

If i understand, var could refer to other objects/widgets (here the buttons)... while locals concerns only the widget we're scripting in... i imagine we could have used "var" too... so what's the benefit ? Shorter to write ? avoiding conflict ?

Trying to get it clear in my mind :wink:

A variable created with a javascript var statement will only exist during the execution of the script, it will be created each time the script is executed.

The locals object is persistent during the lifetime of the widget and can be used to share variables between properties.

Variables created using set setVar() are different than regular javascript var however, and can be seen as persistent locals (the difference being that other widgets can access them with getVar(), and that they can be used in the host widget's properties using the VAR[} syntax)

1 Like

@jean-emmanuel

Omg... aouch... not so far from giving up... i wanted to associate images with the buttons...
At first just according their visibility prop to the button's value...

It works fine when the buttons behave like normal buttons... but something's wrong in their "switch" mode : visibilities won't update their values, until i turn the shift button On then Off.

avec les miages

I tried a lot of things here too. Using stateGet / stateSet with those images too... setting button's values to 0 in the middle of the process... using other variables... Using long lines of #{@{...} ? ... : ... in the visibility props...

But here again, i'm missing something...

So...

Thank you