[Solved] Tower Targeting Mode

Discussion in 'Programming' started by jgaryedyen, Aug 12, 2019 at 2:39 AM.

  1. jgaryedyen

    jgaryedyen Member

    Joined:
    Jun 26, 2019
    Posts:
    5
    So I've been making a TD and I've been looking at targeting systems. Atm I've been play testing with it shooting at the nearest enemy but I'd like to improve that. I found a comment that seemed very helpful but I've never used ds lists or anything like that so I am really at a loss how to implement it. The comment is:

    //I just use a ds_priority and a "switch" statement to determine what priority to give each enemy.

    switch(other.target_mode)
    {
    case eTargetMode.first:
    ds_priority_add(other.target_pri,id,1-path_position);
    break;
    case eTargetMode.last:
    ds_priority_add(other.target_pri,id,path_position);
    break;
    case eTargetMode.close:
    ds_priority_add(other.target_pri,id,edis);
    break; case eTargetMode.strong:
    ds_priority_add(other.target_pri,id,-mhp);
    break;
    }

    //eTargetMode is an enumeration (enum). You can look that up in the manual if needed. I just loop through every enemy in range that the tower can hit and run that code^. After looping through all enemies, I run this:

    while(!instance_exists(target) && !ds_priority_empty(target_pri))
    {
    target = ds_priority_delete_min(target_pri);
    }

    I have a lot of questions concerning how to implement this:
    1) What is the "other" this code is referring to, and how would you get it? As in this is not a collision, and I'm not sure how or why you would refer to an other from a with function.
    2) Which objects should store which variables.
    3) With the "case eTargetMode" how does it actually tell which enumeration is the desired one.
    4) How would you suggest looping through the enemies? I'm experimenting with ds_circle_list, but once again have no experience with it.

    If you have a better suggestion of course that would be great too.

    Thanks for your time! This seems like the kind of thing I should be able to get but after hours trying to figure it out I think I'm just too stupid.
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,301
    1: other is referring to the instance running the with block. This is clearly detailed in the Manual entry for with:
    2: Instance variables are properties. If X is a property of Y, then X should be a variable in object Y. Read up on variable scope and use some common sense.

    3: eTargetMode is the enumeration. Read the Manual entry on enumerations.

    4: If the enemies are already in a list, then use a for loop. Otherwise, use a with loop.
    Code:
    var enemy_list_size = ds_list_size(enemy_list);
    for (var i = 0; i < enemy_list_size; i++) {
      var current_enemy = enemy_list[| i];
      /* Operate on current_enemy here */
    }
    Code:
    // This loops through all obj_enemy instances
    with (obj_enemy) {
      /* Operate on the current obj_enemy instance here and refer to the calling instance as other if need be */
    }
    Read the Manual entry on for and with and other control structures. Computer science classes start with these basic syntax elements not because they're something to haze rookies with, it's because they're constantly used.
     
  3. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    595
    I'm assuming you are asking this in regards to the shooting object itself, rather than how the enums work in this code block. Without seeing more of their project it's difficult to say how the towers are deciding which of these four activities they are pursuing.

    You could have different towers / objects that only target certain enemy conditions, or enemy types. That might be one way that they decide which condition to target (what enum value they are setting 'target_mode' to)

    For example: those set to "close" might be a tower type, or object, that has a limited range. Those that are set to "strong" might be the ones that do the most damage. Slower firing ones target the "last", whereas faster firing ones target the "first". Can't really say without seeing all the rest of the code, but those sound like plausible explanations.

    Or it might be that the response of any targeting objects is set by the player, and at any point in time they can be doing one of those four conditions...??
     
  4. jgaryedyen

    jgaryedyen Member

    Joined:
    Jun 26, 2019
    Posts:
    5
    Ok after a while of looking it through and trying to figure it out I don't get exactly what the code is doing. I have an idea but I don't know exactly what it is doing. That's probably why I can't implement it. So if anyone has the time and patience for an amateur could they please explain exactly what the code is doing because there are several things that just seem wrong to me but almost certainly aren't.
     
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    595
    You will have to include more of the code, for people to be able to do that.

    I can easily explain what the above code is doing, but it is far from all of it. It is just a very simple response, while everything it is responding to hasn't been included.

    A quick explanation. Depending on what they are set to:
    1) "First" How much of an objects path remains
    2) "Last" How far into an objects path it is
    3) "Close" Presumably uses point distance to see which is nearest target, or uses distance to find target
    4) "Strong" Goes on a targets health to decide which to go for
     
    Last edited: Aug 13, 2019 at 1:45 PM
  6. jgaryedyen

    jgaryedyen Member

    Joined:
    Jun 26, 2019
    Posts:
    5

    Thats the problem Im having. Im not sure what its responding to so I thought someone more experienced and smarter than me might be able to work backwards a bit and help me out. I suppose I'll just try to work it out myself from the ground up so I can understand it better. Sorry I havent explained better to everyone whos tried to help but unless someone has anything else to add I suppose I'll just call this thread solved.
     
  7. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    595
    With the amount of code you included here - that is as far back as anyone can realistically take it. You said you got this from a community thread: If that didn't have the rest of project then anyone else will basically have to fill in 99% of this for you. With all the best will in the world....that's not going to happen.

    Between my last response, and the one before that, you have:
    1) The (possible) reasoning behind how towers / objects get their targets - either automatically based on type, or through intervention by the player
    2) What it is looking at in terms of target characteristics (path position, distance, hp)

    If you want code for that then users will provide suggestions, but you're going to have program it. They won't do that for you...at least, not without paying them as a programmer, or having them on a team etc
     
  8. jgaryedyen

    jgaryedyen Member

    Joined:
    Jun 26, 2019
    Posts:
    5
    Ok hallelujah I managed to figure it out myself. Thanks for all the help. It didn't directly answer what I wanted but you all helped poke and prod me in the right direction. Ty
     

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