[Bug?] Leave it to cleaver

I have been trying this level for about 15 minutes and was like 90% sure this code was correct.

function cleaveWhenClose(target) {
    if(hero.distanceTo(target) < 5 && hero.isReady("cleave")) {
        hero.cleave(target); 
    } else {
        hero.attack(target);
    }
}

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        cleaveWhenClose(enemy);
    }
}

How ever when I hit run my guy would go through the attacks while running up to the top of screen to kill the enemies and at very end just stop until the time ran out and I would fail.

So I rewrote the code in a much longer way and it works every time, however my character just doesn’t run to the top of the screen anymore.

function cleaveWhenClose(target) {
    if(hero.distanceTo(target) < 5) {
       if(hero.isReady("cleave")){
            hero.cleave(target); 
       }
       else {
           hero.attack(target);
       }
    }
}

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        cleaveWhenClose(enemy);
    }
}

So is there a glitch when the character hits the top part of the screen where the enemies spawn?

3 Likes

I don’t think so but it might be the language your using. I used python and it worked for me if
I do:

Find Nearest Enemies
distance to:
if enemy distance < 10:
if enemy:
if self is ready (cleave):
cleave enemy
move to original position
else:
attack enemy
move to original position

2 Likes

I dont really know what the goals of the level are so I dont really know how your failing.

All I can tell is that in your first solution it will only cleave when enemy distance is less than 5 or attack an enemy from any distance.

In the 2nd solution your hero will only cleave or attack if the enemy distance is less than 5.

2 Likes

The goal was just to defeat all the ogres with out dying.

You had to make a function to cleave when cleave is ready or attack when cleave is not ready as long as the enemies are with 5 units of you.

2 Likes

Well that explains why your 2nd solution works then. Your 1st solution didnt do what you needed to do. Does it make sense on why your 2nd solution works?

2 Likes

Why not? If the target is within 5 units and cleave is ready he will cleave? If not he will default to a regular attack?

2 Likes

Yes since the if part was false it went right to the else. Which would attack an enemy from any range.

Your 2nd solution fixed that problem. It wouldnt do either of the the attacking methods unless the enemy was with in a distance of 5 or less.

2 Likes

Are you sure? I just went back to the level and pasted in

function cleaveWhenClose(target) {
    if(hero.distanceTo(target) < 5 && hero.isReady("cleave")) {
        hero.cleave(target); 
    } else {
        hero.attack(target);
    }
}

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        cleaveWhenClose(enemy);
    }
}

and now it works, I didn’t change anything. How is it false?

If the enemy walks within a distance less then 5 units then I will cleave the enemy if it is off of the cool down. Else I will just attack the enemies with basic attacks. I don’t understand what you are saying. Doesn’t the code work like this?

if this is true, which is it when the enemy comes with in 5 units of my character and I have cleave off cool down then it will run that code.

    if(hero.distanceTo(target) < 5 && hero.isReady("cleave")) {
        hero.cleave(target); 

else it would be false for one of 2 reasons, either the unit isn’t within 5 units of me or cleave is on cool down. Which makes the statement false because both need to be true for the statement to be true so it will run this code

    } else {
        hero.attack(target);
    }

which will make my character to basic attack all enemies while waiting for cleave to come back off cool down resulting in the cleave to happen again.

3 Likes

This is very helpful discussion.

2 Likes

Please do not revive dead threads unless you have a related issue, etc. :slight_smile:

2 Likes

Every tried using the :heart: button?

2 Likes

I see a couple likes on some of these posts from him :slight_smile:

2 Likes