Skip to main content

MakeCode and Command Blocks

Comments

17 comments

  • Truong Can Van
    xin chào
    0
  • Truong Can Van
    thanks you
    0
  • Truong Can Van
    thanks you
    0
  • ahmed aden
    nn
    0
  • Paul Shin

    This is great. Thanks

    0
  • Márton Bori

    I know this is an old post, but maybe someone could help me with this.

    So I tried using this technic, and it worked, but only for the host. Is there any way to make this work for all players in a world?

    0
  • Bryan Sanders Community moderator
    Bug Spotter Minecraft Certified Beacon of Knowledge Moderator Expert (Gold)

    only operators can run commands

    0
  • Márton Bori

    But even operators couldn't do this, only the host of the world

    0
  • Bryan Sanders Community moderator
    Bug Spotter Minecraft Certified Beacon of Knowledge Moderator Expert (Gold)

    I'll have to test that. 

    0
  • Márton Bori

    And members can run commands as well.

    The

    /tell @p commandName

    command did execute for members also. Only it didn't start the function for them

     

    0
  • Bryan Sanders Community moderator
    Bug Spotter Minecraft Certified Beacon of Knowledge Moderator Expert (Gold)

    Dominick Penny

    0
  • Márton Bori

    Since then I found a work around by the way: If they sign up for the on_block_broken event, it works for members too.
    So instead of having a command block "tell" them a trigger word, I could have the kids destroy a certain kind of block, and always regenerate that block with a repeting command block. They just have to write in the code:

    blocks.on_block_broken(BLOCK_TYPE, method_name)

    And this does work with members too.
    The only problem with this is that for every method I want to trigger this way needs a different type of block, which can quickly become a mess. So it would be nice if the on_tell_command worked as well.

    But hey! At least it works! :)

    0
  • Debbie Alexander Community moderator
    Beacon of Knowledge Expert (Gold) Moderator

    OKay, I am intrigued. How are folks implementing this? I'd like gory details! =D Like, what are the commands you are implementing and how are your students / joined members managing? What are the world settings? How are you communicating the code they need? Like, very, very gory details! =] What kind of code are you running? Line by line, you know, all the minutia!  Am I being excruciating enough? (I considered a picture of an awful mess, but this is a family show!) Please tell me how this works!!!!!!! =]

    0
  • Márton Bori

    Okay, so in my instance the goal was to make a game where the students have to navigate their agents on a chessboard-like field by pushing buttons. I also wanted to let them code the movements of the agent themselves.

    My initial world looked like this:
    Below each button there was a command block with settings like this:

    The program itself is pretty simple: It consists of 5 functions for the 5 different kinds of movement, and their triggers. (In this case: on_tell_command):

    agent.teleport(pos(9, 0, 9), WEST)

    def forward():
      agent.move(FORWARD, 1)

    def back():
      agent.move(BACK, 1)

    def left():
        agent.turn_left()

    def right():
        agent.turn_right()

    def action():
    agent.set_item(WHITE_CONCRETE, 1, 1)
      agent.move(FORWARD,1)
        agent.destroy(DOWN)
        agent.place(DOWN)
        agent.move(FORWARD,1)
       
    player.on_tell_command("forward", forward)
    player.on_tell_command("left", left)
    player.on_tell_command("right", right)
    player.on_tell_command("back", back)
    player.on_tell_command("action", action)

    The whole program worked like this:

    https://drive.google.com/file/d/1q9NK_eZ6lrP545qCxYmMMfV2HnwI5Dia/view?usp=sharing 

     

    This solution is good, because I can simply write a skeleton code like this, and let the students themselves fill out the body of the functions.

    def forward():
        pass

    def back():
        pass

    def left():
        pass

    def right():
        pass

    def action():
        pass

    player.on_tell_command("forward", forward)
    player.on_tell_command("left", left)
    player.on_tell_command("right", right)
    player.on_tell_command("back", back)
    player.on_tell_command("action", action)
    0
  • Márton Bori

    The problem with the solution above, is that it only works for the host of the world. When anyone else tries to push the buttons, nothing happens.

    The thing is, I usually have my students join a single common world as members, just because this way it's way easier to keep their attention, and not let them get lost in the game itself.

     

    So after some try and error, I found out, that there are some triggers that work for members too. One of them is when the player destroys a block.

    So I changed all the buttons for a different kind of block (fortunately there are enough of those :) ), and instead of the on_tell_command(), I used the on_block_broken() trigger.

    My new world looked like this:

    Under the blocks there are command blocks, with settings like this to regenerate the glasses after they have been destroyed.

    The code itself has changed to this:

    agent.teleport(pos(9, 0, 9), WEST)

    def forward():
      agent.move(FORWARD, 1)

    def back():
      agent.move(BACK, 1)

    def left():
        agent.turn_left()

    def right():
        agent.turn_right()

    def action():
    agent.set_item(WHITE_CONCRETE, 1, 1)
        agent.move(FORWARD,1)
        agent.destroy(DOWN)
        agent.place(DOWN)
        agent.move(FORWARD,1)

    blocks.on_block_broken(WHITE_STAINED_GLASS, forward)
    blocks.on_block_broken(LIGHT_GRAY_STAINED_GLASS, right)
    blocks.on_block_broken(BLACK_STAINED_GLASS, back)
    blocks.on_block_broken(GRAY_STAINED_GLASS, left)
    blocks.on_block_broken(GLASS, action)

    This program worked for members too:

    https://drive.google.com/file/d/1tONg4Mu8guHy0ejOqzNZDjrBo453Mfwg/view?usp=sharing 

    0
  • Márton Bori

    Was that detailed enough? :)

    As an extra I'll write down the full game as well.

    • So the game is played by 2 players (black and white).
    • Each player starts by teleporting their agent to a tile with their own color.
    • The players can move their agent forward or back and turn it left or right.
    • They can also make their agent "jump over" a tile and change it's color to their own.
    • The agent can not move if it's on enemy territory (the opponents color)
    • The goal is to trap your opponent on a tile with your color.

    The full code of the project:

    origo = world(4,3,84)
    agent.teleport(pos(9, 0, 9), WEST)
    agent.set_item(WHITE_CONCRETE, 1, 1)

    def inspect():
        if (blocks.block_by_id(agent.inspect(AgentInspection.BLOCK, DOWN)) == blocks.block_by_name("wool")):
            agent.teleport_to_player()

    def forward():
        inspect()
        agent.move(FORWARD, 1)

    def back():
        inspect()
        agent.move(BACK, 1)

    def left():
        agent.turn_left()

    def right():
        agent.turn_right()

    def action():
        inspect()
        agent.move(FORWARD,1)
        agent.destroy(DOWN)
        agent.place(DOWN)
        agent.move(FORWARD,1)

    blocks.on_block_broken(WHITE_STAINED_GLASS, forward)
    blocks.on_block_broken(LIGHT_GRAY_STAINED_GLASS, right)
    blocks.on_block_broken(BLACK_STAINED_GLASS, back)
    blocks.on_block_broken(GRAY_STAINED_GLASS, left)
    blocks.on_block_broken(GLASS, action)

     

     

    0
  • Bryan Sanders Community moderator
    Bug Spotter Minecraft Certified Beacon of Knowledge Moderator Expert (Gold)

    Wow! Excellent work on the chess game! I’m going to borrow and try it. Thanks!

    0

Please sign in to leave a comment.