GMS 2 Adding different music for different rooms

ArudanIsMe

Member
So I know how to add BG music for one level on repeat. But I have 0 clue on how to change the song when my character goes to a new room. Can anyone lend me a hand?
 

Yal

šŸ§ *penguin noises*
GMC Elder
Easiest idea is to play the music from the Room Creation Code of the room in question. Tied to that room specifically, changing it only affects that room.
 

ArudanIsMe

Member
Easiest idea is to play the music from the Room Creation Code of the room in question. Tied to that room specifically, changing it only affects that room.
This is probably going to be a stupid question for me to ask but could you show me a code reference by chance? :oops:
 

FrostyCat

Member
I don't see what's so difficult about playing new music in a second room, given you already know how to play music in the first room. The only difference is that you have to stop the first room's music (easily done with audio_stop_all();), other than that you're just copy-and-pasting the first room's music-playing code into the second room.
Code:
audio_stop_all();
audio_play_sound(bgm_second, 1, true);
When you learn to program, don't try to memorize exact copy-and-pastes. Memorize general patterns, and adapt them on the go. And if you don't fully understand what a specific function does, use the Manual's index tab and follow its instructions.
 

lemonhead

Member
And you may or may not wish to use audio_sound_gain to fade out the old song and then fade in the new song. Totally dependent of course on your preference and specific usage of the audio aesthetics you're trying to achieve.
 

ArudanIsMe

Member
No I understand. Im new to this. I managed to get the music to work but when I go back to the first room, the second room song still plays.

it'll probably be understandable if I show my events and creation codes:

this is my events for the objMusic: Creation: audio_play_sound(SectorV_Tree,1,true) Step: if (audio_sound_get_gain(NoSound) <= 0) { audio_stop_sound(NoSound); } Creation code for room 1: audio_stop_sound(SectorV_Tree) audio_play_sound(NoSound, 1, true); Creation Code for room 2: audio_stop_sound(NoSound); audio_play_sound(SectorV_Tree, 1, true);


PS: I have a learning disability so it takes longer for me to understand
 

Gamebot

Member
I would create a script so that all my data was in one place. This way it's easy to change too, if perhaps change your mind on which soundtrack for which room... Call said script each time you enter a room. (I suppose you could use a global with a persistent object too).

scr_music

Code:
/* PSEUDOCODE */

var myroom = room;

audio_stop_all();

switch(room)
{
 case room1: play track1; break;
 case room2: play track2; break;
 .
 .
 .
}
 
Last edited:

ArudanIsMe

Member
I would create a script so that all my data was in one place. This way it's easy to change too, if perhaps change your mind on which soundtrack for which room... Call said script each time you enter a room. (I suppose you could use a global with a persistent object too).

scr_music

Code:
/* PSEUDOCODE */

var myroom = room;

audio_stop_all();

switch(room)
{
 case room1: play track1; break;
 case room2: play track2; break;
 .
 .
 .
}

not gonna lie. Im lost bud. Im new to this. >.< Do I link the scr with the music obj?
 

Simon Gust

Member
Dude, if you're new to this, you're better off starting at the bottom of the mountain like everyone else. Because the second you've solved this problem you'll just be stuck again with your next problem.
 

Gamebot

Member
I would try both ways though the script only has to fire once per room. No extra objects needed.

With the script you can call it from the creation of every room and it will automatically fire.

Just write:
scr_music(room);

How many rooms were you thinking?
 

ArudanIsMe

Member
I would try both ways though the script only has to fire once per room. No extra objects needed.

With the script you can call it from the creation of every room and it will automatically fire.

Just write:
scr_music(room);

How many rooms were you thinking?
I made 19 rooms(including main menu and game menu) 4 rooms uses "main_menu_music audio" 3 rooms use the "SectorV_Tree" audio. two rooms have no audio as its a inbetween(like stairs) 5 rooms use "Cherry_Lane audio" 5 rooms use "City audio" and one room uses "beach audio"
 

Gamebot

Member
I think a script would be the way to go, the only reason is so you can easily make changes if needed. Otherwise as suggested you can always put in every room creation event the track also
 

ArudanIsMe

Member
I think a script would be the way to go.
so would


var Main_menu = room;

audio_stop_all();

switch(room)
{
case control_room: play SectorV_Tree; break;
case Hallway_Uno_house: play Cherry_Lane; break;
.
.
.
}

work?


Main_Menu is a room while control_room and Hallway_Uno_house are also rooms
 

Gamebot

Member
You would have to use appropriate functions within the switch event to play the sound. ( audio_play....)
Within the creation event of every room. You would just call the same script every time: scr_music(room)

Oh poo write at the bottom of the switch (Since I stupidly forgot the escape clause)
default: exit; break;

Actually you shouldn't need break on this one line only but I do it just because of better habit practice.

first line should be:

var myroom = argument0;
switch(myroom){
.
}

You can look up switch statements in the manual for further reference.
 
Last edited:
Top