Pig Stacking Plugin

About this lesson

Use loops to place a pile of entities atop one another.

Concepts Reviewed:

While Loops
Planning
Step 1
In this lesson, we will use loops to create a tower of pigs
Temp1519257446
Planning
Step 2
First we will use loops to make a simple function

This function will be used to display many blank lines in chat.

Temp1517538000
Step 3
Create our function

exports.name turns that name into a function you can call in chat

Temp1517538128
Step 4
Create an empty line in chat

Add this code into your clearChat function

Temp1517538211 Temp1517538321
Step 5
Create a variable to control how many times we want to do this.

linesToClear is a variable that will keep track of how many times we want to echo

Temp1517538688
Step 6
Create a While loop

This while loop should be around your echo, but beneath the linesToClear variable

Temp1517538985
Check Your Work
Step 7
If we run it now, our program will fail
Temp1517960717
Challenge
Step 8
Talk to an instructor about why this doesn't work!
Temp1517961054
Step 9
Decrease linesToClear after you clear a line
Temp1517961146 Temp1517961284
Check Your Work
Step 10
This is the code you should now have
Temp1517961392
Challenge
Step 11
More lines!

Make your function clear all visible lines from chat

Temp1517961451
Planning
Step 12
It's pig stacking time

The rest of the lesson will walk you through creating a stack of pigs.

Temp1519257489
Step 13
Spawning a single pig

This is the code to spawn a pig.
In order to use it, we need a world and a location

Temp1517961692

Type the following two lines into chat to set up these variables.

Temp1517961749 Temp1517961835

Now we can use the above command to spawn a pig at that location in that world

Temp1517962127 Temp1517962221

Note that every time we reset the world we will need to recreate these variables

Step 14
Set up what we just did in game as its own function

Create pigStack as a new function at the bottom of our code
This will let us call */js pigStack() to spawn a pig at our location

Temp1517962678
Check Your Work
Step 15
Pig!
Temp1519257524
Challenge
Step 16
Pigs!

Make a loop like we did above to create more pigs!

Temp1517963355
Planning
Step 17
Return types

when you call the world.spawnEntity(location, entityType.pig) function in chat, it returns to you CraftPig
CraftPig is the pig we just spawned

Temp1518130008

we can store this return in a variable to further affect our pig

Temp1518130063 Temp1519257549
Step 18
Store Pigs being spawned in variables

Add this to our pigStack function.
This will allow us to reference the pigs we are creating.

Temp1518130303

Lets give them names to better keep track of them

Temp1518130396 Temp1519257603
Step 19
Now to stack them!

add this after you give them custom names

Temp1518130574 Temp1519257618
Step 20
More Pig Stacking!

update your while loop so we make a new upper pig each time.

Temp1518130828 Temp1519257643

This stacks all of the pigs on top of one pig

Step 21
Taller Stacks!

make the old upperPig the new lowerPig

Temp1518131040 Temp1519257722
Challenge
Step 22
Different Creatures

change the entityType to your favorite creature

Temp1519259265 Temp1519259812 Temp1519259895 Temp1519260306
Pig Stacking Plugin Info

Account

MVCode Clubs

Created By

Missing tiny jake wheaton

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach