1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Destroy multiple instances on collision

Discussion in 'Programming' started by iBack, Mar 12, 2019.

  1. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    Hi, I am having a hard time figuring out how to destroy multiple instances on collision. At the moment I have a second attack (right_click) object called "water_balloon_test" the size of this sprite and collision mask is full 300 x 300. On collision with enemy it only destroys whatever instance that is in the surrounding area of the collision, however the "water_balloon_test" instance does not get destroyed once on collision.

    What I want is, once "water_balloon_test" collides, multiple instances in that area of collision gets destroyed, along with the "water_balloon_test" instance.
    I am unsure what to use, i'm new to gamer maker studio and currently trying to create a game.
    what would i do? please help.

    this is what i have inside the collision event:

    [COLLISION]
    Code:
    //with statement so this applies to another object other than the one we are using, moreover, other so it applies to individual objects instead of all.
    with (other)
    { 
        if  (instance_nearest (x,y,obj_enemy))
        {
            hp = hp - 5; //since the enemy health is set to 5 hits, the one right_click attack would kill the enemy with one hit
     
        }
        instance_destroy();//currently destroys all instances the object collides with but doesn't get destroyed itself.
    
    }
    
    instance_destroy(); // if i place this here it will destroy the "water_balloon"test" instance  and one enemy instance but not multiple instances in the surrounding area.
    
    
    
     
    Last edited: Mar 12, 2019
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,969
    Make a distinction between objects and instances.
    Code:
    with (other) {
      hp -= 5;
    }
    
     
  3. Dev_M

    Dev_M Member

    Joined:
    Mar 22, 2018
    Posts:
    23
    1. How do you check your collision now?

    2. Do you want the object to move x amount of distance and destroy all instances it passes, or
    do you want for it to destory itself on first collision and also destroy all instances within a radius?
     
  4. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    1. I check using the collision event on "water_balloon_test" object , collision with"obj_enemy"

    2. i want it to destroy itself on first collision and all instances within a radius.
     
  5. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    this goes right through the instances and destroys whatever it hits but does not destroy itself on collision.

    i want it to destroy itself on first collision and all instances within a radius.
     
  6. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,054
    In the collision event, you can use the function collision_circle_list() to retrieve instances of a certain type within a radius.

    Destroy all the objects in the list (or do hp -= 5 if that suits your game), then destroy the water balloon (and destroy the list as well to avoid memory leaks).

    You shouldn't need to use "other" in this case, as it will be picked up by the collision_circle_list() function.

    If you are using GMS 2, the circle function is built-in, if not, you can download the equivalent script from gmlscripts.com : Link : https://www.gmlscripts.com/script/collision_circle_list
     
    Dev_M and Ludo Design like this.
  7. Dev_M

    Dev_M Member

    Joined:
    Mar 22, 2018
    Posts:
    23
    I was gonna recommend same function as IndianaBones just wrote up for you.
     
  8. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    Hi thank you for helping me.

    I was able to test out both collision_circle and collision_circle_list

    while using collision_circle it works as explained, however, when i try destroy the "water_balloon_test" object it doesn't work as it should.

    Code:
    with (other) {
    
    if collision_circle(x,y,30,obj_enemy,false, true)
    
    {
    
    instance_destroy();
    
    
    }
    
    instance_destroy();
    
    }
    
    
    
    I had to use with (other) for the collision_circle(); because without it the instances would not destroy.

    this is when i used the collision_cirlce_list:
    Code:
    var _list = ds_list_create();
    var _num = collision_circle_list(x, y, 100, obj_enemy, false, true, _list, false);
    if _num > 0
        {
        for (var i = 0; i < _num; ++i;)
            {
            instance_destroy(_list[| i]);
            }
        }
    //instance_destroy();//if i place this here the water_balloon_test instance is destroyed but not the enemy instances, without this though it works perfectly except for the instance not being destroyed.
    ds_list_destroy(_list);
    is there anyway i can make it work while the water_balloon_test instance is destroyed on collision and it still take out other instances in radius? so far it works without that being destroyed.
     
  9. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,054
    For this piece of code:

    Code:
    with (other)
    {
        if collision_circle(x,y,30,obj_enemy,false, true)
        {
            instance_destroy();
        }
        instance_destroy();
    }
    
    Your second instance_destroy() call is inside the scope of the with(other) function, so it will actually only apply to the obj_enemy being referenced by "other".

    If you want this piece of code to work, move instance_destroy() to after the last curly bracket.

    For the second piece of code, I would set a breakpoint and step through the code in debug mode to check that the values.

    Or put show_debug_message(_num) to check that the collision_circle_list() function is actually detecting enemies with the radius.

    Is the radius big enough? How big is your water balloon sprite and enemy sprites.

    Based on your description above, my suspicion would be that the first time the collision occurs, the enemies are not in range and don't get added to the list. Then, because you commented out the instance_destroy() that destroys the water balloon, the water balloon still exists, and it keeps moving until some enemies are in range, and it destroys those.

    When you uncomment the instance_destroy(), the first time the water balloon hits an enemy, it runs the collision_circle_list(), doesn't detect any enemies(perhaps because they are not in range), destroys the balloon and then it seems like it doesn't work.

    That would be my first guess to start investigating, which will require as I said either using the debugger or putting a bunch of debug messages in to check what is actually happening.
     
  10. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    Code:
    with (other)
    {
        if collision_circle(x,y,30,obj_enemy,false, true)
        {
            instance_destroy();
        }
      
    }
    instance_destroy(
    moving instace_destroy(); after the last curly bracket does destroy the "water_balloon_test" instance but fails to destroy the enemy instances.



    If i do this
    Code:
    with (other)
    {
        if collision_circle(x,y,30,obj_enemy,false, true)
        {
            instance_destroy();
        }
        instance_destroy();
    }
    instance_destroy();
    it destroys the "water_balloon_test" instance and only one enemy instance, however not multiple when within radius.
     
  11. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    As for the second piece of code, the radius is big enough since it takes out multiple enemies at the same time. the only problem im having with this is destroying the "water_balloon_test" instance and it working the same way. because without destroying the "water_balloon_test" instance it works perfectly fine.
     
  12. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    it seems to just not want to work when i destroy the "water_balloon_test". if i don't destroy that it works as it should. there must be a way.
     
  13. iBack

    iBack Member

    Joined:
    Sep 8, 2018
    Posts:
    20
    right i tested the second code again and it seems to work. i set the radius to 500 and it worked out.
     

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