# \[SOLVED\] Define commandSoldier()

**URL:** <https://discourse.codecombat.com/t/solved-define-commandsoldier/25543>\
**Category:** Level Help\
**Created:** [November 19, 2020, 5:58am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543 "2020-11-19T05:58:48Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [November 19, 2020, 5:58am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/1 "2020-11-19T05:58:48Z")

</div>

can someone help me on how to define commandSoldier  
(this is not for a specific level)  
my code is

```python
def commandSoldier(soldier):
    target = hero.findNearestEnemy()
    if (target):
        hero.command(soldier, "attack", target)

```

on line 4 it says command’s argument minion should have type unit, but got null

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [November 19, 2020, 6:15am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/2 "2020-11-19T06:15:40Z")

</div>

You have to define your soldier first. Then you could use it in a for-loop.  
Lydia

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [November 19, 2020, 6:16am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/3 "2020-11-19T06:16:49Z")

</div>

like this? @Lydia_Song

```python
def commandSoldier(soldier):
    soldier = hero.findByType("soldier")
    target = hero.findNearestEnemy()
    if (target):
        hero.command(soldier, "attack", target)

```

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [November 19, 2020, 10:10am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/4 "2020-11-19T10:10:51Z")

</div>

Not quite. Because you’re function uses a single target soldier, you’ll need to also use a for loop somewhere to loop through each of your soldiers.

```python
for friend in friends:
    commandSoldier(fr...

```

> [@ineedhelpwithcoding](#):
>
> `soldier = hero.findByType("soldier")`

This is an array, so you can’t use command with it - you can’t command a whole array in one statement (although that could be a cool feature if you just wanted all your troops to do the same thing…)  
Danny

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [November 23, 2020, 5:57am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/5 "2020-11-23T05:57:51Z")

</div>

> [@Deadpool198](#):
>
> This is an array

so like this?

```python
def commandSoldier(soldier):
    soldier = hero.findNearest(soldiers)
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", enemy)
```

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [November 23, 2020, 7:05am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/6 "2020-11-23T07:05:25Z")

</div>

What @Deadpool198 is saying is that when you define `soldier` as a parameter in your function, you do not need to define it again. In this case, it would be likely that you are using more then one type of friend; it would definitely save time like this, so well done on that. If so, then you shouldn’t define `soldier` and leave the function as it was at the start of this topic.  
Next you need to define a for-loop that iterates through your friends. I’ll leave that to you to think about but in this case

```python
for each friend in your friends:
    if your friend is a ...:
        command...(friend)

```

you would carry on with the required friends.  
Elijah 🦁

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [November 25, 2020, 8:08am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/7 "2020-11-25T08:08:09Z")

</div>

now my code is

```python

def commandSoldier(soldier):
    soldier = hero.findNearest(soldiers)
    enemy = hero.findNearestEnemy()
    for friend in friends:
        if soldier.type == "soldier":
            hero.command(soldier, "attack", enemy)

```

on line 2  
it says soldiers is not defined

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [November 25, 2020, 1:51pm UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/8 "2020-11-25T13:51:58Z")

</div>

Try changing to `friends = hero.findFriends` And the soldier type to `if friend.type == “soldier”`

---

<div class="post-metadata">

**Author:** ![cheddarcheese](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cheddarcheese/32/22417_2.png) [@cheddarcheese](https://discourse.codecombat.com/u/cheddarcheese)\
**Post date:** [November 25, 2020, 2:14pm UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/9 "2020-11-25T14:14:01Z")

</div>

you havent defined “Soldiers” yet

---

<div class="post-metadata">

**Author:** ![Alexbrand](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Alexbrand](https://discourse.codecombat.com/u/Alexbrand)\
**Post date:** [November 25, 2020, 2:18pm UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/10 "2020-11-25T14:18:22Z")

</div>

And I would suggest to change condition a bit. Like  
`if enemy and enemy.health>0:`

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [November 25, 2020, 10:52pm UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/11 "2020-11-25T22:52:21Z")

</div>

```python
def commandSoldier(soldier):
    #find friends
    for friend in friends:
        #if the friend type is soldier
             #find the enemy nearest to the friend or find the enemy nearest to your hero
                   #if there is an enemy
                       #command your soldier to attack the enemy

```

I hope this helps.  
Lydia

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [November 26, 2020, 5:56am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/12 "2020-11-26T05:56:21Z")

</div>

> [@Eric\_Tang](#):
>
> `friends = hero.findFriends`

> [@Deadpool198](#):
>
> This is an array, so you can’t use command with it

which one do I listen to???

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [November 26, 2020, 5:56am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/13 "2020-11-26T05:56:50Z")

</div>

now my code is

```python
def commandSoldier(soldier):
    friends = hero.findFriends()
    friend = hero.findNearest(friends)
    for friend in friends:
        if friend.type == "soldier":
            enemy = hero.findNearestEnemy()
            if enemy and enemy.health < 0: 
                hero.command(friend, "attack", enemy)

```

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [November 26, 2020, 6:02am UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/14 "2020-11-26T06:02:38Z")

</div>

nvm i just messed up  
it was

```python
if enemy and enemy.health<0:

```

not

```python
if enemy and enemy.health>0:

```

thank you Lydia\_Song Alexbrand cheddarcheese Eric\_Tang FalconX11\_312 Deadpool198

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [November 26, 2020, 6:02pm UTC](https://discourse.codecombat.com/t/solved-define-commandsoldier/25543/15 "2020-11-26T18:02:39Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
