# Trying to make a multidimensional array which can connect to my buttons Please Help as soon as possible Please

**URL:** <https://discourse.processing.org/t/trying-to-make-a-multidimensional-array-which-can-connect-to-my-buttons-please-help-as-soon-as-possible-please/34047>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [December 11, 2021, 1:01am UTC](https://discourse.processing.org/t/trying-to-make-a-multidimensional-array-which-can-connect-to-my-buttons-please-help-as-soon-as-possible-please/34047 "2021-12-11T01:01:50Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 11, 2021, 8:34am UTC](https://discourse.processing.org/t/trying-to-make-a-multidimensional-array-which-can-connect-to-my-buttons-please-help-as-soon-as-possible-please/34047/2 "2021-12-11T08:34:08Z")

</div>

Let me see if I got it right…

You have an array w/ 6 buttons and a boolean array w/ 2 states.

Each button makes 1 of the states `true` and the other `false` when clicked.

So basically you need an extra array which stores the indices to be set to `true` when their corresponding button is clicked:  
`STATE_INDICES = Uint8Array.of(1, 0, 1, 1, 0, 1)`

The function **createButton()** has a 2nd parameter _value_ which we can use it to store the index we want to be set to `true` for that button:

> **[reference | p5.js](https://p5js.org/reference/#/p5/createButton)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

> createButton(label, [value])

In order to read a button’s stored _value_ we can invoke the method p5.Element::**value()**, given a button is of datatype [p5.Element](https://p5js.org/reference/#/p5.Element):

> **[reference | p5.js](https://p5js.org/reference/#/p5.Element/value)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

Inside the **mousePressed()**'s event callback we need to use the keyword `this` in order to access the clicked button.

The only gotcha is that p5.Element::**value()** returns a string, so we need to use the unary plus `+` operator to convert it to a number: `+this.value()`

> **[Unary plus (+) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus)**
>
> The unary plus (+) operator precedes its operand and evaluates to its
> operand but attempts to convert it into a number, if it isn't already.

After that we can use that _value_ as the index for the boolean array _buttonStates[]_:  
`buttonStates[+this.value()] = true;`

Here’s the full button event callback:

```auto
function changeButtonStates() {
  buttonStates.fill(false);
  buttonStates[+this.value()] = true;
  redraw();
}

```

And this is the complete example demo for it:

https://glitch.com/embed/#!/embed/state-buttons?path=sketch.js&previewSize=0&sidebarCollapsed=true

---

_[View the full topic](https://discourse.processing.org/t/trying-to-make-a-multidimensional-array-which-can-connect-to-my-buttons-please-help-as-soon-as-possible-please/34047)._
