GMS 2 how to count shots or enemy destruction[solved]

Discussion in 'Programming' started by diester, Sep 11, 2019.

Tags:
  1. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    19
    Hello, I'm looking for a tutorial that would allow to count the number of enemy detruis to fill a gauge.
    this gauge would allow me to activate a new function once full (like having the possibility to have a special shot or activate a shield)
    I try to coded from the tutorial healtbar (since I have not found any tutorial on this way to do).

    but I can not do anything. who can help me or give me the link of a tutorial please thank you
     
  2. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    759
    create an obj_enemy_counter
    Code:
    /// create event
    enemy_needed = 10; // how many enemys need to be killed
    killed = 0; // how many have been killed (leave at 0 here)
    width = 200; // width of the bar to draw
    height = 16; // height of the bar to draw
    dp = 0;// delta percent : will be 1 when all you have enough kills
    
    Code:
    /// draw event
    dp = (killed / enemy_needed);
    var dw = dp * width; // the width of the bar to draw
    var dh= height;
    // draw a rectangle
    draw_set_color(c_black);
    draw_rectangle(x, y, x+width; y+height);
    // draw main bar
    draw_set_color(c_red); // color of bar
    draw_rectangle(x, y, x+dx, y+dy);
    
    now when you kill an enemy
    Code:
    /// enemy is ready to die
    obj_enemy_counter.killed++;
    
    and when you need to see if you have enough kills
    Code:
    if(obj_enemy_counter.dp == 1) {
         obj_enemy_counter.killd = 0; // make sure to reset it to show it was consumed
         // .... do your special attack ......
    }
    
     
  3. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    19
    so simple while I go around in circles for days ...... thank you very much I will test this, it shows me that I still have much to learn
     
  4. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    19
    I just have a little problem if I kill more enemy than the number defined
    Code:
    enemy_needed = 20; // how many enemys need to be killed
    it does not work anymore. How can you make sure that once the set number has been reached, the bar will remain locked until the special shot is used?
     
    Last edited: Sep 11, 2019
  5. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    759
    at the top of the draw event for obj_enemy_counter write:
    Code:
    killed = clamp(killed, 0, enemy_needed):
    clamp makes it so the value cant go below 0 and cant go above the max
     
    diester likes this.
  6. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    19
    thank you very much it works perfectly you manage. thanks thanks thanks ;)
     

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