This.distanceTo(anything) glitch

Hey i’m back. I was having a lot of trouble on the levels so i went back to the basics to get better. Any way i have been playing the new campaign(btw finally) and i noticed in one level the code:

loop {
    var enemy = this.findNearestEnemy();
    var distance = this.distanceTo(enemy);
    if (distance < 10) {
        this.attack(enemy);
        
    }
else {
    this.moveXY(40, 37);

but now in later levels it doesn’t work it says target is not specified, null or something along those lines and i dont know why. in the codex or whatever that book is called it has this code

else if (this.isReady("cleave") && this.distanceTo(enemy) < 10) {
    this.cleave(enemy);
}

and i doesn’t work no matter what i do like make a distance variable. i dont understand also recently the var item = this.findNearestItem(); doesn’t work anymore but it was working fine a day ago either im doing something wrong or the code in the campaign is messed up

You probably got upgraded glasses and no longer have access to the findNearestEnemy function.

You’d need to convert that to

this.findNearest(this.findEnemies());

with:

var enemy = this.findNearestEnemy();

enemy exist only if there is an enemy in view range.
If there is not, you can’t get the distance to it.
You should use something like

var enemy = this.findNearestEnemy();
if (enemy)
{
    var distance = this.distanceTo(enemy);
    //do something
}

To complete Sotonin answer, you can use also this.findItems()

1 Like