Commanding Soldiers

Hi, I’m new and I was hoping someone could help me explain how to command soldiers in general? I can summon them fine but I have trouble getting them to move or attack or do anything other than stand still and die. -_- Thanks!

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

The only way I manage to command them is to summon them then do like soldier = hero.findByType(“soldier”) then for i in range(len(soldier)) then define soldier again then command soldier[i]

So, hypothetically, this should work?

while True:
     soldier = hero.findByType("soldier")
     enemy = hero.findNearestEnemy
     for i in range(len(soldier))
     hero.command(soldier[i], "attack", enemy)
1 Like

You can just do

friends = hero.findFriends()
for friend in friends:
    if friend.type == whatever:
        hero.command(friend, "attack", someone)

I tried doing this but my hero just kept doing the building animation and my soldier still didn’t do anything. What do I need to fix?

while True:
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.gold>=20:
            hero.summon("soldier")
            for friend in friends:
                if friend.type == "soldier":
                    hero.command(friend, "attack", enemy)
1 Like

as for this, no. cuz you need a “:” after the (len(soldier)). and the stuff below should be indented.

1 Like

more like this.

while True:
     soldier = hero.findByType("soldier")
     enemy = hero.findNearestEnemy
     for i in range(len(soldier)):
     soldier = hero.findByeType("soldier")
     enemy = hero.findNearestEnemy()
     hero.command(soldier[i], "attack", enemy)

@GregW i think you should do if

hero.gold>=hero.costOf("soldier"):

friends = hero.findFriends()
    for friend in friends:
        enemies = hero.findEnemies()
        enemy = friend.findNearest(enemies)
        If enemy:
            Hero.command(friend, “attack”, enemy)

This will make ur friends attack the nearest enemy

And it won’t attack 1 apenemy at a time. Your soldiers will attack different targets

Question: Is the “:” supposed to be there? Because I’m getting an error from it.

No. It’s supposed to be hero.findFriends()

It was a typo. The : at the end of the hero.findFriends() was a typo

It’s hero.findFriends()

Okay, sorry. Just checking.

It’s fine. Happy to help.

Sorry to still be a bother, but
while True: friends = hero.findFriends() if hero.gold>=hero.costOf("soldier"): hero.summon("soldier") for friend in friends: enemies = hero.findEnemies() enemy = friend.findNearest(enemies) if enemy: hero.command(friend, "attack", enemy) stubbornly refuses to work. What did I do now?

Please format your code.

try hovering your mouse over the >= part. maybe it supposed to be =>? idk rlly. also, i was just wondering. does friend need to be defined?