just as a tip, i dont know whether its important or not. u can save ressources if u first ask “if enemy” and then check the difference, because its the most important that an enemy is generally there…
I found this on another thread but let’s take a look at it.
loop {
var enemy = this.findNearestEnemy();
if(enemy) {
var distance = this.distanceTo(enemy);
if (distance < 5) {
if (this.isReady("cleave")) {
this.cleave(enemy);
}
else {
this.attack(enemy);
}
}
}
}
So first we need to establish that we are looking for an enemy. So we set the variable and then set an if statement to look for an enemy.
Now that we’ve found our enemies we need to check the distance so that we cleave at the right time. But we need a distance variable to do that. The reason we can’t create the variable beforehand is because there are no enemies. So now that the if statement looking for enemies is fulfilled we can go ahead and start tracking their distance.
Once the distance between enemies is under 5 we are almost ready to cleave. But we don’t want to continuously try to trigger an ability with a cooldown. So we need to check to see it cleave is ready.
All the conditions are met and we then cleave.
Now we set the else statement.
Now for closing the braces. The end brace before the else statement closes the if (this.isReady) clause. The one right after the else clause closes the else clause. The one after that closes the if (distance) clause. After that the if (enemy) clause. And finally the loop.