GML target enemy with highest variable? [SOLVED]

C

corwin22

Guest
I am making a simple tower defense game where the turret will target whatever enemy is closest, the issue with that is that it is very specific and I want to make it attack the one in the lead of the line how can I do this?

I can track how far they are with path_position so whatever one has the highest of that variable will be the one targeted. the only question is how can I have it find which one has the highest of that variable?

This is the current targeting system if you need it.
Code:
if (instance_exists(obj_enemy)) {
target = instance_nearest(x,y,obj_enemy)
if (distance_to_object(target) < firerng) {
AimDir = point_direction(x,y,target.x,target.y)+90
    if (canfire = true) {
    canfire = false
    target.hp = target.hp - atk
    tstage = tstage + 1
    alarm[0] = firespd
    alarm[1] = firespd * 0.15
    }
}
}
 
T

TinyGamesLab

Guest
You will need a with loop.

Something like this inside your if statement should work:
Code:
target = noone;
maxposition = 0;
with (obj_enemy) {
 If (path_position>other.maxposition) {
  other.maxposition = path_position;
  other.target = self;
 }
}
 
C

corwin22

Guest
You will need a with loop.

Something like this inside your if statement should work:
Code:
target = noone;
maxposition = 0;
with (obj_enemy) {
 If (path_position>other.maxposition) {
  other.maxposition = path_position;
  other.target = self;
 }
}
wait which if statement are you talking about?
 
C

corwin22

Guest
Oh, wait something wrong happened.
it chooses the target even if they are out of range, how can I make it so that it picks the highest one that is also within range.
EDIT:
nvm i managed to get it working thanks for all the help!
 
Top