[SOLVED] Hunter and Prey Help

I would define archers as an array to loop over (if that helps)

You are defining coin as an array instead of the nearest item in pickUpCoin()

I would define archers as an array to loop over (if that helps)

im stupid what does that mean

and pickUpCoin() is something they put when you load the lvl

As in use something along the lines of this: friendType = hero.findByType("friendType", hero.findFriends()) to create an array and then use a line to create a for loop or use something like an archerIndex (kinda like the mad maxer levels) to loop over them.

As for pickUpCoin, I’m talking about the code inside the function, not the function itself.

I’m sorry if the first part seemed confusing.

so you want me to put this instead of

    enemy = archer.findNearestEnemy(archer.findEnemies)

(and the first part is a little confusing)

No define who the archer is first is what I’m trying today. sorry

im confused (20characters)

im stupid when it comes to somethin like this

It’s okay. Do you remember in the desert where you used arrays to loop over enemies or friends? I’m asking you to do something similar here.

hang on give me a minute

1 Like

nope im still confused sigh

is there a certain lvl in the desert your talking about?

Yes and no. A few desert levels use arrays. Look at Sarven Savior. (or mad maxer if you subscribed)

In that level, the names were defined. However, here, they are not. So use friendType = hero.findByType(“friendType”, hero.findFriends()) #the friendType is a placeholder to find an array of that kind of friend. Then loop over the array to command all of that kind of friend to do something.

1 Like

good news or bad news which do u want to hear first?

bad then good…

bad news is there is another error here

    if enemy == hero.distanceto(enemy) < 25:

the error is there is no enemy. Use enemy = hero.findNearestEnemy

the good news is that the archer problem is solved

1 Like

current code

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    if coin:
        coin = hero.findItems()
        hero.moveXY(coin.pos.x, coin.pos.y)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold < hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", target)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
    friendType = hero.findByType("friendType", hero.findFriends())
    if enemy == hero.distanceto(enemy) < 25:
        hero.command(archer, "target", enemy)
    else:
        hero.command(archer, "move", archer.pos)
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandarcher(friends)
            pass

Enemy is not defined properly. use the distanceTo line as a nested if inside the if enemy line in commandArcher().