Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

I am getting an unexpected error using findNearestEnemy() on Kithgard Brawl

# Survive the waves of ogres.
# If you win, the level gets harder, and gives more rewards.
# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
while True:
    enemy = hero.findNearestEnemy()
    if hero.isReady("cleave"):
        hero.cleave(enemy)
    else:
        hero.attack(enemy)

The error says "attack’s argument expected object but got null

Howdy and welcome to the forum!

What happens if there is NO enemy?

Hi,

hmm I’m not sure how to deal with this.

If I but the above if statement inside another I guess? But what condition

EDIT: Maybe somethign like:

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

How do I say if enemy is not null?

‘if enemy’ does that for you. You are testing to see if the object ‘enemy’ is null (false) or True (it has a value). Tho not in this case, there might be instances where you want to test for ‘no enemy’. Something like this, for example:

    if item and not enemy:
        getItem(item)
    elif enemy:
        hero.attack(enemy)

Ah okay thanks. When I have a chance to get back to my pc I’ll have another go.

I just started learning to program. I have a book called python crash course and found this game by luck. I even learned things here I’ve yet to cover in the book. Like while loops and if statements

There isn’t an app so there?

Here you can put instead something like this:

elif enemy and enemy.health>0
1 Like

True, but in this case, everything is within the ‘while true’ loop, so ‘enemy’ gets refreshed every iteration…dead enemies will not be included in the array.

Anyway, that is still a good suggestion to keep in mind. :slightly_smiling_face:

Sorry I have given it a go here but it isn’t working as expected stil

# Survive the waves of ogres.
# If you win, the level gets harder, and gives more rewards.
# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
        hero.cleave(enemy)
        else:
            hero.attack(enemy)
    else:
        hero.say("Clear")

Opps not indented, sorry. Sorted.

What is (or isn’t) happening? And which level are you on in Kithgard Brawl?

The above code, indented correctly, works. He would attack the first enemy then after that just stand there and let them kll him. Poor guy I can relate :smiley:

It is the first level of the brawl. Sadly I accidentally submitted the broken version and now have to wait a day