# Input not disabling

**URL:** <https://forum.makecode.com/t/input-not-disabling/28164>\
**Category:** micro:bit\
**Created:** [April 9, 2024, 6:17pm UTC](https://forum.makecode.com/t/input-not-disabling/28164 "2024-04-09T18:17:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuvalyly](https://avatars.discourse-cdn.com/v4/letter/y/8edcca/32.png) [@Yuvalyly](https://forum.makecode.com/u/Yuvalyly)\
**Post date:** [April 9, 2024, 6:17pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/1 "2024-04-09T18:17:41Z")

</div>

I’m trying to make Simon Says game for the micro:bit, but I have a problem with inputs getting recorded if pressed while the game is running some other code (like displaying the sequence or the game over message).

I tried to disable inputs by using a variable to flag when other code is running. The code for each input first check if that flag is false (0. I didn’t find a way to make a Boolean variable with the blocks), and if so, change it to true and run the main function. This variable is then reset at the end of the main function.

It works for most inputs, but for some reason, the input that was used last to call the main function and set the variable can still be triggered again, which the game react to later as if the variable didn’t existed.

I even tried to make a debug function that records the inputs and call it at the end of the main function before the variable is reset, and it turns out that after each time the input register and the main function is called, it is only “aware” of that input and the next trigger is not taken into account yet, and the variable is indeed set to true.

I also tried to pause the main function before resetting the variable to let the scheduler run the even handlers for the inputs (and hopefully realize that the variable is still true so the if statement should prevent them from calling the main function) but it still didn’t worked.

How can I fix this? Here is the code in JavaScript (I used the blocks to code but the generated JavaScript is easier to share.

“working” is the variable that keeps track if other code is running (or is currently “working”)

Play() is a function to play a sound and display an arrow pointing at an input (because I can’t make the buttons glow like in the real Simon Says).

The main (Check) function:

```auto
function Check (num: number) {
    if (sequence[index] == num) {
        Play(num)
        index += 1
        if (index >= length) {
            sequence.push(randint(0, 3))
            length += 1
            basic.pause(500)
            for (let index = 0; index <= length - 1; index++) {
                Play(sequence[index])
                basic.pause(100)
            }
            index = 0
        }
    } else {
        music.play(music.stringPlayable("C5 A B G A F G E ", 120), music.PlaybackMode.LoopingInBackground)
        basic.showString("Game Over")
        music.stopAllSounds()
        basic.showString("Score:")
        basic.showString("" + (length - 1))
        sequence = [randint(0, 3)]
        index = 0
        length = 1
        Play(sequence[0])
    }
    basic.pause(100)
    Working = 0

```

Example input:

```auto
input.onButtonPressed(Button.B, function () {
    if (Working == 0) {
        Working = 1
        Inputs.push("B")
        Check(1)
    }

```

---

<div class="post-metadata">

**Author:** ![KIKIvsIT](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kikivsit/32/10492_2.png) [@KIKIvsIT](https://forum.makecode.com/u/KIKIvsIT)\
**Post date:** [April 9, 2024, 6:18pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/2 "2024-04-09T18:18:26Z")

</div>

Hmm, @richard, would your new state extension also work with micro:bit?

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [April 9, 2024, 6:28pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/3 "2024-04-09T18:28:37Z")

</div>

no, i’m afraid it is arcade specific at the moment

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [April 9, 2024, 6:30pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/4 "2024-04-09T18:30:40Z")

</div>

@Yuvalyly can you share a link to your project?

---

<div class="post-metadata">

**Author:** ![Yuvalyly](https://avatars.discourse-cdn.com/v4/letter/y/8edcca/32.png) [@Yuvalyly](https://forum.makecode.com/u/Yuvalyly)\
**Post date:** [April 9, 2024, 7:22pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/5 "2024-04-09T19:22:44Z")

</div>

Here is the project link: [https://makecode.microbit.org/S62918-32140-58102-60932](https://makecode.microbit.org/S62918-32140-58102-60932)

BTW ignore the “Inputs” array. It is used only for testing.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [April 9, 2024, 7:36pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/6 "2024-04-09T19:36:21Z")

</div>

@Yuvalyly ah, I see what’s going on here. When you pause inside of a button handler, we queue up any other button presses that happen while the handler is still executing.

This is sort of annoying to work around in micro:bit because there isn’t a nestable “run in background” block. Here’s an extension you can add to your project to get one:

> **[run-in-background](https://makecode.microbit.org/89883-93833-97261-60511)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.

The fix here is to take the portion of your button handler that pauses and wrap it in a “run in background” block like so:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/064dc0c3a1980a9d9296b2d266d23e353c730930.png)

This will prevent the button handlers from queueing up

---

<div class="post-metadata">

**Author:** ![Yuvalyly](https://avatars.discourse-cdn.com/v4/letter/y/8edcca/32.png) [@Yuvalyly](https://forum.makecode.com/u/Yuvalyly)\
**Post date:** [April 9, 2024, 10:24pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/7 "2024-04-09T22:24:18Z")

</div>

Thanks for the help. I’ll need it to not be midnight to be able to focus on it enough to understand it though. I’ll check it tomorrow.

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [April 9, 2024, 10:24pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/8 "2024-04-09T22:24:41Z")

</div>

Nice game, interesting case and welcome, @Yuvalyly!

@richard:  
Thanks for answering!

I’ve been playing around with background/parallel tasks in both MakeCode for micro:bit and Mindstorms, and have found the former to be more difficult to grasp.

It just occurred to me that the nestability of these blocks might be exactly why, because the background task in micro:bit is not nestable;

> **[Run In Background](https://makecode.microbit.org/reference/control/in-background)**
>
> Run part of a program while the rest of it is doing something else.

while in Mindstorms it is;

> **[run In Parallel](https://makecode.mindstorms.com/reference/control/run-in-parallel)**
>
> Run some other code at the same time that your main program code runs.

Aren’t there really equally numerous use cases and needs for support for both nestable and un-nestable versions of this block, by default and without the need for an extension, as this example case shows?

And for the micro:bit maybe, in fact, a nestable block could be more important to have instead of the un-nestable, since you apparently can achieve the same result as the un-nestable background block by using a (un-nestable) forever loop, like the documentation link above shows?

Here’s a grey block version similar to your solution, which I probably would never have thought was possible, just due to the background block not being nestable…:

> **[Simon Says grey](https://makecode.microbit.org/39542-62459-79540-71030)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [April 9, 2024, 10:31pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/9 "2024-04-09T22:31:22Z")

</div>

@Vegz78 yeah, the reason it’s not nestable in micro:bit is purely historical. We realized our mistake after shipping it for micro:bit and so it’s nestable in all of our other editors.

The fix never propagated backwards because we try very hard not to update blocks once they have been shipped; a lot of teachers take screenshots of the editor when making teaching materials and we don’t like to break them.

That being said, in a perfect world I would do away with the un-nestable version entirely since the nestable version can be used to replicate it perfectly.

---

<div class="post-metadata">

**Author:** ![Vegz78](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/vegz78/32/1201_2.png) [@Vegz78](https://forum.makecode.com/u/Vegz78)\
**Post date:** [April 10, 2024, 4:05pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/10 "2024-04-10T16:05:52Z")

</div>

Any way we(also as teachers with said screenshots in teaching material) can help?

The utility argument for a nestable background block seems pretty obvious.

If not possible to aim for perfection, maybe an addendum, which wouldn’t be that a breaking\* change, in a command submenu that is not the most frequently used(or screenshot), anyways?

\*Even though a vocal and fragile group, I am sure we teachers would survive such a change without having to lie down in the fetus position, so you do not have make us the main consideration against doing the right thing… 😉

---

<div class="post-metadata">

**Author:** ![Yuvalyly](https://avatars.discourse-cdn.com/v4/letter/y/8edcca/32.png) [@Yuvalyly](https://forum.makecode.com/u/Yuvalyly)\
**Post date:** [April 10, 2024, 5:26pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/11 "2024-04-10T17:26:59Z")

</div>

I can’t figure out how to import the run-in-background extension. When I click the link you pasted here, it just lead to an empty project.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [April 10, 2024, 5:27pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/12 "2024-04-10T17:27:39Z")

</div>

@Yuvalyly share links can be imported the same way as extensions! Just paste the link into the search box you get when you open the extensions dialog

---

<div class="post-metadata">

**Author:** ![Yuvalyly](https://avatars.discourse-cdn.com/v4/letter/y/8edcca/32.png) [@Yuvalyly](https://forum.makecode.com/u/Yuvalyly)\
**Post date:** [April 10, 2024, 7:46pm UTC](https://forum.makecode.com/t/input-not-disabling/28164/13 "2024-04-10T19:46:55Z")

</div>

It’s working! Thanks!
