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

Bullet not always destroying enemy

Discussion in 'Programming' started by Wolfrick, Jan 8, 2019.

  1. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    So I've been trying to destroy my bullet after it hits an enemy (the enemy should disappear and a short death animation will play). However, sometimes the bullet hits the enemy and the death effect will play but the enemy is still there and it can take multiple shots before the enemy disappears even though after one hit it should die. Out of 5 enemies at once this'll happen to 1 or 2 out of the 5 as the rest will get destroyed on the first hit.

    obj_leftBullet_shooter [event collision w/ obj_water]

    Code:
    ///Destroy Enemy
    
    with(other){
        instance_change(obj_red_particles, true);
    }
    
    instance_destroy();
    
    obj_water [event step]

    (This is the only code in my obj_water)

    Code:
    ///Follow Player
    
    //Call Script
    scr_enemy_follow()
    

    *Just to be clear my enemy is called obj_water because it's a glass of water that chases you*
     
  2. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    Are you sure there aren't multiple instances of obj_water stacked on top of each other (meaning that the bullet will hit and kill one, but the others will 'survive' in the exact same position and it will appear as though the original enemy survived)? Is the instance_change to obj_red_particles what you mean by the death animation or is there an actual animation by obj_water BEFORE it gets changed to obj_red_particles?
     
  3. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    The red particles are the death animation (think of it like turning to dust)
    The game takes place in a room where up to 5 enemies spawn in every 5 seconds so there's multiple obj_water in the room at once. If that's the issue how would I fix it?
    You can check my twitter for a video of the issue https://twitter.com/AlexAbbottYT/status/1082486874021740549
    (video might look more clear on the mobile app)
     
  4. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    The issue is not necessarily that there are multiple instances in the room, simply if the instances STACK on top of each other (it's a common problem with most "follow someone" code, that the following instances all end up in the same position and look like one instance). In that video, how many obj_water instances are you spawning? Is it just the one like it appears, or are you spawning more than one?
     
  5. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    I'd draw the instance_number(obj_water) somewhere on the screen to see if the number changes as you shoot even while it appears as though the enemy isn't dying.
     
  6. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    They keep respawning so either just one can spawn or up to 5 can spawn at once. I set up a controller I pace in the room:

    cont_enemySpawn [event create]

    Code:
    //Variables
    
    //point index is the spawn position (think of it like an object placed in the room)
    point_index = 0
    spawn_positions = 3; // number of spawn positions (0 is a position)
    //we set up a little 2d array
    //spawn point 1 (Right)
    spawn_point[0,0] = 306;                         // x position if first spawn point
    spawn_point[0,1] = 135;                         // y position of first spawn point
    //spawn point 2 (Down)
    spawn_point[1,0] = 175;                            // x position if second spawn point
    spawn_point[1,1] = 234;                         // y position of second spawn point
    //spawn point 3 (Left)
    spawn_point[2,0] = 13;                         // x position if first spawn point
    spawn_point[2,1] = 136;                         // y position of first spawn point
    //spawn point 4 (Up)
    spawn_point[3,0] = 175;                            // x position if second spawn point
    spawn_point[3,1] = 13;                         // y position of second spawn point
    
    //Start Spawning
    alarm[0] = 5;
    
    cont_enemySpawn [event alarm 0]

    Code:
    ///Alarm
    
    //Spawn more every 5 seconds
    alarm[0] = room_speed * 5;
    
    //Spawn Multiple at once
    repeat(5) {
        if instance_number (obj_water) < 20 {
            point_index = irandom(spawn_positions); //choose a random spawn point each time you spawn an enemy
            instance_create_layer(spawn_point[point_index,0],spawn_point[point_index,1],"Players",obj_water);
        }
    }
    

    *Just saw what you wrote with the number check i'll try it out*

     
  7. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    Ok, I would say that you are indeed spawning them on top of each other. A way to fix it would be to put a little variation in the spawn points:
    Code:
    //Spawn Multiple at once
    repeat(5) {
       if instance_number (obj_water) < 20 {
           point_index = irandom(spawn_positions); //choose a random spawn point each time you spawn an enemy
           var xx = spawn_point[point_index,0];
           var yy = spawn_point[point_index,1];
           if (instance_position(xx,yy,obj_water)) {
               xx += 50;
           }
           instance_create_layer(xx,yy,"Players",obj_water);
       }
    }
    That will move the spawning point 50 pixels to the right if there's already an enemy there. This is just a quick fix to show you what the problem probably is, you'll have to adjust it depending on how you want the game to function (obviously, you might not necessarily want them to simply spawn more to the right if they are overlapping).
     
  8. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    Video:
    https://twitter.com/AlexAbbottYT/status/1082492960296587264

    I added the counter and when theyre was only one obj_water it counted down 5 times. I'll try out what you wrote.
     
  9. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    Think of it this way. You are setting up 4 possible spawn points, and spawning 5 possible enemies. If you DO spawn 5, you are guaranteed to have at least one enemy spawn on top of another (because there aren't enough spawn points for the amount of enemies). You could increase the number of spawn points BUT there's still a potential problem with that, in that you are randomly choosing spawn points, so the random choice could still be the same spawn point for two enemies. The only real way to make sure they're not colliding is to check for an enemy already existing in the spawn point (as my code does above) and then either selecting a new spawn point until one is found that does not have an enemy on it (this will require 6 spawn points and a while loop that runs until an empty spawn point is found) OR move the newly spawned enemy slightly, as my code does.
     
  10. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    278
    randomize their movement speed and if there are multiple waters overlapping, all will be revealed.
     
  11. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    Video for reference:
    https://twitter.com/AlexAbbottYT/status/1082494517566152705

    I did try what you put above but it didn't fix the issue, although it is spawning 5 a lot more than it did before.
     
  12. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    Ah, yeah, that's because I didn't put a loop (it only checks the first position, so if an enemy has already checked there and been moved to the side, the next enemy checking will not check to see it doesn't check the NEW position if an enemy has already been put there), this should fix it:
    Code:
    //Spawn Multiple at once
    repeat(5) {
       if instance_number (obj_water) < 20 {
          point_index = irandom(spawn_positions); //choose a random spawn point each time you spawn an enemy
          var xx = spawn_point[point_index,0];
          var yy = spawn_point[point_index,1];
          while (instance_position(xx,yy,obj_water)) {
              xx += 50;
          }
          instance_create_layer(xx,yy,"Players",obj_water);
       }
    }
    
    But note, this is only one way to fix the problem. It's more important to conceptually grasp what is happening so that you can adjust it to your liking.
     
    Wolfrick likes this.
  13. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    Video:
    https://twitter.com/AlexAbbottYT/status/1082501758029574145

    It's now working I believe? Except it's always spawning 4 in the room and I'm not sure why? It also happens to spawn in a box a lot which I think I know how to fix that.

    fyi set it to a max of 4 rn

    Code:
    ///Alarm
    
    //Spawn more every 5 seconds
    alarm[0] = room_speed * 5;
    
    //Spawn Multiple at once
    repeat(4) {
       if instance_number (obj_water) < 20 {
          point_index = irandom(spawn_positions); //choose a random spawn point each time you spawn an enemy
          var xx = spawn_point[point_index,0];
          var yy = spawn_point[point_index,1];
          while (instance_position(xx,yy,obj_water)) {
              xx += 50;
          }
          instance_create_layer(xx,yy,"Players",obj_water);
       }
    }
    
     
  14. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,219
    Code:
    repeat(irandom(5)) {
    And the reason it sometimes spawns in a box is because it'll just push more and more to the right if it keeps finding instances in the position it wants to spawn. It doesn't care if there's a box there or not. That's why I said it's more important to conceptually understand the problem and the fix rather than just having this code in the game. Because you might want it to behave differently. The important thing is to check whether an instance exists at where it's trying to spawn and to do SOMETHING if there is. That something is up to you.
     
    Wolfrick likes this.
  15. Wolfrick

    Wolfrick Member

    Joined:
    Jan 7, 2019
    Posts:
    10
    Thx it finally works!
     
    RefresherTowel likes this.

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