Agent Wall

Agent builds a wall @showdialog

Instruct your 🤖 Agent 🤖 to build a wall protecting your land from monsters. Agent wall

On chat command

Change the ||player:on chat command|| block on the workspace from “run” to “wall”

player.onChat("wall", function () {

})

Position the Agent

The first thing we should do is teleport our Agent to our player. And then we can move the Agent a few paces away from our player to start building the wall.

From the ||agent:Agent|| category, drag out an ||agent:agent teleport to player|| block and drop into the ||player:on chat command|| block. Then drag an ||agent:agent move forward|| block out of the Toolbox and drop after the ||agent:agent teleport to player|| block. In the ||agent:agent move forward|| block, change the number of blocks from 1 to 4.

player.onChat("wall", function () {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
})

Set Agent properties

Now we need to set some ⚡ properties ⚡ for our Agent so they will place blocks when they move, and also automatically destroy any obstacles that might be in their way.

From the ||agent:Agent|| category, drag out an ||agent:agent place on move|| block and drop into the end of the ||player:on chat command|| block. Then click on the off switch to turn it to on.

Drag out another ||agent:agent place on move|| block from the Toolbox and drop after the previous one. In this one, click on the ||agent:place on move|| drop-down menu and select ||agent:destroy obstacles||. Then click on the off switch to turn it to on.

player.onChat("wall", function () {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
})

Give the Agent blocks

We need to give our Agent some building 🧱 materials 🧱 to build the wall with.

From the ||agent:Agent|| category, drag out an ||agent:agent set block or item|| block and drop it at the end of our code in the ||player:on chat command|| block.

In the ||agent:agent set block or item|| block, click on the Grass Block drop-down menu and select a block to build the wall with (like Mossy Stone Bricks). And then change the count from 1 to 4, just to give us enough blocks for a row.

player.onChat("wall", function () {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    agent.setItem(MOSSY_STONE_BRICKS, 4, 1)
})

Move and Build

Now let’s move our Agent to place blocks while they go.

From the ||agent:Agent|| category, drag out an ||agent:agent move forward|| block and drop it at the end of our code. In the ||agent:agent move forward|| block, change the number of blocks from 1 to 3.

player.onChat("wall", function () {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    agent.setItem(MOSSY_STONE_BRICKS, 4, 1)
    agent.move(FORWARD, 3)
})

Move to the next row

At the end of the row, let’s move our Agent ↑ up to be ready to build the next row.

From the ||agent:Agent|| category, drag out another ||agent:agent move forward|| block and drop it after the previous one. In this block, click on the ||agent:forward|| drop-down menu and select ||agent:up||. This will move our Agent up by one and place the last block completing our row.

player.onChat("wall", function () {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    agent.setItem(MOSSY_STONE_BRICKS, 4, 1)
    agent.move(FORWARD, 3)
    agent.move(UP, 1)
})

Test our code

Let’s test our code! Press the 🟩 green 🟩 play button to run your code. Move to a flat area in the world and press “t” to open the chat window in Minecraft and type “wall” to start your Agent building a wall. How did it work?

Make the wall higher!

The first thing you may have noticed, is that our wall is only 1 block high! Let’s add some additional rows. And we can do this with a ⭮ Repeat ⭮ loop to make our code more efficient.

From the ||loops:Loops|| category, drag a ||loops:repeat 4 times|| block out and drop it into the ||player:on chat command|| block, right after the ||agent:agent destroy obstacles|| block, so that the ||agent:agent set block|| block and the two ||agent:agent move|| blocks are inside the ||loops:repeat|| loop.

player.onChat("wall", function () {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    for (let index = 0; index < 4; index++) {
        agent.setItem(MOSSY_STONE_BRICKS, 4, 1)
        agent.move(FORWARD, 3)
        agent.move(UP, 1)
    }
})

Turn the Agent

We also need to make sure our Agent is facing the right direction to build the next row in our wall.

