Bonemender What Am I doing Wrong?

Can anyone tell me what I’m doing wrong please…

# Heal allied soldiers to survive the siege.
while True:
    if hero.canCast("regen"):
        bernardDistance = hero.distanceTo("Bernard")
        if bernardDistance < 10:
            # "Bernard" needs regeneration!
            hero.cast("regen", "Bernard")
        # Use "if" and "distanceTo" to regenerate "Chandra"
        # if she is closer than 10 meters away.
        if hero.canCast("regen"):
            chandraDistance = hero.distanceTo("Chandra")
            if chandraDistance < 10:
        # If you aren't casting "regen", use "if" and "distanceTo"
        # to attack enemies that are closer than hero.attackRange.
        enemy = hero.findNearestEnemy()
        enemyDistance = hero.distanceTo(enemy)
        if enemy:
            if enemyDistance <10:
             passste code here

can you send me a screen shot? maybe i can help

1 Like

Hello and welcome back to codecombat discourse @BlackRabbit! :partying_face: This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

And what level is this @BlackRabbit?


its a subscriber level called bonemender

you dont need this enemyDistance = hero.distanceTo(enemy)
and this if enemyDistance <10:
hope this helps

1 Like

@cheddarcheese is correct that if you remove those lines your code will work, but the actual error lies in the fact that you’re defining enemyDistance, without knowing whether the enemy exists. You simply move that line to another location where it will only run when you know an enemy exists.
Instead of 10 as the distance you can use: hero.attackRange(), that’s what’s recommended in the hints and it will find the range of your wand.


thanks for telling me i missed out on that point @Deadpool198!

1 Like

That’s ok, thank you!


@Deadpool198 : I despair when I see a code where “Strings” and all its derivatives are attacked , cast, taken distance to…
The above code will fail if Bernard or Chandra are killed. There is very good post how to find a minion by id (Name): Easiest way to select the enemy hero
You can use this method without advanced glasses or books. Simply remember the pattern. You can find this way id, type, team of the unit(s).
So for me the beginning of code will look like this:

while True:
    friends = hero.findFriends()
    Bernard = [friend for friend in friends if in ["Bernard"]][0]
    Chandra = [friend for friend in friends if in ["Chandra"]][0]
    if hero.canCast("regen"):
        if Bernard:
            bernardDistance = hero.distanceTo(Bernard)
            if bernardDistance < 10:
                hero.cast("regen", Bernard)
        if  Chandra:  
            chandradDistance = hero.distanceTo(Chandra)
            if hero.distanceTo(Chandra) < 10:
                hero.cast("regen", Chandra)


the level is designed to pass even you completely comment the else clause…
but it’s really important to use properly if enemy, if friend clause and learn hero.attackRange()


I double and quadruple Checked again, it doesn’t actually say to use attackRange in the hints.

1 Like

It states in the Puzzel that you have to use the names and not the I.d.

1 Like

@BlackRabbit . . . ?

1 Like

You need to delete an indent so it will be on the same line as the first if hero.canCast("regen")

You do not need enemyDistance, just check if there is an enemy, and then attack the enemy.

1 Like

Thanks for the offer to help, you are right it is bonemender but I don’t think it explains it well enough about hero.attackRange before you go into this puzzel. It will be the first time encountering hero.attackrange and I really think they should have used it as an example somewhere in some interactive code rather than making it so you have to guess what would work. Which is difficult if you have not seen it working context before.

Thankyou hun, its backwards forest so level 2 and its the Bonemender Puzzel. But I have done it now thanks.

Well, if that happens you’ve lost anyway so it doesn’t matter all that much. But yeah your code is better.

What about this:

1 Like

Yep that would be in the Comments not the Hints… but if you never used hero.attackRange or seen how its used how would you know how it works. How do you know if you need to define it? I know where it gives a value for it. I don’t want to give any spoliers unless that is part of the learning process. But from the comments and the Hints how would you know the value , I’m not sure if im explaining it well enough but it is a variable that can change so should not be referred to as a given value when learning and not having used it before. I’m just trying to help for other people learning in that maybe it can be described better in the hints or comments. I have done the puzzel now so hopefully if someone does get stuck these pointers may help them get ALOT closer than I first did.

1 Like

I’ve seen awful examples of 200 lines of code in the Clash of Clones where the hero attacks “Enemy 1”, “Enemy 2”…, “Enemy N”. Even in the Glacier this error is common. This way of coding is so perplexing…

1 Like

Yes, you’re right. It’s probably important to stop that habit now, before you move on to harder levels. It really isn’t a good tactic.
Yes @BlackRabbit you’re also right, it hasn’t explained hero.attackRange() that much.


Thanks Deadpool. That means Alot i got so frustrated by it yesterday i almost quit coding thinking it was me.