Requiring Selection in Sequence for Blockly

I want to create a puzzle where the 3 objects need to be clicked on in sequence in order to make an action happen. I have been using blockly. I tried a few different things that should logically work, but they don’t. Any help or ideas?

The game runs as long as you haven’t selected the boxes in the right order. Here is the code:

2 Likes

@Benny What is the variable “box” set to in this code?

I think @JeffHennigar has one made as well for a simple machines breakout. The students had to click the 3 items in the correct order to reveal the clue.

He does, it is where I wanted the idea, but I am having the same problem here. This is a little simpler, but I am short of what the variable “box” is set. I have 3 different pumpkins that I need to set to that “box variable” at once.variable box

Here is what I have. pumpkin trap door

It is part of the “for each” loop. What this loop does is the following ->

First it needs a list to iterate over. Then the code inside the “for each” loop will be executed for each item in the list. So if the list holds 3 elements, the code inside “for each” runs 3 times. If the list holds 1000 elements, the code runs 1000 times.

The reason why a “for each” loop is useful is because you have access to each element of the list while the loop is iterating over the whole list. This current element is given you by the “for each” loop as a variable and it has a name. The default name is item and you can find it in the “Variables” category (after you created the “for each” block).

blockly

So the code snippet I provided would work just fine using item instead of box. But I decided to use a different variable name (box) because it is more descriptive. Imagine you have multiple lists and multiple “for each” loops in your code with every “for each” loop using item as the name for the current element. It can be quite confusing.

So it’s good practice to rename this variable. If you have a list of animals then call it animal, if you have a list of buildings call it building and so on. In my case I used box because I have a list of boxes. Then I use this variable name inside the “for each” loop to access the current box and to do something with it.

It’s not so easy to explain and I hope this makes sense. Please ask if something isn’t clear.

1 Like

It makes perfect sense. The renamed “Item” variable threw me for a loop.

If I do this process twice in one scene will it still work without resetting the variable for “Item”?

Sorry I really want this Holloween escape room out today.

Yes, you can have multiple “for each” loops using item as the variable name because they live in their own scope. If you do something like this it will work just fine.

blockly

Yes, Benjamin made it for me! works great!

2 Likes