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)
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.
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).
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.
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.
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 objectisReady – 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.