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

Audio

Discussion in 'Programming' started by Twilight Raven, Jan 26, 2020.

  1. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    So I don't know much about the audio system. I know I have 48 stages, each with its own music. Now I have a store where the player can purchase gems. The store has its own music. So if you click on the coin icon it takes you to the store, the music changes. Now I want to be able to return to the game and switch back to the previous room's music. How do I go about that? I thought about setting a variable when I do the audio_play_sound but that means I would have to have 48 variables to code by hand. Actually 96 lines code since I have to change the initial audio_play_sound and then I have to change the music in the store back to the room's music. There has to be a more efficient way to go about this.

    Thanks
    Ted
     
  2. Nidoking

    Nidoking Member

    Joined:
    Nov 20, 2019
    Posts:
    107
    I have a Controller object handle the music. In the Room Start event, I check whether audio_is_playing(Music) and, if not, I stop the current music and play the Music variable. Then, in each room's Creation Code, I set Controller.Music to the music for that room. So each room handles its own music automatically.
     
  3. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    922
    I do the same as @Nidoking except without the controller object. Let you room determine what music should be played, and in the Room Start stop all other music and start the one for the room. It's basically 2 lines of code in each room, and does not require 48 variables.

    Alternatively you can set up one global variable for previousMusic, and when leaving the room to go to the store you set the variable to be the music that you want to play when it leaves the store, and then when leaving the store just play the music resource that is in previousMusic.
     
  4. Twilight Raven

    Twilight Raven Member

    Joined:
    Sep 1, 2016
    Posts:
    829
    That's what I need to know. How do I save a sound in a variable? So I can do this:

    room_music = somehowgettheaudioplayingrightnow
    goto store room
    music = store music
    goto back to first room
    music = room_music
     
  5. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    922
    You don't need to get the music that is currently playing. Both @Nidoking and I have already suggested:
    The simplest answer is just to put into the Creation Code of each room:
    Code:
    audio_stop_all();
    audio_play_sound(yourMusicResource, 10, true);
    That's it, and you would do exactly the same in the store room as well. Now if your rooms are all persistent then you would have to use a different method as the Creation Code would only be called the first time that you go to the room. But as you haven't said if they are or not, you might just be able to get away with the above. Even if you are going to use the controller, or the global variable for the previous music, you still don't need to try to figure out what music is playing - you would just set the variable to a music resource and then use that for the audio_play_sound call.
     
    Last edited: Jan 27, 2020
  6. Nidoking

    Nidoking Member

    Joined:
    Nov 20, 2019
    Posts:
    107
    If you do want to save a sound in a variable, you don't do it while it's playing. You store the return value of audio_play_sound. That's the specific sound you would need to stop.
     

Share This Page