Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Library Tactition help appreciated

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

1 Like

so i got the error fixed but now there is another one :frowning: saying, cannot convert undefined or null to object. what does that mean?! @AnSeDra

2 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

2 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”

1 Like

What is your recent code?

Andrei

1 Like

same except i indented all of it labled here

1 Like
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)
1 Like

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

1 Like

Why do you not use the array enemies anywhere even if you declared it?

Andrei

1 Like

can you give me an example?

1 Like

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)
1 Like

Here it is. :grinning:

Andrei

2 Likes

so what do i put there then? (and i dont use self)

1 Like

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