How to destrory old instance if a new instance is created?

Discussion in 'Programming' started by Matthew Imamura, Jul 12, 2019.

  1. Matthew Imamura

    Matthew Imamura Member

    Joined:
    Jul 10, 2019
    Posts:
    10
    So basically I want to destroy the 1st instance if a 2nd one is created.
    here are the current codes I have:

    Code:
    if (instance_number(obj_activetrail) > maxobject)
    {
         with (instance_find(obj_activetrail, maxobject))
         {
               instance_destroy();
         }
    }
    
    But instead of destroying the 1st one, it is destroying the new ones that are being made.

    is there anything i can use to destroy the 1st instance if a new instance is created?
    any help would be much appreciated, thank you for your time :)
     
  2. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,432
    I think this will work...

    with (instance_find(obj_activetrail, 0))
     
    Matthew Imamura likes this.
  3. Matthew Imamura

    Matthew Imamura Member

    Joined:
    Jul 10, 2019
    Posts:
    10
    It worked! thank you so much!
    but if you can, can you please explain to me how it worked?
     
    obscene likes this.
  4. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,549
    The Manual's entry on instance_find() tells you why it worked.
    Putting a 0 in the position you were shown means finding the first instance of the object.
     
    Matthew Imamura likes this.
  5. Matthew Imamura

    Matthew Imamura Member

    Joined:
    Jul 10, 2019
    Posts:
    10
    thank you. I think i understand it a bit. i'll just have to read and practice more.
     
  6. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,432
    Basically, any time you are working with a data structure in GM, like a list or even an array, counting begins at 0. A list with 3 positions would be numbered 0,1,2. The first position is 0. Similarly, the first frame of an animation is 0. The beginning position of an audio sample is 0. To go to the first room in your game, the code would be room_goto(0). So just the same, when you get the list of instances of an object, the first one will be 0.
     
    Matthew Imamura likes this.
  7. Matthew Imamura

    Matthew Imamura Member

    Joined:
    Jul 10, 2019
    Posts:
    10
    Now it makes total sense. thank you!
     

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