Run python code by pushing a button
I have the following setup:
I have a shared world where, I am the only operator, and the students are members. I'm using the classroom template with a few changes:
In each classroom there's a chessboard, and some buttons operated by command blocks to reset the board.
The goal of the lesson would be to create a game where 2 players control their agent on the board by pushing buttons.
I tried to solve this with command blocks, that use /tell to run a function in the python code, the students wrote. For example I put
/tell @p forward
in a command block, and the students put
player.on_tell_command("forward", forward)
into their python code, which should have resulted in executing the player's forward() function, when they pushed the button
The problem is, this only worked for the host!
When any other player (operator, or not) tried to put this in their code, and pushed the button, nothing happend.
Is this a thing, or did I just get something wrong?
-
How are the students adding the forward function? IOW do they add the "def forward():" and the program contents in the same program where they add the:
player.on_tell_command("forward", forward)
0 -
Yes, the whole program looks like this:
def forward():
agent.move(FORWARD,1)
def back():
agent.move(BACK,1)
def left():
agent.turn_left()
def right():
agent.turn_right()
player.on_tell_command("forward", forward)
player.on_tell_command("back", back)
player.on_tell_command("left", left)
player.on_tell_command("right", right)0 -
Okay, the code itself works for me in MakeCode Python (not Azure Notebooks)
And in order to work, since it is moving the agent, the agent has to be present
And as you say, the user has to have cheats on, and be able to code. I'm assuming the kiddos have the code pasted into Python in Makecode and have run it.
But a member can break, place and interact, but not code. I think this is your problem. Have your students try to run independent code. I think if they try to run a say command from MakeCode you will see they can't.
If they can, then we have another problem.
As a note, if you make this on a chat command they can just say "forward" and the agent will move without all the other typing.
0 -
As a note, if you make this on a chat command they can just say "forward" and the agent will move without all the other typing.
Yes, thats what we ended up doing, but its just so much more inconvenient, than triggering code by pushing a button.
But a member can break, place and interact, but not code. I think this is your problem. Have your students try to run independent code. I think if they try to run a say command from MakeCode you will see they can't.Thats true, they can't run say command from MakeCode, or any command that executes a normal /command, but they can run simple commands from MakeCode, like moving the agent. The problem is that there's no indication what so ever of a given MakeCode command's permission level.
So I never know if the kids did something wrong, or its just that members can't run that specific command.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
Please sign in to leave a comment.
Comments
4 comments