I am having trouble commanding my soldiers in cloud rip brawl.
My Code:
# Stay alive for one minute.
# If you win, the next time you play will be more difficult and more rewarding!
# If you lose, you must wait a day before submitting again.
while True:
enemy = hero.findNearestEnemy()
item = hero.findNearestItem()
soldier = hero.findFriends()
flag = hero.findFlag("green")
if enemy:
if hero.canCast("chain-lightning", enemy):
hero.cast("chain-lightning", enemy)
else:
hero.attack(enemy)
elif item:
hero.move(item.pos)
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
if soldier:
hero.command(soldier, "attack", enemy)
Yeah, like @phoenixRider2022 alluded to, hero.findFriends() returns an array of units. Either you could do hero.findNearest(hero.findFriends(), hero.findFriends[0], or something like this:
soldiers = hero.findFriends()
for i in range(len(soldiers)):
soldier = soldiers[i]
hero.command(soldier, "attack", soldier.findNearestEnemy()
# Any other hero.command() code here.
OR
for soldier in hero.findFriends():
hero.command(soldier, "attack", soldier.findNearestEnemy()
# Any other hero.command() code here.
The latter for-loop is normally the easiest, but sometimes the former can be useful, too. A for-loop loops through an array of objects, and allows you to function on a single unit in the array of units before continuing to the next unit. Using a for-loop is almost always the best idea for commanding soldiers.
If you used hero.findNearest(hero.findFriends()), as described earlier, it would work, but it would only command the nearest soldier. If you used hero.findFriends[0], it would command the first soldier in the array. I’m not sure how that is determined. I think the first soldier in the array is going to be the first one spawned, but it might just be random.
# Stay alive for one minute.
# If you win, the next time you play will be more difficult and more rewarding!
# If you lose, you must wait a day before submitting again.
while True:
enemy = hero.findNearestEnemy()
item = hero.findNearestItem()
soldiers = hero.findFriends() # add an 's', make this an array
flag = hero.findFlag("green")
if enemy:
if hero.canCast("chain-lightning", enemy):
hero.cast("chain-lightning", enemy)
else:
hero.attack(enemy)
elif item:
hero.move(item.pos)
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
for soldier in soldiers: # command each soldier in 'soldiers' array...
soldierEnemy = soldier.findNearestEnemy() # make a enemy
if soldierEnemy:
hero.command(soldier, "attack", soldierEnemy) #attack the enemy!
The reason you need to use a for-loop is because you need to select a single object to command, you cannot command ALL soldiers at once, even if the command method is really fast.
If you still have trouble about for-loop structures, you can check out this tutorial which covers 3 types of for-loops: Guide