Save the cake!
Introduction
To protect your cake block, you have to return to it periodically to make sure some other mob isn’t eating it! In order to return back to your cake, you need to remember the position it was placed at. We use variables in programs to remember numbers, logic values, text, and positions. In this tutorial, you will use variables to save position and time values.
Place a cake block
||blocks:Place||
a cake
block at your current position.
blocks.place(CAKE, pos(0, 0, 0))
Set the ‘cake’ variable
Make a new variable called ||variables:cake||
. Set the variable to the ||player:player's world position||
.
blocks.place(CAKE, pos(0, 0, 0))
let cake = player.position()
Set the start time
Now, make another variable to remember what the time is when you start to wander away
from the cake. Name this variable ||variables:wander||
. Set the ||variables:wander||
variable to current game time (use the ||gameplay:time query||
).
blocks.place(CAKE, pos(0, 0, 0))
let cake = player.position()
let wander = gameplay.timeQuery(GAME_TIME)
Make a ‘wander’ loop
Use a ||loops:forever||
loop to check how long you’ve wandered away from your cake. Inside the loop, use an ||logic:if||
condition to check to see if the game time is long enough to make you return to your cake.
blocks.place(CAKE, pos(0, 0, 0))
let cake = player.position()
let wander = gameplay.timeQuery(GAME_TIME)
loops.forever(function () {
if (gameplay.timeQuery(GAME_TIME) > 0) {
}
})
Check the ‘wander’ time
Inside the if condition, subtract the time in wander
from the current game
time. Make the condition true
if the time difference is greater than 300
ticks.
blocks.place(CAKE, pos(0, 0, 0))
let cake = player.position()
let wander = gameplay.timeQuery(GAME_TIME)
loops.forever(function () {
if (gameplay.timeQuery(GAME_TIME) - wander > 300) {
}
})
Wander message
If the 300
tick time expired, broadcast to the game that you’re going back to your
cake!
blocks.place(CAKE, pos(0, 0, 0))
let cake = player.position()
let wander = gameplay.timeQuery(GAME_TIME)
loops.forever(function () {
if (gameplay.timeQuery(GAME_TIME) - wander > 300) {
player.say("Back to cake!")
}
})
Return to the ‘cake’
Teleport your player back to the cake if the 300
ticks have elapsed. Also, set
the wander
variable to the current game time.
blocks.place(CAKE, pos(0, 0, 0))
let cake = player.position()
let wander = gameplay.timeQuery(GAME_TIME)
loops.forever(function () {
if (gameplay.timeQuery(GAME_TIME) - wander > 300) {
player.say("Back to cake!")
player.teleport(cake)
wander = gameplay.timeQuery(GAME_TIME)
}
})
Get it a try!
Run your program and start walking away from your cake. Did you get pulled back to
the cake after a short while? Change the tick time for 300
to a larger amount if
want to wander longer.