[SOLVED] Define commandSoldier()

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

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

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

like this? @Lydia_Song

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

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.

for friend in friends:
    commandSoldier(fr...

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

3 Likes

so like this?

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

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

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

you would carry on with the required friends.
Elijah :lion:

2 Likes

now my code is


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

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

you havent defined “Soldiers” yet

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

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

which one do I listen to???

now my code is

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)

nvm i just messed up
it was

if enemy and enemy.health<0:

not

if enemy and enemy.health>0:

thank you Lydia_Song Alexbrand cheddarcheese Eric_Tang FalconX11_312 Deadpool198

1 Like

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