Learn what your agent is and how to program it to assist you in Minecraft.
In this project we are going to learn what a Minecraft agent is and how you can use it to help you do things in Minecraft.
First, open Minecraft Education Edition, login, create a new world and open the Code Builder.
To create a new Minecraft coding project you will need to follow these steps:
The agent is your assistant who helps you do things in Minecraft. You command the agent with the agent blocks.
Your agent appears when you teleport it to you. When you have your agent, it can do work for you. Give things from your inventory to your agent and have it build stuff for you.
You make your agent go in different directions by moving, turning, and detecting blocks in the way.
First let's make the agent to come the position in the Minecraft world that we are in.
Create a command called summon and give it this code.
player.onChat("summon", function () { player.say("Hey Agent!") agent.teleportToPlayer() })
Go to your world and run the summon command. The agent should come to you.
To run a Minecraft command follow these steps:
Next let's create a command to make the agent dig a certain number of levels that we tell it to.
For each level the agent will repeat the following 4 times:
Add the following code to create the command.
player.onChat("dig", function (levels) { for (let i = 0; i < levels; i++) { for (let i = 0; i < 4; i++) { agent.move(FORWARD, 1) if (agent.detect(AgentDetection.Block, DOWN)) { agent.destroy(DOWN) agent.collectAll() } agent.turn(LEFT_TURN) } agent.move(DOWN, 1) } })
Now run the command in your world (e.g. "dig 3" to dig 3 levels down) and watch the agent dig down the number of levels you specified. Try it a few times with different levels.
The for block is a loop block that starts the variable i at 0 and then adds 1 to i each time that it runs through it's code until i equals the number after "to" in the block.
Because we start at 0, we need to subtract 1 from the levels variable that we pass in.
For example if we pass in the number 4 for the levels ("dig 4") that will make the for loop do the following:
Now let's make the agent build something!
Create a command called tower and add the following code to it to make the agent build a tower 10 blocks high.
player.onChat("tower", function () { agent.move(FORWARD, 5) agent.setSlot(1) agent.setAssist(PLACE_ON_MOVE, true) agent.setAssist(DESTROY_OBSTACLES, true) for (let i = 0; i < 11; i++) { for (let i = 0; i < 4; i++) { agent.setItem(SANDSTONE, 16, 1) agent.move(FORWARD, 4) agent.turn(LEFT_TURN) } agent.move(UP, 1) } })