Variables

In my previous blog, we covered how you can customize your script to take user input and make your sprite jump to the height specified by user.

Now let’s extend the previous scenario to asking user input for both height and length of the jump. How would you enhance your existing script so that the jump’s height and width is defined by the user?

Why answer block will not fit in this case?

Yes, we cannot use answer block in this case since answer block can store only one value at a time. For example, first we ask user the height of the jump and store it in answer block. Next when we ask user for length of the jump, the previous value stored in answer block (in this case, the height of the jump), will be overridden with the new one (which is the length of the jump).

So we need a mechanism to store two values so that they can be used individually at the same time.

What is a variable?

Variable, in computer science, is a place to store a value for retrieval and use later. Take an example of a kitchen container, which you label as “Dog Food” and store different kinds of dog food you have. In this case, “Dog Food” is the name of the variable and the contents of the container represent the value of it. In the same manner, in computer science, variables is a way to tag or name a location in memory and the contents stored in that memory location are called it’s value. Just like in “Dog Food” container, the contents of the containers can change based on the inventory of the dog food you have, the contents of the variables can also change. Variables are often used in computer programming to store a value and then manipulate based on the program logic.

Different programming languages have different ways of creating a variable and storing information in it. In this blog, we will cover how it is done in SNAP!

In the above scenario of simulating a sprite jump based on user input height and length of the jump, we will use 2 variables in this program: one for the height (let’s also call it “height”) and other for the length of the jump (called as “length”).

How to create Variables in SNAP!?

Creating variables in SNAP! is very easy. Select the Variables palette in the left pane and click on Make a variable button.

Make a Variable

A window with title Variable name will open up. Give the variable a name and click OK once done. Let’s firstly create a variable with the name “height”, which we will use to store the height of the sprite jump. Type in “height” and click OK.

Define Variable Name

Follow the same steps to create another variable and name it “length”, to store the length of the sprite jump.

Once done, you will see both the variables created in the left pane, like this:

Use New Variable in SNAP!

Modify the current Script to ask for user input and store the values in variables

Now that we have created both the variables, lets modify our current script to use them.

After the scripts asks user for the jump height using wait block, and the user has input the value, which is stored in answer block, our first task is to store that value in height variable we have created using set block. We will repeat the same process for asking user for jump length and storing that value in length block, and finally using both the variables to define the sprite jump behavior.

This is what our final script will look like:
Using Variables, Sprite Jump to user input height and length

If you have any questions, or want me to cover any specific topics then please let me know in the comments below.

Share This:

A geek, a techie, a daughter, a sister and a wife. Worked at Microsoft for 8 years and currently working at Amazon Web Services (AWS). All opinions expressed here are my own and has no correlation to my employer or ex-employers!

One thought on “Variables

Leave a Reply

Your email address will not be published. Required fields are marked *