We can attach code to a word we type in the chat.
Sample Code:
def on_chat():
player.say("You used the 'start' command!")
player.on_chat("start", on_chat)
Try: Type start in the game chat and see the message appear.
We can make choices in our code using if and else.
Sample Code:
health = 10 # pretend health value for this example
if health > 5:
player.say("You are healthy!")
else:
player.say("You need healing!")
Discussion:
health > 5 is a Boolean condition (true or false).if block runs when the condition is true.else block runs otherwise.Here we use a chat command and a conditional to choose between different builds.
Sample Code:
def build_structure(struct_type):
if struct_type == 1:
player.say("Building a stone tower!")
for y in range(5):
blocks.place(STONE, pos(0, y, 0))
else:
player.say("Building a glass pillar!")
for y in range(5):
blocks.place(GLASS, pos(0, y, 0))
def on_chat_type(num):
build_structure(num)
player.on_chat_number("build", on_chat_type)
Usage:
build 1 in chat → builds a stone tower.build 2 in chat → builds a glass pillar.(If your Python template does not support numeric chat parameters in this way, you can simplify to one structure per command such as "tower" and "pillar".)