Eagle Eye (Backwoods Forest) Problem

Could I ask you format your code? Use triple ` to wrap your code.
Because in the current state I don’t see indents and they are really important in Python.

1 Like

Ah, finally figured it out.

[redacted, please don’t post solutions]

But, I have a question: aren’t lines 7 & 8 redundant? Thanks.

1 Like

The shouldAttack method does not perform any action, it merely returns a boolean (true/false) so that you can use it in an if statement before calling the attack method, which actually performs the attack action.

If there’s any problem with the code you’ve posted, it is naming the result of the findNearestEnemy method as ogre. That is because findNearestEnemy may return an ogre or a burl in this level, so your ogre variable may hold something that is not an ogre at all.

2 Likes

Thanks i was stuck with that for a a day thx :grin
SPOILER ALEART FOR SECRCRET LIFE OF PETS:In the secreat life pets it starts like this…NOPE:sweat_smile:

2 Likes

The hero.shouldAttack is not a function due to the fact that the “hero” part is supposedly mean’t to be replaced by “self”.
This is my theory so forgive me if I’m wrong.
This is my code:

Ogre = self:findNearestEnemy()
while true do
local target = hero:findNearestEnemy()
local shouldAttack() = target shouldAttack()
if doAttack then
hero:attack(target)
end
end

The game says the script is "end expected (to close if at 3:5) at 3:5"
Is this a bug?

2 Likes

@S_scopes_S Both hero and self should refer to your hero unit—most of the time (self may reference something else in the context of classes and event-oriented programming, but CodeCombat has just started working on these concepts and there are very few levels that address these so far). Thus, the recommended way to reference your hero unit is to use the hero identifier, but self will also work for the time being.

Also, please take a look at the FAQ - I need help with my code! to see how to format your posted code correctly.

As for your code, this line seems odd:

local shouldAttack() = target shouldAttack()

I believe it should be:

local doAttack = hero:shouldAttack(target)
2 Likes

Thanks for the correction, however, whenever i use the term ‘hero’ the game reports it as an error.
Is this because I’m using Lua, or is it a bug?

2 Likes

Please post your entire code for the level so we can take a look.

2 Likes

This is refusing to attack 1 of the Ogres as it walks by and running out of time. Any reasons why that you can see?

2 Likes

@Xeriph Your code seems okay. I believe you may have got a bad seed where there’s an enemy that you shouldn’t attack staying closer to you than the one you should attack, resulting in this bug.

Please try resubmitting the level (click “Submit” again) and check if it helps.

2 Likes

So I’m having a problem now with it where it just doesn’t want to attack any of the ogres?
here’s the code

Ogre = self:findNearestEnemy() while true do local target = hero:findNearestEnemy() local doAttack = hero:shouldAttack() if doAttack then hero:attack(target) end end

2 Likes

Well let me just show you

You shouldn’t attack the burls, only the ogres!

while True:
target = hero.findNearestEnemy()
# Some of the targets will be Burls. You don’t want to attack them!
# Use this to tell if you should attack: hero.shouldAttack(target)
doAttack = target # ∆ Change this to use shouldAttack()
if doAttack:
hero.attack(target)
This is what it looks like before you change it
To help you the only thing you need to do is change if doAttack to if hero.shouldAttack()
(IDK If your using python but thats what this is. you dont need to change anything else! i thought i was way harder!

2 Likes

This code works…:slight_smile:

2 Likes

Here is one of many code I have tried

enemy = hero.findNearestEnemy()
if enemy:
if hero.shouldAttack(target):
hero.attack(target)

1 Like

I can’t get my hero to attack ANYONE

let alone using the “should I attack”

even
enemy= hero.findNearestEnemy()
if enemy:
hero.attack(enemy)

the usual appoach elicits no attacking on my heros part!!!
Mod edit: Please don’t swear

1 Like

Maybe you are forgetting to put your code inside a while True: loop? You need a while-loop in order to run your code repeatedly, otherwise it only runs once at the beginning of the level.

Ok guys, I spent few hours on this. At the end I have discovered that my hero was equipped with hammer instead of sword. That was the reason the function hero.attack(target) didn’t work. Thought it my be helpful.

Best,
Krystian

1 Like

@Krystian_Cieslak This is one of the first things I now check with my code. (after countless times that I made that mistake)

spoiler alert this code WORKS!!!

[please don’t post solutions]

when you barley start the level on line 7 u just have to put hero.attack(target)