here is my current code
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
self.command(soldier, "defend", defendPos);
def findStrongestTarget():
mostHealth = 0
bestTarget = findStrongestTarget()
enemies = self.findEnemies()
enemyIndex = 0
for enemy in enemies:
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)
archerTarget = None
while True:
if hero.gold > hero.costOf("soldier"):
self.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = self.findFriends()
soldiers = self.findByType("soldier")
archers = self.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
my problem is that there is a error saying try hero.findEnemies() on this line
for enemy in enemies:
with the second enemies glowing red
2 Likes
so i got the error fixed but now there is another one saying, cannot convert undefined or null to object. what does that mean?! @AnSeDra
3 Likes
Can you show me your code your new code? That error means that you can not command or attack (that requires objects) on something that does not exist (anymore).
Andrei
2 Likes
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
self.command(soldier, "defend", defendPos);
def findStrongestTarget():
mostHealth = 0
bestTarget = findStrongestTarget()
enemies = self.findEnemies()
enemyIndex = 0
for enemy in enemy:
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)
archerTarget = None
while True:
if hero.gold > hero.costOf("soldier"):
self.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = self.findFriends()
soldiers = self.findByType("soldier")
archers = self.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
the only thing i changed was enemies to enemy
3 Likes
Put this inside the findStrongestTarget() function. (by adding 4 empty spaces before those lines)
Andrei
1 Like
“code never finished its either really slow or never finished or has an infinite loop”
2 Likes
What is your recent code?
Andrei
1 Like
same except i indented all of it labled here
2 Likes
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
self.command(soldier, "defend", defendPos);
def findStrongestTarget():
mostHealth = 0
bestTarget = findStrongestTarget()
enemies = self.findEnemies()
enemyIndex = 0
for enemy in enemy:
if enemy.health > mostHealth:
bestTarget = enemy
mostHealth = enemy.health
bestTarget = enemy
if bestTarget and bestTarget.health > 15:
return bestTarget
else:
return None
def commandArcher(archer):
nearest = archer.findNearestEnemy()
if archerTarget:
hero.command(archer, "attack", archerTarget)
elif nearest:
hero.command(archer, "attack", nearest)
archerTarget = None
while True:
if hero.gold > hero.costOf("soldier"):
self.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = self.findFriends()
soldiers = self.findByType("soldier")
archers = self.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
2 Likes
098765432123:
for enemy in enemy:
Check your spelling of the array.
Andrei
1 Like
if you mean for me to change one of the words it will give an error
2 Likes
Why do you not use the array enemies anywhere even if you declared it?
Andrei
1 Like
can you give me an example?
2 Likes
Where do you wanted to use the array enemies from the function that you created?
Andrei
1 Like
in here somewhere?
while True:
if hero.gold > hero.costOf("soldier"):
self.summon("soldier")
if not archerTarget or archerTarget.health <= 0:
archerTarget = findStrongestTarget()
friends = self.findFriends()
soldiers = self.findByType("soldier")
archers = self.findByType("archers")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
for i in range(len(archers)):
archer = archers[i]
commandArcher(archer)
2 Likes
so what do i put there then? (and i dont use self)
2 Likes
It seems that you did not create the code yourself and you copied an old code. (the self have not been used since 2017) If you want any more help, just create yourself a new code and I will gladly help you with it.
Andrei
2 Likes
Hin_Lee
February 6, 2021, 8:19pm
20
Maybe you should use bestTarget = None instead of bestTarget = findStrongestTarget()?
Welcome @Hin_Lee . I think that 098765432123 has already solved this so please do not revive dead topics.