From the ||agent:Agent|| Toolbox drawer, drag 2 ||agent:agent turn left|| blocks out and drop them into the ||loops:repeat|| block at the end after the ||agent:agent move up|| block.

player.onChat("wall", function () {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    for (let index = 0; index < 4; index++) {
        agent.setItem(MOSSY_STONE_BRICKS, 4, 1)
        agent.move(FORWARD, 3)
        agent.move(UP, 1)
        agent.turn(LEFT_TURN)
        agent.turn(LEFT_TURN)
    }
})

Run our code

Let’s test our code again! Press the 🟩 green 🟩 play button to run your code. Move to a flat area in the world and press “t” to open the chat window and type “wall” to start your Agent building a wall.

Support different wall dimensions

You should have seen your Agent building a wall 4 blocks wide and 4 blocks high. Great job! But what if you want a wall 6 blocks wide and 10 blocks high? Or some other dimension? Let’s add some code that will support different widths and heights.

In the ||player:on chat command wall|| block, press the plus (+) icon twice to expand the block to show two numbers. Click on the drop-down num1 menu and select Rename variable… Type width as the new variable name. This will represent how wide we want our wall to be.

Then click on the drop-down num2 menu and select Rename variable… Type height as the new variable name. This will represent how high we want our wall.

player.onChat("wall", function (width, height) {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    for (let index = 0; index < 4; index++) {
        agent.setItem(MOSSY_STONE_BRICKS, 4, 1)
        agent.move(FORWARD, 3)
        agent.move(UP, 1)
        agent.turn(LEFT_TURN)
        agent.turn(LEFT_TURN)
    }
})

Set width number of blocks

From the ||variables:Variables|| category, drag out a ||variables:width|| variable block and drop it into the ||agent:agent set block or item|| block in the count field replacing 4. This will give our agent enough blocks to build the width of our wall.

player.onChat("wall", function (width, height) {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    for (let index = 0; index < 4; index++) {
        agent.setItem(MOSSY_STONE_BRICKS, width, 1)
        agent.move(FORWARD, 3)
        agent.move(UP, 1)
        agent.turn(LEFT_TURN)
        agent.turn(LEFT_TURN)
    }
})

Move Agent forward

Now we need to move the Agent forward one less than the full width of our wall - because remember, we move up one at the end of the row.

From the ||math:Math|| Toolbox drawer, drag a ||math:(0 - 0)|| block out and drop into the ||agent:agent move forward|| block in our ||loops:repeat|| loop, replacing the 3.

From the ||variables:Variables|| category, drag out another ||variables:width|| variable block and drop it into the first field of the ||math:(0 - 0)|| block replacing the 0. In the second field, type a 1 replacing the second 0.

player.onChat("wall", function (width, height) {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    for (let index = 0; index < 4; index++) {
        agent.setItem(MOSSY_STONE_BRICKS, width, 1)
        agent.move(FORWARD, width - 1)
        agent.move(UP, 1)
        agent.turn(LEFT_TURN)
        agent.turn(LEFT_TURN)
    }
})

Set the height

Finally we just need to set the number of times our Agent will build a row in our wall.

From the ||variables:Variables|| category, drag out a ||variables:height|| variable block and drop it into the ||loops:repeat|| block replacing the 4.

player.onChat("wall", function (width, height) {
    agent.teleportToPlayer()
    agent.move(FORWARD, 4)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    for (let index = 0; index < height; index++) {
        agent.setItem(MOSSY_STONE_BRICKS, width, 1)
        agent.move(FORWARD, width - 1)
        agent.move(UP, 1)
        agent.turn(LEFT_TURN)
        agent.turn(LEFT_TURN)
    }
})

Run your code

Press the 🟩 green 🟩 play button to run your code. Press “t” to open the chat window in Minecraft, move to a flat location where you want your Agent to start building a wall, and type “wall 6 10” (or use different numbers) to start your Agent building a wall 6 blocks wide by 10 blocks high (or whatever dimensions you specify).

⭐ Great job!! ⭐ How big of a wall can your Agent build?