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

[SOLVED]Timber Guard Python Help

Here is my code, please help me on this level.
`while True:
# Collect gold.
item = hero.findNearestItem()
if item:
hero.move(item.pos)
# If you have enough gold, summon a soldier.
if hero.gold >= hero.costOf(“soldier”):
hero.summon(“soldier”)
# Use a for-loop to command each soldier.
# For loops have two parts: “for X in Y”
# Y is the array to loop over.
# The loop will run once for each item in Y, with X set to the current item.
for friend in hero.findFriends():
if friend.type == “soldier”:
enemy = friend.findNearestEnemy()
# If there’s an enemy, command her to attack.
# Otherwise, move her to the right side of the map.
soldiers = hero.findFriends()
soldierIndex = 0
if enemy:

            while soldierIndex < len(soldiers):
                soldier = soldiers[soldierIndex]
                hero.command(soldier, "attack", enemy)
                soldierIndex += 1`

Remember to format your code @Blaze_Fire1!

define soldiers, then it should look like this:

for friend in soldiers:
    # whatever below

it should work

you should do findByType instead, peasents can’t attack

if you do findfriends, u will command every friend to attack. if peasents can’t attack, it will be an error

I don’t do python(I don’t know a lot of it) so bye

Ok I am going to try the findByType instead of findfriends.

while True:
    # Collect gold.
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
    # If you have enough gold, summon a soldier.
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    # Use a for-loop to command each soldier.
    # For loops have two parts: "for X in Y"
    # Y is the array to loop over.
    # The loop will run once for each item in Y, with X set to the current item.
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            # If there's an enemy, command her to attack.
            # Otherwise, move her to the right side of the map.
            soldiers = hero.findByType("soldier", hero.findFriends())
            
            soldierIndex = 0
            if enemy:
                
                while soldierIndex < len(soldiers):
                    soldier = soldiers[soldierIndex]
                    hero.command(soldier, "attack", enemy)
                    soldierIndex += 1
            else:
                soldier = soldiers[soldierIndex]
                hero.command(soldier, "move", {73, 47})
                soldierIndex += 1

Did it work?(20 characters)

jeremy.say("Did it work?(20 characters) {0_0}")

Not exactly, the findByType helped a lot, but there is another problem.

ok what problem is that

Wait let me screenshot problem.

OK (lets see if i can help)

Hmm thats weird , try asking a moderator

ok I will try (20 chars)

Or try asking owners(@nick or @differentmatt )

How? (20 characters)