Introduction to Variables
-
Description
A variable is like a box. It allows your program to “remember things”.
For example, you can store a number in a box:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0b138254-7a97-4519-ae37-32a87a122982.gif" width="400" style="border-radius: 5px; border: 1px solid #29622d;">
Later, you can check what’s in the box:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/39cdc829-7bda-4b1d-b5b2-7c690036e597.gif" width="400" style="border-radius: 5px; border: 1px solid #29622d;">
You can also read out the number, change it, then put the new number back:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c1d353d6-3ade-4b4a-aecf-ec8571c99cc9.gif" width="648" style="border-radius: 5px; border: 1px solid #29622d;">
Making a Variable
To make a new variable, go to the “Variables” group, and click “Make a Variable”. You need to give it a name, such as “number”, then click “OK”. Note that every variable needs to be named differently.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/27b969ed-c718-4582-b734-3157d3517693.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
Renaming a Variable
If you want to change the name of a variable, you can right-click on it, then select “Rename variable”.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f3a7a728-048b-46c9-8b6e-2e40f638cdc4.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
Deleting a Variable
To delete a variable, you can right-click on it, then select “Delete the [censored] variable”.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/eae21591-6486-4094-beae-485b559e8688.gif" width="518" style="border-radius: 5px; border: 1px solid #29622d;">
Show or Hide a Variable
If you check the box to the left of a variable, it will be displayed on the stage, so that you can easily check its value. If you uncheck that box, the variable will disappear.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1c6ff12f-b16b-4857-9800-8fe8afa98ebc.gif" width="854" style="border-radius: 5px; border: 1px solid #29622d;">
Setting a Variable’s Value
To set the value of a variable, you can use this block:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/08581138-31c5-4a84-a79f-6f658182a1f6.gif" width="882" style="border-radius: 5px; border: 1px solid #29622d;">
You need to select which variable to set, and then input the value. The value can be a number or some text.
Note that when you save the project, the value of a variable is saved as well. So the next time you open this project, the variable will contain the same value.
Reading a Variable’s Value
To read the value of a variable, you can use the reporter block:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/72e585b4-55fe-4e7d-b9c8-f85cbe1cbefb.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
Changing a Variable’s Value
If a variable contains a number, you can use the “change variable” block to increase or decrease its value.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6a7f4e0d-88e9-44c3-9c02-41801c591c47.gif" width="917" style="border-radius: 5px; border: 1px solid #29622d;">
If you do not know how to use negative numbers, you can use the “reduce variable” block instead:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/59e8e2f1-b760-46c1-b208-f31b1dcc934a.gif" width="917" style="border-radius: 5px; border: 1px solid #29622d;">
Show or Hide a Variable Using Blocks
Lastly, you can use these 2 blocks to hide or show a variable. For example, in your program, you might want to show a variable briefly then hide it.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a2a4b852-c6bd-4798-b871-cbd04cfddfc4.gif" width="917" style="border-radius: 5px; border: 1px solid #29622d;">
-
I CreatiCode pinned this topic on
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login