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...
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 !
é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)
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
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)
}
How to explain this (and have the whole thing working...):
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
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.
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).
@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 ?
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)
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.
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...