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.
Ah, finally figured it out.
[redacted, please don’t post solutions]
But, I have a question: aren’t lines 7 & 8 redundant? Thanks.
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.
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:
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?
@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)
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?
Please post your entire code for the level so we can take a look.
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?
@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.
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
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!
This code works…
Here is one of many code I have tried
enemy = hero.findNearestEnemy()
if enemy:
if hero.shouldAttack(target):
hero.attack(target)
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
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
@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)