1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 How do I damage an instance only once? (id/instances)

Discussion in 'Programming' started by Dr_Nomz, Dec 4, 2019.

  1. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    619
    I have a sword swing that I want to damage all enemies that get caught in it's swing, but I want them to ONLY take damage from it once, instead of getting damaged for every frame of it that touches them.

    Code:
    ///CREATE
    attack = 1;
    ///STEP
    var collision_enemy = instance_place(x,y,par_Enemy);
    if collision_enemy && attack == 1{
      with collision_enemy{
        instance_create(collision_enemy.x,collision_enemy.y,obj_Hit_Detector_2);
        scr_Enemy_Damage(obj_Sword_Swing_1);
      }
      attack = 0;
    }
    This is the current setup I have, makes it so you can only hit one enemy per swing, which I don't want.

    How it works is it checks if the sprite is colliding with the enemy, and if it is it creates a hit indicator and passes the damage script to the enemy, then stopping the attack from causing more damage per frame by setting attack to 0.

    How do I get the instance ID of the enemies it touches in the current swing so it doesn't damage a single enemy more than once while it exists? (Basically so I can hit 50 enemies in one hit, but they all only get hit once each, in this case for 30 damage, and only 30 damage)
     
  2. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    So, firstly you want to have a list, you can either create and destroy one each swing or have one created from the get go and just clear it after each swing. Once you have your list, every time you hit an enemy, loop through the list and check to see if that enemies ID is in the list. If it is, ignore it. If it's not, apply the damage and add the ID to the list. This way, each enemy can only get hit once, then it goes on the list and if it gets hit again, nothing will happen because you only do stuff if the instance isn't in the list.

    Just make sure to destroy/clear (depending on whether the list is created each swing or "always" exists) the list after each attack.
     
    DaMuffin, TheSly and Dr_Nomz like this.
  3. Laura Bravo

    Laura Bravo Member

    Joined:
    Jun 25, 2016
    Posts:
    31
    Or just put this in enemy code.
    [create] hit_timer = 0;
    [step] if (hit_timer > 0) { hit_timer -=1;}
    [on hit] if( hit_timer = 0) {my_hp -= dmg; hit_timer = delay amount (say 30 a full second); }
    now enemies can only be damaged once a swing, but if you drop fire or something can still burn for damage each second.
     
  4. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    619
    Question, what's the strain on performance/whatever on the game depending on how I handle the lists? Is it better or worse to use a fixed list that gets cleared every swing over just creating and deleting a list every swing?
     
  5. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,220
    The impact on performance with a list is dependent on how many enemies you hit at the same time (during the lifetime of a single swing). In this scenario (short range and short lifetime of attack) performance will not be a problem. This changes if you have projectiles as attacks that may pierce enemies and fly on through the whole map. But even then you shouldn't worry about performance unless you have walls of enemies like in they are billions. Then maps are starting to get more performant. Ds maps do not slow down depending on how many entries they have (unlike lists) but they read and write slower than lists initially.
     

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