[SOLVED] Cloudrip Brawl: Where´s the problem?!

Hi guys, I´m really freaking out trying to get why I get an error in this level:

# If you win, the next time you play will be more difficult and more rewarding!
# If you lose, you must wait a day before submitting again.
while True:
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    
    if hero.gold >= 20:
        hero.summon("soldier")
    hero.move(hero.findNearestItem().pos)
    
    for friend in friends:
        target = friend.findNearestEnemy()
        hero.command(friend, "attack", target)
     
   
    if pet.isReady("charm"):
        ogre = pet.findNearestEnemy()
        if ogre and ogre.health > 100:
            pet.charm(ogre)

if I comment the part about pet.charm(ogre) it works smoothly, but without comments I get argument error in the line 14, hero.command(friend, “attack”, target)

I don´t see how they are connected, both blocks of code work if they are alone.

I’m guessing it is a timing issue. Since you use the charm, it creates an instance where your hero can’t find an enemy, but you command your friend to attack one that doesn’t exist. When you don’t charm, your hero finds the enemy so no error is created

Before any command method that needs a thing to attack or move to, you need to make sure there is something there.

There are two lines of code that may create this error, but only one seems to trigger it.

       target = friend.findNearestEnemy()
       # add a check between finding a target and attacking
       hero.command(friend, "attack", target)
----
    hero.move(hero.findNearestItem().pos)  # this line may also trigger an error if your hero can't find a Nearest Item.


1 Like

Yes, for sure would be better to check if something is there, but it doesn´t solve the problem.
Now it seems to work if I add a “break” at the end of the for cycle, but I still don´t get exaclty why I have to do that.

# Your pet has charmed an ogre, so he/she is a friend for some time. But you cannot command friends of type "ogre"

    for friend in friends:
        target = friend.findNearestEnemy()
        if target and target.health > 0 and friend.type != "ogre":
            hero.command(friend, "attack", target)

1 Like

Got it, I didn´t expect that the charmed ogre was considered a friend.
Thanks