Pet engineer, code doesn't work

Everytime the enemy comes, my pet doesn’t move at all.
This is my code:

// Move your pet to the left or right button as needed.

function onHear(event) {
    // Find the guards to listen to.
    var archer = pet.findNearestByType("archer");
    var soldier = pet.findNearestByType("soldier");
    // If event.speaker is the archer:
    if (event.speaker && "hear" == "archer") {
        hero.moveXY(32, 30);
    }
        // Move to the left button.
        
    // If event.speaker is the soldier:
    if (event.speaker && "hear" == "soldier") {
        hero.moveXY(48, 30);
    }
        // Move to the right button.
        
}

pet.on("hear", onHear);

// You don't have to change the code below.
// Your hero should protect the bottom right passage.
while(true) {
    var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
    }
}

1 Like

Are you moving the hero or the pet in the pet onHear function?

1 Like

going to do that
(post needs 20 characters)

1 Like

ok, I changed it, but the pet still won’t move. Is there anything wrong with the hear thing?

1 Like

Place this into your code

hero.say(event.speaker);

after the var soldier = pet.findNearestByType("soldier"); and ask yourself how the if statement should be created.

2 Likes

I will. I’ll reply after.

1 Like

I’m having the same problem with my code. I tried hero.say(event.speaker), but my pet is just not responding to my commands. I looked at my code and I thought that it seemed fine, but the onHear code stops after defining soldier.

def onHear(event):
# Find the guards to listen to.
archer = pet.findNearestByType(“archer”)
soldier = pet.findNearestByType(“soldier”)
hero.say(event.speaker)
# If event.speaker is the archer:
if event.speaker == “archer”:
# Move to the left button.
pet.moveXY(32, 30)
# If event.speaker is the soldier:
if event.speaker == “soldier”:
# Move to the right button.
pet.moveXY(48, 30)

pet.on(“hear”, onHear)

while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)`

Could you format your code as it is explaned here?

Mod edit: Since the code originally posted is so close to an actual solution, the code section has been modified to show only the problem area. This will ensure that others will not be able to copy it and pass the level without actually learning the lesson.

    # If event.speaker is the `archer`:
    if event.speaker == "archer":
        # Move to the left button.
        pet.moveXY(32, 30)
    # If event.speaker is the `soldier`:
    if event.speaker == "soldier":
        # Move to the right button.
        pet.moveXY(48, 30)

Sorry, I realized that the event.speaker was an “archer”, not an archer. Thank you!

3 Likes

Well done! I was just starting to type that, but it’s much better that you’ve solved the problem yourself :smile:.

2 Likes

I just wanted to tell you that! :smiley: Then congratulations for completing the level! :partying_face:

1 Like

what do you mean by it was “archer” not archer??? there is no difference that i can tell can anyone help me @KalPal @AnSeDra @jka2706

1 Like

My understanding is that putting a word inside quotation marks (either ’ ’ or " ") makes it into a string. Without the quotation marks it might be a variable (assuming you’ve defined it as something). So “archer” and archer will be interpreted differently.

Jenny

1 Like

thank you so much i just deleted the " and it still wouldn’t work and then i realised my glasses is see through
edit: nvm i pressed submit now its not working?

now 2 brawlers kill the peasants

Can you post your code?

1 Like

def onHear(event):
    # Find the guards to listen to.
    archer = pet.findNearestByType("archer")
    soldier = pet.findNearestByType("soldier")
    hero.say(event.speaker)
    # If event.speaker is the `archer`:
    if event.speaker == archer:
        # Move to the left button.
        pet.moveXY(32, 30)
    # If event.speaker is the `soldier`:
    if event.speaker == soldier:
        # Move to the right button.
        pet.moveXY(48, 30)

pet.on("hear", onHear)

# You don't have to change the code below.
# Your hero should protect the bottom right passage.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)

this is my code @jka2706
edit:

problem

the pet goes the right artillery but it doesn’t work and then he goes to the left and then back to the right but when he fires it at the right it’s too late and two brawlers kill the humans

Take out this line. When I put it into my code it makes it stop working.

Jenny

2 Likes

thank you @jka2706(20char)

1 Like