Score! Using variables

Knowledgebase / Score! Using variables

We’re going to make the score get displayed in the game, and collecting apples will increase the score. This tutorial follows on from Get over here! Setting an object’s position.

Step 1: Create a new variable for the score

Edit Newton’s script. We’re going to create a new variable for keeping track of the score. A variable is a piece of data that the program stores in the computer’s memory.

Open the Variables tab, and from under the Properties section, drag out the “Set true/false i to” block. This block is the default block for setting variables. Drag this block into the “When level starts” block.

ezgif-2127514169

Click on true/false and change the data type to number. Then click on i and select New variable… Name this variable score. 

ezgif-2281610513

Then open the Operators tab and drag the 0 block into the empty space in the variable block. Now the score will be saved as a number variable with the number set to 0.

ezgif-1849350126

Step 2: Update the score

Now we’ll make the score increase by 1 when Newton collects an apple. Open the Variables tab and grab the “Set number score” block. Drag this block into the “When I am touched, get toucher” block.

Screen Shot 2016-05-26 at 10.59.28 am

We’ll use the addition block to add 1 to the score. Grab the ( ) + ( ) block from Operators, and drag it into the empty part of the “set number score” block. Then grab the “number score” block from Variables and drag it into the left side of the addition block. Lastly, grab a 0 block from Operators and put it in the right side of the addition block. Change the 0 to 1.

Screen Shot 2016-05-26 at 11.02.21 am

We’ve got the score variable updating, but if you play the game you won’t notice any visible difference. We need to update the text field to display the score.

Step 3: Create a new property variable for the text field

The text field we’ve been using so far is stored as a local variable, so to update this variable in a separate event block we need to save it as a property variable (Learn more about the difference between local variables, property variables, and global variables). Open the Variables tab, and under Properties, grab the “set true/false i” block and drag it “When level starts” block. Change the variable type to instance, and change the name to score text.

Screen Shot 2016-05-26 at 11.05.17 am

Now open the Variable tab once more, and from under Local, grab the “instance textfield” block. Drag this block inside the empty gap in the “Set instance score text” block.

ezgif-2410870223

Now that the text field is stored as a property variable, it can be updated inside other event blocks. We’ll be updating it inside the “When I am touched, get toucher” event block.

Step 4: Change the text field to display the score

Open the Draw tab, and grab the “Set text of instance textfield” block. Drag it into the “When I am touched, get toucher” block, and drag out the “instance textfield” block inside it (you can drag this block into the trash).

ezgif-3638518127

Then open the Variables tab and drag the “instance score text” block (from Properties) into the “set text of” block.

ezgif-2002212134

Lastly, open the Variables tab again, and try to drag the “number score” block into where the empty text block is (the block with quote marks). But it doesn’t go in! That’s because this block will only accept text, not numbers. The block to convert numbers into text is found under Operators, and is named “Create text with”. Drag this block into the empty gap in “set text of instance score text”, and then drag the “number score” block into this block.

ezgif-2351246992

Now when the apples are touched, the text field will update to display the score variable. Click Play to test it out.

Great work! Don’t forget to save your game.