1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Audio Error When Object is Destroyed (earr@pe sound)

Discussion in 'Programming' started by Rahiokawines, Sep 3, 2019.

  1. Rahiokawines

    Rahiokawines Member

    Joined:
    Aug 30, 2019
    Posts:
    4
    Once all enemies in the room are dead, my gate is destroyed. I set it so when the gate is destroyed, a sound effect plays. It works fine when in sounds menu, but in game, it is super loud and keeps repeating itself untill i exit the room. I have tried using different sounds and it still happens so I think its in the coding. here it is:

    oPlayer Step Event

    //Next Level Gate break
    if(instance_number(oEnemy) <= 00)
    {
    instance_destroy(oGate);
    object_set_visible(oGate,false); // I could Still see Gate after "destroy" so I added this.
    audio_play_sound(sdGateOpen, 10, false)
    }
     
  2. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    4,214
    You have it in the step event, so it will play 60 times per second... use a variable to stop playing it once you've played it once.
     
    EvanSki likes this.
  3. CedSharp

    CedSharp Member

    Joined:
    Sep 5, 2016
    Posts:
    665
    First of all, instance_destroy() doesn't take any arguments. It will destroy the object that is calling it.
    To destroy another object, you can use the "with()" statement, that allows you to execute a script as if it was running inside that object.

    In this case, the proper usage of "instance_destroy()" to destroy the gate would be:
    Code:
    with(oGate) {
      // This code will run inside the oGate object,
      // like if we were executing something in
      // the step event
      instance_destroy()
    }
    Then, you can either create a variable to track if you played the audio or not, like @Yal mentioned,
    or you could make a very simple addition to your if() condition:
    Code:
    if(instance_number(oEnemy) == 0 && instance_exists(oGate)) {
      with(oGate) instance_destroy(); // This line makes the above if condition to never be true again
      audio_play_sound(sdGateOpen, 10, false);
      // Note you don't need the visibility anymore
      // since we actually destroyed the gate this time
    }
    Hope this helps
     
  4. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,925
    instance_destroy takes arguments in GMS 2.
     
    CedSharp likes this.
  5. CedSharp

    CedSharp Member

    Joined:
    Sep 5, 2016
    Posts:
    665
    You are absolutely right, guess that's something I transitioned from GMS 1.x...
    Thanks, I've learned today :D
     
  6. Rahiokawines

    Rahiokawines Member

    Joined:
    Aug 30, 2019
    Posts:
    4
    The oGate code is in the create event?
     
  7. CedSharp

    CedSharp Member

    Joined:
    Sep 5, 2016
    Posts:
    665
    The code is simply yours, but modified. You mentioned your code was in the step event of oPlayer ?
    That's where the code goes.

    The first code of my post was to show you how to use the "with()" statement, but as @TsukaYuriko mentioned,
    you don't even have to use my method since I was wrong and in GMS 2, instance_destroy() does take an ar gument, the target instance to destroy.
     

Share This Page