**# 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():
enemy = hero.findNearestEnemy()
soldiers = hero.findByType("soldier")
for soldier in soldiers:
hero.command(soldier, "attack", enemy)
**# Define the function: pickUpNearestCoin**
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
peasant = hero.findByType("peasant")[0]
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
My hero will summon soldiers, but the soldiers just protect me and Hector dies.
Can’t figure out what is wrong
I am JavaScript but try telling your soldiers to find the enemy, not your hero.
enemy = friend.findNearestEnemy()
or
enemy = friend.findNearest(friend.findEnemies())
Also maybe add an “if enemy” in there
Also quick question, what is the purpose of this line:
peasant = hero.findByType(“peasant”)[0]
?Are you identifying Hector for some reason?
Sorry last question, I noticed you have a line separating your definition of “summonSoldiers()” but no line separating “commandSoldiers()” and “pickUpNearestCoin”. I am not familiar with Python yet but could that be a contributing factor?
Also for future reference it is good practice to post your code with the </> button
ie)
# 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():
enemy = hero.findNearestEnemy()
soldiers = hero.findByType(“soldier”)
for soldier in soldiers:
hero.command(soldier, “attack”, enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)
peasant = hero.findByType(“peasant”)[0]
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
What kind of strange double quotes are you using? Your browser is maybe not set to English as default language. If properly indented your code must work if your double quotes are right and you
hero.moveXY(item.pos.x, item.pos.y) # comment this line and replace it with
hero.move(item.pos)
#/ use the **</>** symbol above to format properly /
You must also replace boots if they don’t support move - moveXY has a hidden loop inside it and can add troubles when you command troops. In your case I think that they follow you hero as pets until you all die.
#cmmnd can be also defend ( attack needs enemy)
#and pos can be peasant.pos or hero.pos or soldier.pos
#Try all possibilities and see what works best
hero.command(soldier, cmmnd, pos)
Thanks to you all, but I still have a problem. This is my code again
# 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():
friend = hero.findFriends()
enemy = friend.findNearest(friend.findEnemies())
soldiers = hero.findByType("soldier")
for soldier in soldiers:
hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.move(item.pos)
this = hero.findByType("peasant")[0]
if this:
hero.command(friend, "defend", this)
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
# 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():
friend = hero.findFriends()
enemy = hero.findNearestEnemy()
soldiers = hero.findByType("soldier")
for soldier in soldiers:
hero.command(soldier, "attack", enemy)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.move(item.pos)
if hero.findByType("peasant"):
hero.command(hero.findFriends, "defend", hero.findByType("peasant"))
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
Now I get an error message: Command's argument minion should have type unit, but got function. Hero Placeholder needs something to command.
# 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():
friend = hero.findFriends()
enemy = hero.findNearestEnemy()
soldiers = hero.findByType("soldier")
for soldier in soldiers:
hero.command(soldier, "attack", enemy)
peasant = hero.findByType("peasant")
if peasant and soldier:
hero.command(soldier, "defend", peasant)
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.move(item.pos)
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
It says defend takes no arguments. As in the part in commandSoldiers.
# 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():
friend = hero.findFriends()
enemy = hero.findNearestEnemy()
soldiers = hero.findByType("soldier")
peasant = hero.findByType("peasant")
x = peasant.pos.x
y = peasant.pos.y
for soldier in soldiers:
hero.command(soldier, "defend", {x, y})
# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
item = hero.findNearestItem()
if item:
hero.move(item.pos)
while True:
summonSoldiers()
commandSoldiers()
pickUpNearestCoin()
Please don’t post solutions on the board, thanks 123hi123!
It took me a little bit because I don’t know python but I got it as well lol (not in time to help you though!) I didn’t try defend because I am not familiar with the language but I am happy you got it!
If you want to post spoilers people usually put them between a
Here you should select only an element of the array that findByType gives you, not the whole array (maybe something like hero.findByType(“peasant”)[0]?)
Here you should command your soldiers to attack their nearest enemy instead of defending the peasant’s position.