Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Mountain Mercenaries Help - Cloudrip Mountain

Hi, I’m not sure what’s wrong with my code. This code allows my character to summon 1 soldier, but he doesn’t do anything. What’s more, an error pops up. Error: ‘ArgumentError: command’s argument minion should have type unit, but got null
Code:

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

Hi! You need to summon a soldier in a different place from where your doing it at the moment. At the moment your summoning a soldier when you don’t know if you have enough money to summon it. Look at this line:

At the moment your commanding a soldier that you defined before you summoned it. So when you defined the variable soldier, you didn’t have any, meaning it’s contents is null.
You also need to increment (increase by one) soldierIndex at the end of your while loop so that you command more than one soldier.
Danny

@Deadpool198 i told u how to get the level 66 items

Did you? I don’t remember this happening. :smile:
Danny

So your mastake is… wait what? oops, i dont do c ++.

wait wait wait, oof thats javascript

Hmm… I did what you said, but I still got the same results Code:

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

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

Ok, there are still a few errors:
Firstly, you’re not defining soldier inside the while loop as well as outside it. The while loop only loops the code inside it, meaning soldier will always stay the same, if you increment soldierIndex or not.
Next:

Why < 10? Do you know how many soldiers you have? Trying to command a soldier that you defined using a soldierIndex higher than the number of soldiers you actually have will cause an error. (array.length returns the length of an array)
You’ll also have to remove the hero.say()s as they slow down your code significantly.
Danny

Well, I replaced 10 with array.length but it says ‘‘array’ is not defined’.

Does that mean I have to put a var soldier outside the while loop as well??

:man_facepalming:
Don’t mean to be rude but do you think I meant “array” when I said “array”?
What array are you using?

Correct.
Danny

yes… :slightly_frowning_face: Sorry about that, I got very confused. :woman_shrugging: :woman_facepalming:

Is this better?

var soldier = hero.findFriends();
while(true) {
    // Move to the nearest coin.
    // Use move instead of moveXY so you can command constantly.
    var coin = hero.findNearestItem();
    hero.move(coin.pos);
    
    // If you have funds for a soldier, summon one.
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy) {
        
        var soldiers = hero.findFriends();
        var soldierIndex = 0;
        var soldier = soldiers[soldierIndex];
        // Loop over all your soldiers and order them to attack.
        while (soldierIndex < soldiers) {
            hero.command(soldier, "attack", enemy);
            soldierIndex += 1;
        }
            // Use the 'attack' command to make your soldiers attack.
            //hero.command(soldier, "attack", enemy);
    }
}

My character collects all the coins and buys (or hires :smile:) soldiers, but they don’t attack; so I end up getting killed.

Sorry, I meant you need to define soldier inside the soldier index loop. And to define it the same way you define it here:

And this line is still wrong:

(Use soldiers.length)
Danny

Thanks, Danny, it worked like a charm! :smiley:

1 Like

hello deathoool!! 20 CHAR

imean deadpool198…

Can someone help me with my code here i will show it

while True:
    coin = hero.findNearestItem()
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    hero.move(coin.Pos)
    hero.say("I need coins!")
    
    # If you have funds for a soldier, summon one.
    if hero.gold > hero.costOf("soldier"):
        hero.say("I should summon something here!")
        
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        # Loop over all your soldiers and order them to attack.
        if soldier:
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)zz
            hero.command(soldier, "attack", enemy)
            soldierIndex+=1

it says that this is the wrong line