# Is there anyway to make the archers follow the soldiers?

#1

So they can always stay at a safe distance

#2

You could loop over the archers, then tell them to move to a point 20 meters left of the nearest soldier, I suppose. Aternatively, you could command all archers to defend a soldier, but that doesn’t work too well.

#3

It works alright, but it is more complicated.
You’ll want to loop through the soldiers and the archers and chose a method of assigning each archer to a different soldier so they’re not all bunching up.

I’m sure some adapted Library Tactician code would work too.

#4

If you know what directioin the enemies are coming from you can campare the x or y values to decide how the archers should move.

So if the enemies are coming from the right:

``````
soldiers = self.findByType('soldier')
for archer in archers:
soldier = archer.findNearest(soldiers)
if soldier.pos.x - 5 > archer.pos.x:
#move the archer right or attack
else:
#move the archer left

``````

Granted if they are at the 5 range they are probaly going to dance and if your soldiers aren’t in a line, your archers are going to do a bunch of weird stuff…

#6

You can create, for each soldier.id an old_pos value
Then if the distance between old_pos and solder.pos is greater than a set amount, old_pos=soldier.pos

1. If the soldiier does not move a lot (close quarters fight) the old_pos does not change
2. If a new attack come from a different direction, the solider and the archer will change places keeping the archer out of harm’s way.

Basically, since the soldier is running TOWARDS enemies, the old pos is the place where there are NO ENEMIES

#7

Take care Python and JavaScript copy objects (anything that is not a simple value) as references.

Run this simple code and discover the trouble you can get into:

``````self.say(self.pos)
self.moveXY(self.pos.x+10,self.pos.y)
old_pos=self.pos
self.say(old_pos)
self.say(old_pos)
# Yeep, all 3 values are different!
``````

#8

This looks like the best solution but how do you define the archers? I don’t have that self.findbytype function, where did you get that?

#9
``````for(i=0;i<friends.length;i++)
if(friends[i].type=="archer")
this.say("hello archer "+ friends[i].id + " at pos " + friends[i].pos);
``````

#10

find by type is on better glasses. You should be able to afford those by now.

in python you can also use:

``````archers = [friend for friend in self.findFriends() if friend.type is 'archer']

``````