Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Protect and serve amateur trying to learn


I am trying to understand completely what the code is doing here and why I get the error I get just by adding a place to defend…

# Protect the workers and animals!

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

soldiers = []

friends = self.findFriends()
for friend in friends:
    if friend.type == "soldier":
        # Defend the workers:

    # Use a for-loop to assign each soldier to a corresponding defend[] target
    # Use command(soldier, "defend", thang) or command(soldier, "defend", position)
    for i in range(len(defend)):
        soldier = soldiers[i]
        target = defend[i]
        self.command(soldier, "defend", target)

Just using the basic code that they help you with I think that it is basically making a list of places to defend. then creating a list of soldiers, determining with of my friends are soldiers and which are not. If they are soldiers it is appending them to the soldiers list. If it is not it is assuming they are a friend that needs defending and appending them to the targets to defend list.

then it gets a list of all things to defend, both friends and coordinates that were set at the top. I pick soldier 0, assign to target 0, soldier 1, assign to target 1, thru my entire list.

If I just keep the two preset targets and change the coordinates to be near the gold, a soldier will go there but not defend it as I have not identified the gold and appended it to the list of things to defend. Ignoring that though(assuming that is even correct which it may not be), if I just add a new " defend instance and set it as the 2nd item in the list I get an error that hero placeholder needs something to command. Why is that alone causing that? I am learning in the python version of the application.

Thanks in advance.



The error handler isn’t always accurate. Sometimes it shows a completely irrelevant message. Anyways, here’s the problem:

When you append friend to defend, you are actually appending their name, not the unit themselves. Once they are appended, they become meaningless strings. This means that you can not defend them. It’s better to command your soldiers to defend their pos.

Addressing your probe, with defending the coins, I haven’t found a way to use the soldiers to defend the coins, so I used my hero instead.