I’m having trouble with Ogre Encampment with the attack part.
My coding looks like this:
# If there is an enemy, attack it.
# Otherwise, attack the chest!
loop:
# If there is an "ogre". attack it:
self.attack(self.findNearestEnemy())
# Otherwise, attack the chest!
self.attack("Chest")
Help us help you.
You say you have trouble with the level. To get the best help possible, you should tell us what happens, and what you expect to happen with your code.
In this particular case your missing an if enemy:-check. Always check if there is an enemy before trying to attack it.
Try to solve it yourself before looking in here
You can check for an enemy like this:
loop:
enemy = self.findNearestEnemy() # Can return undefined
if enemy: # Checks if enemy != undefined
self.attack(enemy) # Causes problems when enemy == undefined
else:
self.attack("Chest")
Welcome to the forums Code_Designer I see you have read the FAQ and formatted your code properly as most new members fail to do(I did it to) the first thing your code is getting stuck on the first attack because the chest is considered an enemy so the second statement is unnecessary
I get killed when the thrower ( I think) and a orc appear on the scene at the same time. Also, my kill the nearest enemy line isn’t working, even though I put the clause I have in front of it.
Now that you have new code, you’ll need to post that up so we can help. Also a little more detail than the line isn’t working would be good - is it never running, or showing an error (i.e. a red cross next to the line)?
Sorry, I haven’t been on here in a while… It’s showing a red error next to the line that says, “Fix your Code: Target line is null, is there always a target to attack? Use if and else.”
loop:
# Use if/else.
# If there are no enemies, attack the "chest"
self.attack("Chest")
# Else, attack the "enemy"
self.attack(self.findNearestEnemy())
I write the code below but i dont do any attacking at all. It says that I see an enemy but never attacks
loop:
enemy=self.findNearestEnemy()
if enemy:
self.attack(enemy)
self.attack(enemy)
I also am having trouble making mine work. This is what my code looks like. There is no error message that pops up, but my guy doesn’t do anything. He just stands there. How can I fix this?
if hero.findNearestEnemy():
enemy = hero.findNearestEnemy()
hero.attack(enemy)
else:
hero.attack(“Chest”)