>>Dr. Amy McGovern: We've learned a lot about abstractions so far and in today's video we're
going to focus on another form of abstraction. That is variables. Variables are really important
to stepping your programs up to be much more powerful. So what is a variable? We will do
examples in just a second, but let's talk about it first. Consider a game, you can pick
any game that you want. How do you know what the score is? Well you can remember it, and
over time that might get complicated depending on how the game is scored and how many players
there are, or you could just try to write it down. In real life that's just what you
would do, you would have a piece of paper and you would write down the score for everybody.
Well, writing it down is one way of storing that variable which is what we are about to
do. As another example, let's consider a bucket. A bucket can be a form of a variable in a
physical analogy. So, consider you have a series of buckets, let's call one bucket X
and one bucket Y. So given that there's two buckets and we can put things into the buckets
and then we can refer to the buckets name to mean what is in the bucket. So for example
let's put 5 things into the bucket labeled X, if we were writing this down in our programming
language we would call this X=5. It looks a lot like your math, as well, and it's just
an assignment operator. And now we can do things to manipulate that bucket, to change
the values in the bucket. So, we could assign X to be X+1 and that would be like putting
one more thing into the bucket. Or we can use that bucket, that X bucket, to make new
variables. So, if I assign Y to be X+5, then what's in Y? So what would be in Y would be
whatever is in X + 5. And since we started out with just X being 5 and then we added
1 to it a minute ago then it would be 6 + 5 and so what would be in Y would be 11. If
you took algebra this should look familiar, there are variables in algebra, you had equations
that you would try to solve for an unknown. Something like X = 5 + Y and another equation
that involved Y. This is very similar; there are some differences, however. So for one
thing, we're not solving a set of simultaneous equations here. Instead, we're using those
variables to refer to an unknown something, what in this case we know it, and you're assigning
them in order. So you can say X = 5, Y = X + 5, for example. So let's talk about how
you do variables in snap. I'll make this more clear by doing a lot of examples. Okay so
we've started a new program in snap and we're going to make an example on how to do variables.
First, we're going to navigate to the variables tab. We've been sticking mostly over here
to the motion and a little bit of control. We're moving to variables, and we're going
to make a variable, you just have to click and make a variable. And you have a choice;
you can make the variable available to all of the sprites or to this sprite only. There
are other programming paradigms and they have other names for this, usually they'll call
this "global" or "local". They're both global variables for this for the variable name where
it says for all sprites or for this sprite only. It's just a matter of which sprites
can actually access the value of the variable. Since I only have one sprite for right now
I'm going to leave it just at all sprites. I'm going to make a variable. I'm going to
call it "count" because we're going to make an example of how many times we run into the
wall. So you'll notice that when it starts there are a couple of things that are different
here, for one thing the variable shows up in the window and it shows you it's initial
value and it always shows as 0 to start with. So let's make just a little quick script to
count how many times we run into the wall. So when we click our flag we are just going
to go forever run back and forth. And we could make this, if we didn't want to count, we
could just move ten steps and you're on the edge bounce. And that'll just simply go back
and forth. But what we really want to do is count how many times we've done that and we're
going to use variables to do that. First, we need to know whether or not we ran into
it. So instead of using this if on the edge bounce, well we can leave that there. We're
going to say if we are touching the edge, and we're going to change, and change lets
you set the value of a variable. And it has a little drop down menu that gives you all
the variables that you can touch at that moment so we can change count by 1. And now you can
see that every time it touches the wall it increments. So that was one quick example
of how to use variables. One other point I want to make about variables is that variables
have types and so in this case the variable was an integer that is just a count, a whole
number count, over here. Let's do another variable, another way that you can actually
tie sensing and variables together. So I'm going to make another variable, I am going
to call this one "name", and I am going to go over to the sensing tab and I'm going to
say, "What's your name?" that's the default little thing, you can ask anything. And then
I'm going to show you how you can get the answer from the user. So you can say set,
name, and then in the sensing tab there's a little circle here which we haven't used
before and you can drag that, it's called "answer" and so now you've set the name to
the answer. And I can just click on this and it'll say, "What's your name?" and I'll say,
"Dr. McGovern" and you see it sets the variable to Dr. McGovern. And then you can do things
with it after that if you want, so for example we could have the little sprite say back to
us something like, "Hello" but we want it to actually use the variable we just did and
so we need to go to the operators tab which we are going to talk about in a different
video but very very briefly we're just going to want it to say 2 words. So I'm going to
have it say, "Hello" and then the name that we just asked for. So I'm going to go to the
variables tab and to show you that you can drag this circle into places like that so
it'll say, "Hello" and name. So if I click on that and I say, "my name is Dr. McGovern"
again, it'll say, "Hello Dr. McGovern". So let's do one other fun example. Let's make
an example where you have 2 sprites and we have the variable available and only 1 sprite
and we'll also do an example of what a script variable is. So I'm going to make a second
sprite... there it is, it just creates it in a new location and with a new random color.
We're going to make this sprite run around the world randomly and then it's going to
count how many times it hits the other sprite instead of counting how many times it's going
to hit the wall. And then we're going to let the other sprite just run back and forth between
the walls. So in this case I'm going to make a variable but I'm going to make it for this
sprite only. I'm going to call it "Blue Hits" since it's a little blue sprite. And you'll
see that over here in the window it says, "sprite 2 Blue Hits" to show you that that
variables only available to the sprite 2. We're going to have it, see the other one
moves when we hit the flag so we're going to have this one move when it hits the flag.
And instead of looking for the wall, we're going to look and say, "Is it hitting the
other sprite?" If it is... touching sprite then it will change its count, change blue
hits. You might think that's enough but we haven't told this thing to actually move and
what we really want it to do is we want it to move sort of randomly around the world
and to bounce off the edges and to not be totally crazy, so I want it to move in a reasonable
direction. I'm going to add a few more bits of control. Well first let's add some moving.
We're going to pick...something fun. We're going to point in a random direction and we
haven't talked about random before, so I'm going to pick a direction and I'm going to
just pick a random number, that means any number and the computer chooses it between
1 and...1 and 180. I'm going to have the sprite move and then it counts if its touching the other sprite but the other
thing we needed to do is look and see if its running off the wall. And if its running into
the wall, so if its touching the edge, instead of bouncing or counting anything else we're
actually just going to give it a new random heading. So grab another random number. Okay,
let's see what this does. There's a bit of a problem, huh? We forgot a few things. For
one, let's start in the middle. For another we looked at our directions and if you go
over here and you look at your directions, 180 means pointing down, 0 means up so we're
always pointing to the right. That isn't necessarily what we want. Let's actually try something
slightly different. Let's first just say if we tough the edge we're just going to turn
around. Our sprite still went off the screen. I wonder what's wrong? Let's look over here.
We have an odd problem, our little 'if' statement is actually inside the other 'if' and that's
what's wrong because it was only checking if it was touching the edge if it was touching
the other sprite which never happened of course. So let's start that over again. There we go.
Now let's add something that's a little bit more interesting into that and have the blue
sprite actually be a little bit more random so after it turns around we're going to let
it, for example, just turn a random amount if it touches the other sprite. Let's call
it, let's do that just when it touches the other sprite, and we're only going to turn
it just a little bit. So some random number between 1 and let's call it 20. Let's try
that. Of course it actually has to... there we go! See when it touches it, it's going
off in random directions. Okay. Okay, so let's do one more thing to this example and that
is just show how we can use script variables, which are variables that are visible only
inside the script. So on the little blue sprite, which is the one we're still in, I'm going
to make a variable and instead of picking a random direction I'm actually going to make
a variable called 'direction'. And you'll notice that it does not show up over here
because it's only visible inside this script and only at this location, anything below
this. It's not going to be visible above it. So to demonstrate that, if I just try to move
this up here, and I try to run it, it gives you an error. See it said a variable direction
does not exist in this context. So let's get rid of it. And let's just show how we're going
to use direction. So we're going to set direction and it still shows up in the list of things
to set and we're going to set the direction to that picking the random number and then
instead of pointing in direction 90, we're going to point to the direction of the variable
direction. And this should to the exact same thing it was doing. There we go. That's it
for variables for right now. We will have another video on operators, which combined
with variables will really make variables much more powerful. Go experiment with variables
and have some fun.