Python Seems to Be Reading Under False 'if' Statements


#1

I use this function in various levels for commanding troops, or at least I try to.

def commandTroops():
    troops = self.findFriends()
    soldiers = self.findByType("soldier", troops)
    archers = self.findByType("archer", troops)
    for troop in troops:
        enemy=troop.findNearest(troop.findEnemies())
        if troop.type='archer':
            self.command(troop, 'attack', enemy)
        if troop.type='soldier':
            if archers:
                archer=troop.findNearest(archers)
                self.command(troop, 'defend', archer.pos)
            else:
                self.command(troop, 'attack', enemy)

The goal of the function is to command archers to attack enemies and command soldiers to defend the archers. For some reason though, this always brings up an error. When I don’t have archers out the game says, “Cannot read property ‘pos’ of null.” It shouldn’t even be trying to read it in the first place because there aren’t any archers so the if statement should ring up false!

I also had a problem with this function on Sarven Siege with the arrow towers. It keeps on telling me that I can’t command arrow towers, but the code specifically refers to archers and soldiers and never tries to command any arrow towers. It seems like the game is trying to read under false if statements.

Any help would be appreciated. Thanks.


#2

Remember, for equality comparison you use two equals signs, not one. Also, I don’t think if archers: doesn’t work. Check if the len of the list archers is greater than 0 instead. And don’t forget to check for an enemy.


#3

OHHH! Facepalm Thank you. I’ve messed that up before