# Nobile Sacrifice

The yeti attacks my soldier and then the other soldiers move to the yeti and they get killed

``````points = []
points[0] = {"x": 90, "y": 52}
points[1] = {"x": 51, "y": 53}
points[2] = {"x": 51, "y": 73}
points[3] = {"x": 13, "y": 73}
while self.gold < 80:
item = self.findNearest(self.findItems())
x = item.pos.x
y = item.pos.y
self.moveXY(x, y)
for i in range(4):
self.summon("soldier")
loop:
friends = self.findFriends()
for j in range(len(friends)):
point = points[j]
friend = friends[j]
enemy = friend.findNearestEnemy()
if enemy:
self.shield()
if friend:
self.command(friend, "move", point)
``````

the first soldier is killed then the others move to its point and they die when the yeti attacks

Is there anything like that it doesnâ€™t like? Does it say "fix your codeâ€™ or â€śerror on line 2â€ť or something?

no it doesnt i dont get any fix your code

Everything your hero does takes time. The only things that do not take time are the commands to your soldiers - they can act in parallel with you.

As a result, when you command your hero to do anything, youâ€™re basically telling your soldiers: do whatever you are set to do right now, I am busy and I canâ€™t instruct you.

You code basically does:

``````for j in range(len(friends)):
if (enemy)
self.shield()
``````

Shield takes 0.433 seconds to process so the entire for will take 1.733 seconds to circle through 4 soldiers. During this time each soldier received only a move command with takes 0.033 seconds to be executed. That means for the rest 1.7s their behavior is not specified.

Solution

command all your soldiers first, then command your hero. The soldiers will follow their current instruction while the hero is busy

1 Like

Also, you are searching for your friends on each loop.
Initially, your friends array is [soldier1,soldier2,soldier3,soldier4]
assigned to [point1, point2, â€¦]

If the first soldier gets killed, then a new search for friends will give you [soldier2,solider3,solider4]
to be assigned to [point1,point2,point3,point4]

Result: soldier2 will have to go to point1. Once he is killed also, soldier3 will go to point1, etc

so what should i do for them not to go to the points after one dies

This is my code now my soldiers move to their points first like they did before and my hero sheilds after the yeti is released

``````points = []
points[0] = {"x": 90, "y": 52}
points[1] = {"x": 51, "y": 53}
points[2] = {"x": 51, "y": 73}
points[3] = {"x": 13, "y": 73}
while self.gold < 80:
item = self.findNearest(self.findItems())
x = item.pos.x
y = item.pos.y
self.moveXY(x, y)
for i in range(4):
self.summon("soldier")
loop:
friends = self.findFriends()
for j in range(len(friends)):
point = points[j]
friend = friends[j]
enemy = friend.findNearestEnemy()
if friend:
self.command(friend, "move", point)
if enemy:
self.shield()``````

@Luke10 can u help me with whats wrong

i also tried changing the points[j] and friends[j] to points[i] and friends[i] and only one soldier moved to the point of the last soldier and he got killed by ogres so i changed it back to points[j] and friends[j]

@ChronistGilver please help me ive been stuck for over a week and i cant figure it out im very desperate

never mind i beat with this it says fix your code line 22 but i beat it still

``````points = []
points[0] = {"x": 90, "y": 52}
points[1] = {"x": 51, "y": 53}
points[2] = {"x": 51, "y": 73}
points[3] = {"x": 13, "y": 73}
while self.gold < 80:
item = self.findNearest(self.findItems())
x = item.pos.x
y = item.pos.y
self.moveXY(x, y)
for i in range(4):
self.summon("soldier")
loop:
friends = self.findFriends()
for j in range(len(friends)):
point = points[j]
friend = friends[j]
enemy = friend.findNearestEnemy()
if friend:
self.command(friend, "move", point)
if enemy:
self.command(friend, "move", 19,53)

``````

Glad to hear you have beat it. The solution is to not re-search for the friends in the loop. You are not going to get new soldiers in this level anyway, so what is the point of searching again?

So:

``````loop:
friends = self.findFriends()
``````

becomes:

``````friends = self.findFriends()
loop:
``````

Inside the loop, you can test if a friend is still alive:

``````if friends[j] && friends[j].health > 0:
self.command(friends[j], "move", points[j])
``````

In this way, if soldier1 dies, he will be a dead soldier at j=0, soldier2 it will still be at index j=1 in friends. They do not change places

1 Like