Protect and serve, please help!

# Protect the workers and animals!

# Defend these two positions:
defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }

soldiers = []

friends = hero.findFriends()
for index in range(len(friends)):
    friend = friends[index]
    if friend.type == "soldier":
        soldiers.append(friend)
    else:
        # Defend the workers:
        defend.append(friend)

while True:
    enemy = hero.findNearestEnemy()
    
    # Use a for-loop to assign each soldier to a corresponding defend[] target
    # Use command(soldier, "defend", thang) or command(soldier, "defend", position)
    if friend.type == "soldier":
        hero.command(friend, "attack", enemy)
    if enemy:
        if hero.isReady("throw"):
            hero.throw(enemy)
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
        hero.attack(enemy)
    pass

You should use a for-loop here:

2 Likes

try using a for loop like this:

for i in range(len(whatever ur soldiers array is named)):
1 Like

Or just for unit in array.

2 Likes

using i is a bit more cleaner

1 Like

No xD but we’re not here to argue, just use one of the methods

1 Like

agree = ["I", "agree"]

for i in agree:
    hero.say(i)
1 Like

This is kinda spam…

1 Like

Using i in functions can allow for more robust functions, but I prefer for object of array (or for object in array if you use Python) because it’s cleaner.

1 Like

I fixed my code. I forget how, but I fixed it. Thanks for all of the help.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.