Object destroys itself even with instance_destroy(other)

Discussion in 'Programming' started by Mael, Oct 12, 2019 at 3:57 PM.

  1. Mael

    Mael Member

    Joined:
    Feb 12, 2019
    Posts:
    6
    Hello there!

    I am working on a big project for quite a time now, so I am pretty familliar with gamemaker now. Everything works fine until I try to add a coin system. Here the explanation:

    I have an obj_ball that you can flick. I also have an obj_coinBrown.
    In the obj_ball step event I coded this:
    // With a brown coin
    if (place_meeting(x, y, obj_coinBrown))
    {
    with other instance_destroy();
    global.coins += 1;
    drawCoins = true;
    alarm[0] = room_speed * 3;
    }

    All of this works fine, but my game crashes when colliding with a coin because the obj_ball gets destroyed too. I tried all different methods aswell but either it doesnt detect the collision or the ball gets destroyed again. For example I tried getting the ID of the coin that collides but no change of results.

    Can someone please explain to me why my obj_ball gets destroyed over and over again? :(
    Thank you very much!
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,547
    place_meeting() is NOT a genuine collision event. It will NOT set the identity of other. You aren't familiar with GM if you still fall for this rookie myth.

    If you need the colliding instance ID, use instance_place() instead of place_meeting().
    Code:
    var inst_coinBrown = instance_place(x, y, obj_coinBrown);
    if (inst_coinBrown != noone) {
      with (inst_coinBrown) instance_destroy();
      global.coins += 1;
      drawCoins = true;
      alarm[0] = room_speed*3; 
    }
    
     
    Mael likes this.
  3. Mael

    Mael Member

    Joined:
    Feb 12, 2019
    Posts:
    6
    Thanks, it worked. Excuse me, I didnt want to brag or anything. Thank you again for teaching me this. :)
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    Also, just as a tip for any future issues you run into: if you get weird results when using a function, check its manual page (place the cursor over its name and press F12 to instantly open it) to make sure it does what you think it does.
     
    ParodyKnaveBob likes this.
  5. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    537
    Howdy, Mael,

    Alternatively, you could move this to a proper Collision Event. Depends on your setup of course. (Btw, tip: wrap your code blocks in [code]example[/code] BB code to keep your indentation and such!)
    Code:
    with (other) instance_destroy();
    
    ++global.coins;
    drawCoins = true;
    alarm[0] = room_speed * 3;
    
    Done. $:^ J
    (I'm really not sure why people cram so much into the Step Event, frankly.)

    I hope this helps,
     

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