so I can’t seem to beat this level when most others seemed to be relatively easy. I simply get an error every time that says “function cleaveWhenClose() not defined”. The point of the level is to define a function and I feel as if I’ve put in all the code it requires as well as tried to play with it for hours to no avail.
`-- This shows how to define a function called cleaveWhenClose
– The function defines a parameter called target
function cleaveWhenClose(target)
if hero:distanceTo(target) < 5 then
-- Put your attack code here
-- If cleave is ready, then cleave target
if hero:isReady("cleave") then
hero:cleave(target)
-- else, just attack target!
else
hero:attack(target)
end
end
end
– This code is not part of the function.
while true do
local enemy = hero:findNearestEnemy()
if enemy then
– Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)
end
end
`
This is my code. Idk if it formatted correctly.
Thanks! This has been an issue for me for variables and I had thought about it for the function but just in a weird, roundabout way. This solved it for me
I am having a hard time with the code because when I push run it does everything except after my hero cleaves he doesn’t attack and dies after 21 seconds. Plz reply soon…
# The function defines a parameter called `target`
def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
ready = hero.isReady("cleave")
hero.cleave(target)
# else, just attack `target`!
hero.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)
also you know how the character is supposed to attack after he cleaves there is another ogre he says “but it’s dead” and it lets the ogre attack him.
def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
ready = hero.isReady("cleave") # you have a boolean variable
# use ready before cleaving
hero.cleave(target)
# else, just attack `target`!
# you have been told to use else if cleave is not ready
hero.attack(target)
so i did everything but in the end of fighting ogres a ogre pops up when theres like 3 seconds left and my code at that time is running the code find nearest enemy plz help
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
# If cleave is ready, then cleave target
ready = hero.isReady(“cleave”)
hero.cleave(enemy)
else:
enemy = hero.findNearestEnemy()
hero.attack(enemy)
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))
To help us see the formatting of the code, please review the link below to show you how to post your code with the formatting. That being said, I see a problem with your code.
ready = hero.isReady(“cleave”) # this should be an if statement not a variable
Your hero is waiting until the cleave is ready instead of attacking the nearest enemy.
Also, you should be using the variable target and not enemy in the function since that is the parameter provided.