Scripting: sum (store) in a variable the values of all widgets with the id "button_*"

I'm wondering if it's possible to define a variable based on the value of multiple widgets.

// (I'm just taking a wild guess)
var sumV = sum("button_*")

Expected result:

  1. Sum the value of every "button_*" (id) widget (create a sum from all these values).
  2. Store the sum (button_1 + button_2 + button_3 + ... + x) in the "sumV" variable.

I'll try this later, but for now could you try something like

Var sum = @{Button1.value} + @{button2.value}} //etc

Oh wait, you mean like it loops through all widgets based on a name...hrmm...have to think...

A for loop and some regexp? I'm not experienced enough to help any more. Would have to experiment myself...

1 Like

Don't worry. I appreciate any response.

var sum = 0
for (var i = 0; i < 20; i++) {
  sum += get("button_" + i) || 0 // || 0: fallback in case value is undefined
}
1 Like

Hurray!

It's amazing what you can do with O-S-C!

GIF

Would you mind sharing the code and where you placed it exactly?

Not yet. But I'm willing to give support if you've tried anything.

Ok cool.

Also, you know about traversing? Seems like it could benefit you...

@theodor_the_1st So I just haven't been able to get my head around this. Would you put up a minimal session with just 3 buttons or so and show how you would sum their values?

Take a look at Jean's post (#3). The code works for 20 buttons (from button_1 to button_20).

Yeah, I'm just not sure where I'm supposed to be plugging in that code.

And you're getting the button's value. You mean the value 0 or 1?

Yes. It's the coolest feature of O-S-C.

The code needs to go into the inspector / scripting tab of the widget. More precisely, it needs to go in the scripting property of the widget where you need to use the variable.

If the toggle button with the id "button_1" is turned on (is on value 1), then the sum's value will be 1. If neither of the prefixed with "button_" buttons is on, then the sum's value will be 0.

@theodor_the_1st Thanks for taking the time to explain this. I'm just unsure how to get the variable out of the script property and use it elsewhere. For example, for testing, how would I display the value of sum in the textbox? Or...How can I display the sum value in the textbox? This is what has stumped me.

The script property is only executed when the widget's value changes, that's why this doesn't work. You could put that script in a script widget with an extra line: set("text_id", sum) and configure the buttons so that they trigger the script when they are pressed:

  • set the buttons' linkId to >> sumLink
  • set the script's linkId to << sumLink

See:

1 Like

@jean-emmanuel You're very kind to explain this. I appreciate it.

For testing you can also use:

console.log(sum)

@theodor_the_1st Is it at all possible to upload a minimal session, a few buttons showing how you got it working? Would be really helpful. I'm understanding Jean's code with a text widget and linkid now, that was really helpful. But how exactly you're getting that functionality with your buttons, I'm just lost.

What did you try and didn't work?