Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Mixed Unit Tactics:)

Hi, So my code is saying that i don’t have an object. If anyone sees this thank you so much!!

# Practice using modulo to loop over an array

# Choose the mix and order of units you want to summon by populating this array:
summonTypes = ["soldier","soldier","soldier"]

def summonTroops():
    # Use % to wrap around the summonTypes array based on len(hero.built)
    solider = summonTypes[len(hero.built) % len(summonTypes)]
if hero.gold > hero.costOf(summonTypes["soldier"]):
    hero.summon(summonTypes[soldier])
    #type = summonTypes[???]
    hero.say("I should summon troops!")

def Item():
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x,item.pos.y)

def Attack():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
while True:
    summonTroops()
    Item()
    Attack()

Thank you so much.

On whitch line do you revive the error?

Why don’t you use the variable that you just made?

summonTypes["soldier"] this pulls an error when you use it in hero.costOf()
I recommend calling this ‘type’ if you’re planning to use other types of troop, which you’ll need to later.
Danny

So instead of this

Put
hero.costOf(soldier):
And try to summon soldiers and archers, too. Maybe try this?

summonTypes = ["soldier","archer"]

You should also watch out for proper indentation…spelling counts too.