[SOLVED] Tactical timing javascript

hello, im at “tactical timing” and my hero doesnt move to flag (i have read the existing thread, it did not help me)
i tried different things but no reaction to flag (btw tried findFlag() empty and also findFlag(“black”))
my code is:


var enemy = hero.findNearestEnemy();
var flag = hero.findFlag();

while(true) {
if (flag) {
    hero.moveXY(flag.pos.x, flag.pos.y);
    hero.pickUpFlag(flag);
} else {
    if (enemy) {
        if (hero.isReady("cleave")) {
            hero.cleave(enemy);
        } else {
            hero.attack(enemy);
        }

(removed few { from bottom, no problems with those)
ty for help <3

You should put those two lines in the while loop and it should work fine. Let me know if you hmore problems, ok?

1 Like

yesss that worked!!
noobish silly q (since i did get all the way here on my own xD):
why didnt it work with the variables outside the loop? ^^"

1 Like

The way variables work is that they retain the value they had when they were defined.

var enemy = hero.findNearestEnemy() // this is a specific enemy
while (true) {
    if (enemy) {
        hero.attack(enemy); //this will always attack the same enemy
        // then when it's gone it won't do anything at all
    }
}

Danny

1 Like

With the variables outside, they are only define one time…like Danny says, they will always be the same single enemy. This method is also used to ‘prime’ (make ready for) another block of code.

With the variables on the inside, they get redefined (refreshed) every time the loop runs.

1 Like