Coding problem with Ogre Encampment


#1

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")

I’m running in Python.

Any thoughts or suggestions?


#2

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")

#3

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


#4

Dang JFBM you beat me to it!


#5

Does it need to be if enemy, or if ogre?


#6

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.


#7

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)?


#8

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())

#9

I also tried not doing the attack the chest, and that whole programming didn’t work.


#10

As J_F_B_M said above - You have do what the comments say (iow: insert the code they tell you to)…

# If there are no enemies, attack the "chest"

and

# Else, attack the "enemy"

Otherwise you aren’t following directions and shouldn’t expect your code to work. :smile:


#11

Got it! Thanks everyone! I realized after restarted it a few times that I had a problem with my indentation!


#12

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)


#13

Hello, Charlotte. Please format your code properly according to the FAQ. Otherwise, it’s hard to tell what’s wrong with your code.


#14

Make sure you have indented everything that you expect to be in the "loop:"
if you have say:

loop:
    enemy=self.findNearestEnemy()
if enemy:
    self.attack(enemy)
    self.attack(enemy)

it will loop forever doing only “enemy=…” and never get to the “if” and so never attack…


#15

same DOES NOT WORK! My code:

enemy = self.findNearestEnemy()

loop:
enemy=self.findNearestEnemy()
if enemy:
self.attack(enemy)
self.attack(enemy)


#16

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”)


#17

Are you using a while true loop?