GMS 2 How to destroy one instance of an object.

Discussion in 'Programming' started by Oxiriark, Oct 12, 2019 at 8:38 PM.

  1. Oxiriark

    Oxiriark Member

    Joined:
    Dec 17, 2018
    Posts:
    4
    I am very new to Game Maker Studio, and I've learned a ton over the past week from tutorials.
    So, I am creating a simple Mario platformer game. I added coins and basically this is my code for them currently.
    Code:
    if (place_meeting(x, y, obj_player))
    {
        instance_destroy(obj_coin, false);
        audio_play_sound(snd_coinGrab, 0, false);
    }
    
    this of course destroys any instance of the object, so if I touch one then all will disappear.
    could I get some help with this?
     
  2. Binsk

    Binsk Member

    Joined:
    Jun 22, 2016
    Posts:
    596
    The function place_meeting checks if two instances's bounding boxes intersect and returns true or false. The function instance_place does the exact same check but returns the id of the first instance that is intersected. instance_place_list does the same check as well but returns a number of instances in that location.

    Assuming you don't have coins that overlap you can use instance_place as follows:
    Code:
    var _instance = instance_place(x, y, obj_player); // Do the check, store result in temporary variable
    if (_instance != noone) // If the check didn't return "no one" we have an ID stored
    {
        instance_destroy(_instance, false); // Destroy just the ID of our checked instance
        audio_play_sound(snd_coinGrab, 0, false);
    }
    
    You can read more about this function (and related functions) here and here.

    Edit: Linked the wrong manual entry. Fixed.
     
    Last edited: Oct 12, 2019 at 8:55 PM
    Amon likes this.
  3. Oxiriark

    Oxiriark Member

    Joined:
    Dec 17, 2018
    Posts:
    4
    I see, thank you so much I appreciate it!
     
  4. Oxiriark

    Oxiriark Member

    Joined:
    Dec 17, 2018
    Posts:
    4
    I attempted to implement this code, and the result seems to delete the player object instead of the coin object.
     
  5. Binsk

    Binsk Member

    Joined:
    Jun 22, 2016
    Posts:
    596
    Ah, apologies. I read through your code too fast and assumed the player was doing the collision checking against the coins, not the other way around. Didn't even read the name of the object being checked.

    In this case your original code was fine with the exception of you destroying 'obj_coin'. Each instance has a unique id that you can specify in order to modify the one instance. You can access this id via the local variable id. Pretty straightforward. Instead of specifying to destroy obj_coin simply specify to destroy the instance's id and it will only destroy the one.
     

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