A variable is a bucket that you wrote a name on. Imagine you have three buckets: A, B, and C.
You can put all sorts of things in variables, just like you can put all sorts of things in buckets. Imagine that you’ve got a sandwich in A, a hammer in B, and a bottle of soda in C.
If you tell your friend to give you A, they’ll give you a sandwich.
If you tell your friend to give you A + C, they’ll give you a sandwich and a soda.
If you ask for A + B, you’ll get a sandwich and a hammer.
Let’s try this in Minecraft.
/js var a = "sandwich"; and hit Enter.
What happened? Nothing, right? Well, nothing you could see. You just told the computer to make a bucket, name it
a, and put the word “sandwich” in it. You didn’t tell it to say anything.
Let’s tell it to say what’s in
/js echo(a); and hit Enter.
It said “sandwich”.
We could talk about a bunch of rules for variables, but let’s just go with the big three:
- Variable names should be letters or even words, but no spaces or punctuation.
- You should use “var ” in front of a variable the first time you use it in a program. It tells the computer to create a new bucket with that name.
- Don’t put quotes around variable names or numbers, only around words or sentences you want to put inside variables.
2 + 2 = 4
"2" + "2" = "22"
When you put quotes around it, the computer thinks it’s a word, and
You might get this wrong a few times before you remember it. Don’t worry and don’t get mad at yourself. Even programmers who have been programming for 20 years make mistakes. When you figure out what your mistake was and fix it, that’s called “debugging,” and it’s an important skill in programming. So if you make a mistake, that’s just a chance to learn debugging.
Functions and Arguments
When you told the computer to
echo(a), you were asking it to perform the
echo there was
(a). That was called an “argument.” An argument is simply a value you send to a function, when you’re asking it to run, and you put it inside parentheses after the function’s name. It can either be a variable, or the value itself.
echo("sandwich") would have the same result as
echo(a) in the variable example above.
One of my favorite functions in ScriptCraft is the
box(). That requires four arguments, separated with commas. Try typing:
Every block in Minecraft has a number associated with it. This function makes a solid square or rectangle out of the material identified by the first number. The next three numbers are the width, height, and length. So
/js box(5,2,3,2); tells Minecraft to make a rectangle out of blocks of oak planks (or block type 5) that is 2 blocks wide, 2 blocks deep, and 3 blocks high.
How good is your multiplication? What if, instead of being oak planks, that was TNT? What if it was 5 x 5 x 5 blocks in size? With one function, you can create a brick of TNT with 125 TNT blocks in it. Pretty cool.
We’ll learn more about functions and arguments when we explore the drone functions in the next blog post.