The Two Flowers - Python Help


#1
def summonSoldiers():
if hero.gold >= hero.costOf("soldier"):
    hero.summon("soldier")


def commandSoldiers():
soldiers = hero.findFriends()
for soldier in hero.findFriends():
    enemy = soldier.findNearestEnemy()
    if enemy:
        hero.command(soldier, "attack", enemy)

peasant = hero.findByType("peasant")[0]


while True:
summonSoldiers()
commandSoldiers()
# pickUpNearestCoin()

I keep getting an error that command’s argument minion should have type unit, but got object: hector


#2

I think you need self.findNearestEnemy


#3
# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    soldiers = hero.findByType("soldier")
    for i in soldiers[i]:
        hero.command(soldiers[i], "attack", friend.findNearestEnemy())
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)

while True:
    peasant = hero.findByType("peasant")[0]
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()

Whenever I press run, Tharin has the red X and says that it cannot convert undefined/null to object.


#4

it should be for i in range(0,len(soldiers)-1) or something like this
and it should be soldiers[i].findNearestEnemy()


#5

How they showed me and displayed for loops was for i in soldiers[i]:


#6

ok so change friend.findNearestEnemy() to soldier.findNearestEnemy()


#7

I need help with my code, he summons soldiers and stuff but then he doesn’t do anything else after that,he just stands there, which I expected but he wont command them to attack enemies!

    coin = self.findNearest(self.findItems())
    self.move(coin.pos)
    if hero.time == 10:
        break
loop:
    
    if self.gold > self.costOf("soldier"):
        self.summon("soldier")
        
    enemy = self.findNearest(self.findEnemies()) 
    if enemy:
        # Loop over all your soldiers and order them to attack.
        soldiers = self.findFriends()
        if hero.findFriends() != "hector":
            continue
        else:
            pass
        soldierIndex = 0
        
        while soldierIndex < len(soldiers):
            soldier = soldiers[soldierIndex]
            enemy = soldier.findNearest(enemy)
            self.command(soldier, "attack", enemy)
            soldierIndex = soldierIndex+1

#8

You are asking the soldiers to look in an array, you are not using the right function.

while soldierIndex < len(soldiers):
            soldier = soldiers[soldierIndex]
            enemy = soldier.findNearest(enemy) <----- # this won't work
            self.command(soldier, "attack", enemy)
            soldierIndex = soldierIndex+1

#9

so what do i do with that part then? i did happen to beat the level by now, but in a different way, the wrong way but i beat it, however it would be good to know how to do that.


#10

ill explain in detail

when you say enemy = soldier.findNearest(enemy)
you are asking the soldier to look in the list or array: enemy,
which doesn’t exist

when you say enemy = soldier.findNearestEnemy()
you are asking the soldier to find the nearest enemy.

what is a list?
It is a changeable data containing multiple variables that can be anything.
list = [value1, value2, value3, value4]

You’ll learn about arrays/list in cloudrip mountains campaign