1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GML target enemy with highest variable? [SOLVED]

Discussion in 'Programming' started by corwin22, Mar 17, 2019.

  1. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    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
        }
    }
    }
     
  2. TinyGamesLab

    TinyGamesLab Member

    Joined:
    Jun 21, 2018
    Posts:
    147
    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;
     }
    }
    
     
  3. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    wait which if statement are you talking about?
     
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,320
    That chunk of code replaces the one line that reads:
    Code:
    target = instance_nearest(x,y,obj_enemy)
    
    Which lies just under your outermost if statement.

    You should instantly recognize where TinyGamesLab's code belongs if you've read my article on with block patterns.
     
  5. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    oh thanks and what do you mean by
     
  6. corwin22

    corwin22 Member

    Joined:
    Nov 18, 2017
    Posts:
    73
    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice