Multiple choice: Questions and decisions with if-statements

Knowledgebase / Multiple choice: Questions and decisions with if-statements

We’re going to add a multiple choice question and answer section to your game. If the player chooses the correct answer, they are free to continue onwards.


Step 1: Make the questioner immovable when it’s created

Find a character (or game object) that will block the path and ask questions. Right-click on it and select Add Script.


The character has to block the path, so grab the “When created” block from Events, and then put the “Set immovable true” block (from Physics) inside the event block.

Screen Shot 2016-06-10 at 12.15.40 pm

Play your game to make sure the character has become immovable. Now Newton can’t get past!

Step 2: Displaying text pop-ups when the object is clicked

Instead of using a textfield, we’re going to display a message in a text pop-up. We’ll trigger this to appear when our character is clicked.

Grab the “When the player presses myself” block from Events, and then grab the “Print” block from Control Flow and drag it into the first event block. Then from Operators, find the empty text block and drag it into the “print” block. Type a message into this block, something like: “Answer my question correctly and I will let you pass.”

Screen Shot 2016-06-10 at 12.19.00 pm

Let’s also make a message appear at the start of the game to let the player know they need to click on the character to talk to him. Grab another “print” block and another empty text block, snap them together, then drag them into the “When created” block. Type in a message like “Click on the character to talk to him.”

Screen Shot 2016-06-10 at 12.20.28 pm

Test out your game to see the text pop-up.

Screen Shot 2016-06-10 at 12.31.59 pm

Note: DON’T click the tickbox that says “Prevent this page from creating additional dialogs”. Ticking this will stop any more text pop-ups from displaying, and you’ll have to close and re-open your web browser to get them back!

Screen Shot 2016-06-10 at 12.21.55 pm

Step 3: Asking a question and prompting for an answer with a text pop-up

Now we’ll use a block that can ask a question, and provide multiple choices for answers that the user can select from. Grab the “prompt for number with message” block from Operators, and drag it out into the workspace. Don’t connect it to any blocks yet, because we’ll need to place it in a new block.

Screen Shot 2016-06-10 at 12.28.37 pm

The end of this block has an empty text field block. Type your question in this block, and provide a number of answers. Type it out similarly to this: “What is the answer to this question?  1. Option one  2. Option two  3. Option three”. Replace the question and answers with the question you’d like to ask the player, and the answers you’d like them to select from. In this example, I’m asking the player: “Magenta is a color made from blue and…?  1. Green  2. Orange  3. Red”.

Screen Shot 2016-06-10 at 12.29.27 pm

This block doesn’t actually create an action in the game, all it does is returns a value that we can then use. In this case, the value that is returned is the number that the user types in. We need to place this block inside an action block that can use the returned number. We’ll use a variable block so that we can store the number the user has typed in, and then check to see what number it was.

Step 4: Creating a number variable for the answer

We’ll create a number variable for storing the user’s answer to our multiple choice question. Grab the “set true/false i” block from the Properties section inside the Variables tab, and drag it out under the “print” block inside the “When the player presses myself” block. Change the variable type to number by clicking on “true/false” and selecting “number”. Rename this variable by clicking on “i” and selection “New variable”. Give it the name “answer”.

Screen Shot 2016-06-10 at 12.30.35 pm

Now we can grab the “prompt for number with message” block that we left floating around, and snap it inside this variable block.

Screen Shot 2016-06-10 at 12.33.01 pm

Play your game to test this out. When you click on the object, the set variable block runs, which then prompts for a number to be typed in, and the variable’s value will be set to the number you type in. But then nothing else happens. The next thing for us to do is to check what number they type in, and then make the user have  happen if they type in the correct number.

Step 5: Checking their answer with an if-statement

Grab the “if do” block from the Control Flow tab. Drag it under the “Set number answer” block.

Screen Shot 2016-06-10 at 12.34.29 pm

To check their answer, we’ll see if the number variable = the correct number for the answer from step 3. In this example, I’m asking the player: “Magenta is a color made from blue and…?  1. Green  2. Orange  3. Red”. Since 3 (red) is the correct answer, I’ll check if the answer number = 3.

Grab the “_ = _” block from Operators, and drag it into the “if do” block. Then from the Variables tab, grab the “number answer” block and drag it into the left side of the “_=_” block. After that, grab a “0” block from Operators and drag it into the right side of the equals block. Click on the 0 to change it to the correct number.

Screen Shot 2016-06-10 at 12.35.48 pm

Now we want the questioner to allow the player to get past if they answer correctly. We can simply disable the physics on this object to allow other objects to move past it freely. Grab the “Set physics enabled true” block from Physics, drag it inside the “if do” block, and click on true to change it to false.

Screen Shot 2016-06-10 at 12.37.25 pm

Once again, test your game by playing it. Now you should be able to walk past the character once you type in the correct number. But what if you type the wrong number?

Step 6: Modifying the if-statement to if-else

We can modify the “if” block to become an “if else” block. This will allow us to make something else happen if we don’t get the right number – in this case we’ll display text telling the player to try again.

Click on the cog icon in the top-right corner of the “if do” block, and drag the else block inside the if block. Then click the cog again to close this box.

Screen Shot 2016-06-10 at 12.38.33 pm

Now grab another “print” block from Control Flow, and another empty text block from Operators. Connect them together, and drag them into the else part of the “if else” block. Type in the message to display to the player when they choose the wrong answer. I’m going to make my questioning character seem a bit mean, so I’ll have them say “Hah! Wrong, sir! Come back when you know something.”

Screen Shot 2016-06-10 at 12.39.35 pm

Play your game to see the final version of your multiple choice script in action. Can you display text with textfields instead of using the print block to create pop-ups? Can you add more questions?

Try making a whole game out of multiple choice question and answers. Did you know that the very popular Adventure game genre uses multiple choice questions to determine what will happen next in the game?