Event Handlers in Python Notebooks with Minecraft Education Edition
Event handlers are the big responders in coding. They are the bridge between code and the outside world. Event handlers are code that waits for an event, such as an action by a player in a Minecraft world, and they run code in response to this event. We can say it this way:
Event – an action, outside the code, that is recognized and responded to inside the code.
Event handler -code which “listens for” and responds to (handles) events outside of the code.
Examples of events in Minecraft include things like a player walking, a block breaking, or a certain word being said in chat. Code for events typically begins with the word “on” – on_player-travelled(location,mode,distance) is a built-in event handler in the Notebooks editor.
In this code, the event handler waits for the chat window to “hear” Hello, and when it does, the function chatter is called.
The code is entered, and and run is chosen, but no visible change will occur in the game until the event handler “hears” what it is “listening” for.
The event handler is “listening for Hello in chat. When it hears Hello, it will call the function named chatter.
Note the “Execution in process” message at the bottom. Event handlers do not run and then stop running the way other procedures do. They are intended to continue running in the background, constantly checking to see when they should respond.
When you go back into your world, you can go to chat (type T) and enter the word Hello.
Success!
Even though the code has “run” once, it isn’t done. It is still executing, waiting to respond again. If you type Hello again, it will reply again. Friendly, right?
You will have to end the process using the stop button, or by exiting Minecraft if you want it to stop.
Recent Comments