Leave it to Cleaver bugged?


#1

Can’t seem to get past this one. The code works perfectly but then will only cleave when the last ogre comes. No normal attack.

# This shows how to define a function called cleaveWhenClose
# The function defines a parameter called target
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        hero.isReady("cleave")
        hero.cleave(target)
    else:
        hero.attack(target)
        
        # else, just attack target!
        

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the enemy as target.
        cleaveWhenClose(enemy)


#2

You aren’t using the hero.isReady method properly and once you correct that, your structure will have to be corrected as well. Look at the methods on the game screen and it will tell you how to use each one properly.


#3

Will take a look but the code seems to hang on the hero.cleave(target) part. Fine up until the last ogre for some reason.


#4

this should be

        if hero.isReady("cleave") :
            hero.cleave(target)
        else:
            hero.attack(target)

#5

Thanks, seemed to work. Just so I understand though, here’s the code:

(Solution removed)

So due to the new spacing put in, all i’ve done is “nested” all the attack code within the distanceTo(Target) code? So if the distance condition is met, it’ll execute the chain of attacks (Cleave or Normal Attack).


#6

I’m glad you finished but in the future please do not post final solutions / working code. The purpose of this board is to help people learn and simply providing/posting completed solutions is counterproductive to that goal. The best way to get assistance with a level that you’ve completed but want to improve, or have someone look at, would be to create a post - sans the code - and explain that you would like someone who can help improve it to respond so you can send a private message with your code to them. There are many people here willing and able to help. I’ve done this before myself.


#7

My mistake.

Could I PM you the code for an answer to the above question?

Thanks.


#8

yes, but it may be a couple of hours before I can get to it.


#9

Can’t even find the PM button to be honest :disappointed:


#10

Go to your profile page, click on “Messages” and press “New Message”.


#11

Only members and above can PM


#12

So @gman019, you will not be able to PM


#13

Seems not. That’s a shame.


#14

Just try to get yor trust level up, that way you can.


#15

Hi there everyone –

This is a common type of error. Most commands have a cooldown, a period of time before they can be used again. Many users will skip seeing if a command is ready.

hero.isReady("cleave") doesn’t do anything by itself. You need to check if cleave is ready, and then you can cleave.

My favorite item, Emperor’s Gloves, lets you cast chain-lightning periodically, but again, you have to check if it’s ready.

if hero.canCast("chain-lightning", target):
    hero.cast("chain-lightning", target)

If chain-lightning wasn’t ready OR if there was no variable named target, the spell would not be cast.

Maka


#16

You can use CanCast or you can use IfReady.


#17

I would prefer IfReady because its range of items is greater .


#18

Just to be clear, you use these to check if something else (like a command) can be used and ifReady is not a command. You check if some ability of an object isReady – for example, checking if the hero’s cleave ability is ready.

if hero.isReady("cleave"):
    hero.cleave()

isReady won’t work on spells, it doesn’t matter if you prefer to use isReady because for spells, you need to use canCast. This is because most spells need a target, so you’re not just checking if a spell is ready but also if you can currently cast it on whatever your target is. This could be a chain lightning spell on an ogre or a healing spell on a soldier. In both cases you would want to first see if you can cast the spell, and if you can, then you do so.

If you don’t check first, and you give the command to try, the hero will get confused and stop acting. You said zap the enemy but there is no enemy in range, so the poor hero gets confused.