Stuck on mounten mernchnts


#1

my code isnt working

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    items = hero.findItems()
    itemIndex = 0
    hero.say("I need coins!")
    item = items[itemIndex]
    hero.move(itemPos)
    # 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")
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        # Loop over all your soldiers and order them to attack.
        
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        
        # Use the 'attack' command to make your soldiers attack.
        #hero.command(soldier, "attack", enemy)
        hero.command(soldier, "attack", target)

line 10 is the one that it sase has an error


#2

I see what’s going on. The hero is not considering whether or not an item exists, so the statement

hero.move(item.pos)

returns a null value.
Try including:

if item:

And your first ten or so lines of code will work.

I also do not see any looping over soldierIndex. Try placing a for-loop before defining

soldier = soldiers[soldierIndex]

and also incrementing soldierIndex after you command your soldiers.
Then your code will be able to run with no errors, and you’ll beat the level.

Happy to help! :smiley:


#3

it still isnt working it sase that line 12 move’s argument pos should have been type object but got null target an {x:number, y:number} position


#4

this is my new 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.
    items = hero.findItems()
    itemIndex = 0
    hero.say("I need coins!")
    item = items[itemIndex]
    if item < hero.costOf("soldier"):
        
        hero.move(item.Pos)
        item +=1
    # 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")
    enemy = hero.findNearest(hero.findEnemies())
    soldier = soldiers[soldierIndex]
    if enemy:
        # Loop over all your soldiers and order them to attack.
        
        soldiers = hero.findFriends()
        soldierIndex = 0
        
        
        # Use the 'attack' command to make your soldiers attack.
        #hero.command(soldier, "attack", enemy)
        hero.command(soldier, "attack", target)
        soldierIndex += 1

#5

Were those supposed to be there? You think you could send a screenshot and what is it saying error wise?


#6

it sase i need to loop over my soldiers but i can’t remember how


#7

never mind i got it! thanks thoe