Python, Sarven Siege 7, appending array help


#1

First this level is madness and I love it lol.

Second, if any of you code geniuses can help me implement, what I myself have failed to do for the last six hours, I’d greatly appreciate it.

*Idea - have two peasants collecting coins for me, the first on the top half of the screen, the second on the bottom half. I have all of my summonables coded with concepts I learned in the mountain level. I have them loop over the three towers and defend, I have a types array like[“soldier”,“archer”,“archer”, “etc”] and that works perfect. Its the code below, that Ive tried a dozen different ways, that I cant seem to get working.

def peasantCommand():
    peasantArray = []
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
                peasantArray.append(friend)
                
        if friend == peasantArray[0]:
            #do this
        
        if friend == peasantArray[1]:
            #do that
            
        # How do I make this concept work, the way I want it, without an infinite loop or an error? 
            

#2

I understand that you want to guide the peasants to collect coins for you. This isn’t exactly like the code that you are using, but I presume that it works the same way. Just find the peasants and command thus:

hero.command(peasant[0], "doThis", something)
hero.command(peasant[1], "doThat", something)

#3

Thanks for the fast reply. I really like that structure. The issue is, my code never stops adding to the peasantsArray.

When i first got the error, I bug checked by having the hero.say(len(peasantsArray)) and sure enough he says 0,1,2,3,4,5…

and never stops lol.


#4

Nothing seems to be wrong though. You could just use hero.findByType(type) because it returns a array of all the units with that type. Same as your code but without having to create any new arrays on your own. :wink:


#5

Yesssss. Cant wait to test when I get home. Thanks so much!


#6

Have you completed the desert yet? If you haven’t, you don’t have a Boss Star, which is what enables hero.command; your idea is great, but you’d have to come back after you beat the desert to implement.


#7

I suppose that this means he’s in or has been in the Mountain campaign, which means he does have at least one Boss Star.


#8

Whoops, missed that.