Mountain Mercs--coin collection ice-skating

I don’t quite get the move() method for coin collecting; till now I’ve been using the moveXY(). I’ve tried passing in the variable position, and when that didn’t work I tried using x,y of position, then coin.pos… now he does a slow ice-skating to each coin instead of rapidly collecting coins.

// Gather coins to summon soldiers and have them attack the enemy.

loop {
    
    // Move to the nearest coin.
    // Use move instead of moveXY so you can command constantly.
    while (this.gold < this.costOf("soldier")) {
        var coin = this.findNearest(this.findItems());
        this.say("I need coins!");
        this.move(coin.pos);
    }
    // If you have funds for a soldier, summon one.
    if (this.gold > this.costOf("soldier")) {
        this.say("I should summon something here!");
        this.summon("soldier");
        
    }
    var enemy = this.findNearest(this.findEnemies());
    if (enemy) {
        // Loop over all your soldiers and order them to attack.
        var soldiers = this.findFriends();
        var soldierIndex = 0;
        while (soldierIndex < soldiers.length){
            var soldier = soldiers[soldierIndex];
            soldierIndex++;
            // Use the 'attack' command to make your soldiers attack.
            this.command(soldier, "attack", enemy);
        }
    }
}

I think your problem here is that you are wrapping the coin collection logic inside a while loop, and that your hero is delaying everything by using this.say().

move() basically sets your hero’s target position to { x, y } (or something like coin.pos), but instead of moving all the way to the target position your hero only moves one meter (I think that’s the unit) in that direction. Therefore you can do something else inside of the loop almost immediately, such as summoning/commanding units, instead of waiting until you moved to your target.

By only collecting coins until you have enough gold to build a soldier, you can’t actually command your soldiers to do anything, and the slow “ice skating” effect is due to your hero moving one unit, then saying “I need coins!”, then moving one unit, then saying “I need coins!” again, etc.

@trotod Thanks!

Simply removing the 2 say methods removed the ice-skating around.

Unfortunately, I will need to change up other things as well because my Hero dies long before the timer has run out.

It sounds like you don’t recommend using a while loop for the coin collection?

Yeah, you shouldn’t wrap the coin collection in a while loop like yours, so your hero can command its allies to attack the enemies while your hero collect coins at the same time.

The hero shouldn’t have to wait to collect 20 coins before commanding its soldiers once, who (I think) will forget the command the next time the loop repeats.