[SOLVED] Need help in Agrippa Defense Returned

I don’t know what I’m doing wrong but it’s not working.Can I please have help fixing this?


def enemyInRange(enemy):
    # Return true if the enemy is less than 5 units away.
    if enemy < 5:
        return True
    else:
        return False

def cleaveOrAttack(enemy):
    if hero.isReady('cleave'):
        hero.cleave(enemy)
    else:
        hero.attack(enemy)

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Check the distance of the enemy by calling enemyInRange.
        if enemyInRange(enemy):
            cleaveOrAttack(enemy)

Hello, I had looked at your code, and I noticed your “enemyInRange” function is not doing what it should. That is because you are not using one command.

If you really need help, look over the commands that your items can do, and select the most appropriate.

Fine…

-hero.distanceTo(enemy) as the if-condition-

def enemyInRange(enemy):
    # Return true if the enemy is less than 5 units away.
    if enemy < 5:
        return True
    else:
        return False

If enemy is less than 5? Enemy is not a range. Distance is a range.

Be sure to define it first.

I still don’t understand what you mean by defining range. Isn’t enemyInRange(enemy); how you define in range??

hero.enemyInRange = function(enemy) {
return true;
if (enemy < 5 units) {
}
return false;
};

Do I have to edit anymore of the code below?

hero.cleaveOrAttack = function(enemy) {
if (hero.isReady(“cleave”)) {
hero.cleave(enemy);
} else {
hero.attack(enemy);
}
};

while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
// Check the distance of the enemy by calling enemyInRange.
if (hero.enemyInRange(enemy)) {
hero.cleaveOrAttack(enemy);
}
}
}

No, it isn’t defining anything. enemyInRange(enemy) is simply the name of the function that YOU need to define.

[spoiler]First, you have to define what distance is:

distance = hero.findNearest(enemy)

Then, tell the function what to do with that information:

if hero.distanceTo(enemy) < 5:[/spoiler]

1 Like

def enemyInRange(enemy):
# Return true if the enemy is less than 5 units away.
enemy = hero.findNearestEnemy()
if hero.distanceTo(enemy)<5:
true = 1
return true;
else:
false = 0
return false

def cleaveOrAttack(enemy):
if hero.isReady(‘cleave’):
hero.cleave(enemy)
else:
hero.attack(enemy)

while True:
enemy = hero.findNearestEnemy()
if enemy:
# Check the distance of the enemy by calling enemyInRange.
if enemyInRange(enemy):
cleaveOrAttack(enemy)

hope this helps you enough

Hey I was looking over this post because I was also confused, I got it to work on my own before I got to @Sirfirelords post by creating this function:

function enemyInRange(enemy) {
    enemy = hero.findNearestEnemy(); 
    // Return true if the enemy is less than 5 units away.
    if (enemy) {
        return true;
    }else {
        return false;
    }
}

From my understanding what you had to do was define the variable enemy to find the nearest enemy with the code

enemy = hero.findNearestEnemy();

then you had to make an if statement that asks if it is true or not and if it is return true and if not return false.

    if (enemy) {
        return true;
    }else {
        return false;
    }
}

Then if I understand the while loop correctly it checks to see if there is an enemy in range and under 5 units away and if that is true it will cleave the enemies and if there are are enemies but cleave is on cool down than it will default to regular attacking.

Hope this helps, I got through the level so I think I understand it feel free to correct me though or just let me know I was right!

you can simply do this by changing the less than to more than and switching round the return true and false.

function enemyInRange(enemy) {
// Return true if the enemy is less than 5 units away.
if (enemy > 5) {
return false;
}
else {
return true;
}
}

Enemy isn’t a range. You might want to switch your code to:
while (true) {
var distance = hero.distanceTo(enemy);
if (distance < 5) {
return true;
} else {
return false;
}
}
}

The rest of your code was automatically generated, so you don’t need to change that! Hope this helps!

i was looking at this Because i got confused and im really stuck here.