Expected Time: 60 - 90 minutes
Students will be able to apply these concepts:
Use loops to repeat chunks of code that occur multiple times
Check if two values are equal using the “equals” operator
Create, keep track of, and manipulate script variables
Create, keep track of, and manipulate global variables
Understand the difference between local (script) and global variables and know which one to use
Delay code so that it doesn’t run immediately
Launch fireworks and cast lightning in Minecraft
Change the color of sheep
Generate a random number
Use random numbers to make events happen randomly
Loops: You can use a loop to repeat a chunk of code multiple times. If a loop is repeating five times, we say it has five iterations. A common misconception is that each loop will necessarily be doing the same thing. If you’re using variables, each iteration of a loop may do something different that would be impossible to write out because it’s deciding what to do dynamically as the program runs.
Variable: Variables store a single piece of information, such as a word, an entity, or a number. You can set the value in a variable to anything and, if it’s a number, there are certain pre-defined things you can do to this variable, like increase it by a certain number using the “change” block.
Local Variable: Local variables are created using the “script variables” block. These variables can only be accessed from within the same script where they were created.
Global Variable: Global variables are created by clicking on the “Blocks” tab and going to the “Variables” section, where you can create a new variable. This variable can be accessed or changed anywhere.
Integer: Any number that can be written without a fractional or decimal component is an integer.
Numbers that are integers: 56, -3, 0, and 8,423
Numbers that are not integers: 3.5, 9/2, and √3
Students should be comfortable with Tynker’s Mod Designer by now. They should also feel confident with the mod editing process of creating a mod in Tynker, deploying it to their server, going into Minecraft to check their mod, then going back to Tynker to tweak, debug, or expand their code. In this lesson, they will get a crash course in variables and using loops to repeat chunks of code. Variables are a crucial concept, so it’s worth making sure that all your students are on the same page and really understand the difference between local and global variables.
Students will learn about the “cast lightning” block, which allows them to summon lightning to any location. They can think of it as similar to the “spawn entity” block, except that entities stick around, but the lightning is only momentary.
The most important concept of this lesson is repetition. Students can repeat chunks of code that they want to happen multiple times. This is an absolutely central programming concept.
For the “spawn pig” example, point out that they wouldn’t need the “repeat” block to spawn three pigs. What else could they do to spawn three pigs at the same location? Ask students why one strategy (using a “repeat” block or just copying the code) might be better or easier. What if they want to make 100 pigs?
2. Lightning Arrows (DIY project)
Make sure students understand how to use loops in conjunction with conditionals. For some, this will be intuitive, but others will try to put the “repeat” loop after the “if” block instead of inside it. In this case, their code won’t work. Discuss with them what would happen if the “repeat” loop comes outside of the “if” block.
Remind students to check for empty text fields before deploying their mods. It’s always important to do a quick sanity check before going through the process of deploying.
Discuss what it means for the “pick random” block to pick an integer between two numbers inclusively. Do students understand what inclusively means? Give them a few examples. If you put -5 and 5 into the “pick random” block, is it possible for it to return -5? How about 0? How about 4.5?
Some of the examples using the “equals” block might confuse students. Here’s the examples and explanations for them, as well as some bonus examples:
[ ] = [ ]
There is no value in either slot. Blank slots are the same as each other, so this will evaluate to true.
-3 = 3
These two integers are different, so this evaluates to false.
this = that
These are both strings (a data type that you can use to store characters, words, and sentences), but they’re not the same! This is false.
0 = 3
Again, these are both integers, but they have different values. This will also evaluate to false
1 = 1
This is true! Both are integers and they have the same value.
false = 0
This is a tricky one. The words “true” and “false” (when spelled in all lower case and not part of a larger string, like “true x”) are special words that the computer stores differently. It evaluates “false” as 0 and “true” as 1. So if you were to have an “if” statement with 1 as the condition, it would always run the code inside the “if” statement. If you had 0 as the condition, it would never run the code inside. So false = 0 is true!
Again, this is true. The value “true” is evaluated as 1, so these two terms are equal.
true = false
Whether you’re comparing these as strings or integers (1 = 0), this is false.
This = this
Although these are the same word, capitalization matters! The computer does not interpret these as the same string, so be careful when you’re comparing strings.
3 = three
This will be false because these two values have different types. Although most humans would say that this is true, the computer doesn’t parse “three” as a number equivalent to 3; it only sees it as a series of five letters. It doesn’t know the meaning of this word.
Script variables are tricky at first, but they’re super useful and students will be using them a lot. Variables allow you to keep track of values that may change during the course of your program. You can also use these values to make decisions. Variables are great for keeping score, checking how much time has elapsed, tracking the players in the game, and much more.
It’s important to think about the scope of your variable whenever you create it. If you have a series of connected code blocks, any blocks below the “script variables” block will be able to access the script variable, but the blocks above will not be able to use the variable because it hasn’t been created yet.
Another subtle, yet important point is that if a script variable is created inside of a loop or conditional, you cannot reference it outside of that.
You need to initialize each variable you create by setting its value to whatever you want it to be at the beginning.
The last block students will learn is the “set color of sheep” block.
4. Rainbow Sheep (DIY project)
In this project, students will create a mod that spawns several randomly colored sheep whenever the player throws a snowball.
Students have already used the “spawn entity” block, but for this project, they’ll be using the rounded version of it instead. This allows them to create a sheep and simultaneously set its color.
This is the longest mod that students have made thus far, so there are a lot of opportunities to make small errors. Before students start customizing their mod, encourage them to make sure it works properly first. Then they can tweak the code to make it their own!
Students will learn about the “when [entity] dies at [location]” event block and the “get killer of” block.
The concept of a global variable is introduced. These are really useful, but students should still think about using a script variable instead whenever possible because it’s considered better practice than creating tons of global variables.
The example shows how students can increase the value of a variable by 1 or 100. How could they decrease the value of a variable? Hint: The number in the “change variable by” block can be negative to subtract from the value of the variable.
6. Master Hunter (DIY project)
This is the first time students will create their own global variables. It requires exiting out of the tutorial view by clicking the “Blocks” tab and clicking the “Variables” section to add a new global variable. If they miss this step, they won’t be able to do the rest of the mod!
This project also introduces the “and” block, which allows you to simultaneously check two conditions in an “if” block. Ask students how they could do this same thing without an “and” block. Hint: You could create two “if” statements nested inside each other, each of which checks a different condition.
The last slide of the tutorial suggests that students use the “mod” block for an extension. If students are interested in this, you’ll need to give them a bit of background on what mod is, as it’s not commonly taught in school. Mod is an operation (just like addition or multiplication) that gives the remainder of a division problem. Take the example of 6 mod 4. 6 divided by 4 is 1 with a remainder of 2, so 6 mod 4 is 2.
The “do after” block allows you to delay code so that it doesn’t happen immediately. If students have done coding before, they might be familiar with the “wait” block, but because of the way Minecraft is set up, “wait” blocks don’t work.
One important subtlety to notice is that, although code blocks inside a “do after” block will be delayed, blocks under a “do after” don’t wait.
The “get location away from [location]” block will be tricky for students who haven’t learned about coordinate planes yet. Be prepared to explain what x, y, and z coordinates mean and how you can change these coordinates to move around a three dimensional space.
Ask students to explain the example given. Why does each pig appear where it does?
8. Pigstorm (DIY project)
In this DIY project, students will create zombie pigs by spawning pigs and then casting lightning at the location of the pig.
The bonus slide of the tutorial suggests using the multiplication operator to speed up the lightning strikes. The values you put in a multiplication operator block can be variables or constants.
Wrap Up and Extend the Learning