Introduction to Programming

Variables in PictoBlox

Lesson 4: Variables

What is a Variable?

Imagine you are keeping score in a game. Every time a player scores a point, the score increases. You need somewhere to store that score — and that's exactly what a variable does!

A variable is a container that stores a value. That value can change as the program runs — hence the name 'variable'.

In PictoBlox, variables are used to store numbers, text, or other data that your program needs to remember and use while it is running.

Creating a Variable

Follow the steps below to create a variable in PictoBlox:

  • Click on the Variables palette in the Code tab.
  • Click the Make a Variable button.
  • Type a name for your variable in the dialog box (e.g. Score).
  • Choose whether the variable is for all sprites or just the current sprite.
  • Click OK — your variable is now ready to use!
PictoBlox dialog box for creating a new variable with a text field for the variable name and options for all sprites or this sprite only
Creating a new variable in PictoBlox

Variable Blocks

Once a variable is created, a set of blocks becomes available in the Variables palette to work with it:

Variable Blocks in PictoBlox

BlockWhat it Does
Set (variable) to ()Sets the variable to a specific value
Change (variable) by ()Increases or decreases the variable's value by a given amount
Show variable (variable)Displays the variable's value on the Stage
Hide variable (variable)Hides the variable from the Stage
Variables palette in PictoBlox showing blocks like set Score to 0, change Score by 1, show variable Score, and hide variable Score
Variable blocks available in the Variables palette

For All Sprites vs This Sprite Only

When creating a variable, you are asked to choose its scope — meaning which sprites can access and use it.

Variable Scope

ScopeWho Can Access ItWhen to Use It
For all spritesEvery sprite in the projectWhen multiple sprites need to share the same value, e.g. a game score
For this sprite onlyOnly the sprite it was created forWhen the value is specific to one sprite, e.g. a sprite's own health

Example: Score Counter

Let's look at a simple example. We will create a Score variable that starts at 0 and increases by 1 every time the sprite is clicked.

  • Create a variable called Score.
  • Add a 'When flag clicked' block and connect 'Set Score to 0' to reset the score at the start.
  • Add a 'When this sprite clicked' block and connect 'Change Score by 1' so the score increases each time the sprite is clicked.
  • Use 'Show variable Score' to display the score on the Stage.
  • Click the green flag and then click the sprite to see the score go up!
Two block coding scripts in PictoBlox — one with when flag clicked and set Score to 0, another with when this sprite clicked and change Score by 1
Script for a simple score counter using a variable
Variables make your programs dynamic and interactive. Without them, your program would always behave the same way every time it runs!

Subscribe to our newsletter.

Get updates to news and events.