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

Is there anyway to make the archers follow the soldiers?


So they can always stay at a safe distance


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.


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.


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
     #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…


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

The archer will try to follow old pos. Advantage:

  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


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:

# Yeep, all 3 values are different!


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?

        this.say("hello archer "+ friends[i].id + " at pos " + friends[i].pos);


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']