• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Music switch statement problem

P

Pikpik

Guest
When I go into one room that has different music than the previous room and come back, the music from previous room doesn't play. Instead the last music track keeps playing until I go into room that has music that is lower than it in the switch statement. How do I fix this?

Here's my code:
GML:
{

case character_select:

audio_play_sound(mus_select, 1000, true);

break;

case hub:

audio_play_sound(mus_hub, 1000, true);

break;
and so on...
 

FoxyOfJungle

Kazan Games
GML:
audio_stop_all();

switch (room)
{
    case character_select:
        audio_play_sound(mus_select, 100, true);
        break;
        
    case hub:
        audio_play_sound(mus_hub, 100, true);
        break;
}
 

Alice

Darts addict
Forum Staff
Moderator
Can't see anything wrong, but with incomplete code I can't really tell well the problem lies. One thing that comes to my mind is having some kind of default case that plays the last track no matter what happens.

At any rate, instead of one central switch-case, I'd instead suggest linking the music to the room more directly.
For example, you could create an object called obj_bgm_player and doodle some placeholder note or other sprite to make it easily recognisable (make it invisible, though).
Then, in object Variable Definitions add a "bgm" variable with "Asset" type, and in options selecting "Sounds" as the only option. Also, type "undefined" as the default value.

Then, in Room Start code, you could add something like:
GML:
if (is_undefined(bgm)) {
    audio_stop_all();
    // if "bgm" is undefined, it's interpreted as no music playing at all
} else if (!audio_is_playing(bgm)) {
    audio_stop_all();
    audio_play_sound(bgm, 1000, true);
} else {
    // if "bgm" is already playing, just keep playing it
}
Such code should be easy enough to expand upon if you e.g. needed to implement a muting/unmuting functionality.

With this, you can directly associate each room with specific background music from within the room itself, and if there are multiple rooms with same track, the BGM will play without restarting when you move between them.

Of course, if there are more advanced BGM-handling scenarios (e.g. choosing a room music based on a global variable), you might need to add a more complex mechanism. That's also why the music handling is in the Room Start code - that way you have a chance to choose the appropriate BGM in Instance Creation Code before Room Start is called.
 
P

Pikpik

Guest
Here's the full code:
GML:
audio_stop_all()

switch room

{

case character_select:

audio_play_sound(mus_select, 1000, true);

break;

case hub:

audio_play_sound(mus_hub, 1000, true);

break;

}
 

Alice

Darts addict
Forum Staff
Moderator
With these few cases and no glaring errors, I guess the problem isn't with the switch/case statement after all.

When this code is executed? Is it the Create/Room Start event of some object? Is that object persistent? If not, is there an instance of this object in each of the rooms you want to switch the music for?
A well-written music-switching code won't do any good if it's not run in the first place.

As it is now, the snippet posted should:
- begin playing mus_select in the character_select room
- begin playing mus_hub in the hub room
- stop the music altogether in any other room

But that's only provided the snippet is run in the first place. Otherwise, the last music will keep playing, because no piece of code stops it.
 
P

Pikpik

Guest
1. When the object is created.
2. Yes, of the music object.
3. No.
4. Yes.
 

Nidoking

Member
Never mind, you answered non-numbered questions with numbers and I misread which answer went with which question. Have you tried stepping through this in the debugger?
 
P

Pikpik

Guest
I just decided to use the code above, does this look right?

Create Event:
GML:
asset_sound = bgm
bgm = undefined
Room Start Event:
GML:
if (is_undefined(bgm)) {
    audio_stop_all();
    // if "bgm" is undefined, it's interpreted as no music playing at all
} else if (!audio_is_playing(bgm)) {
    audio_stop_all();
    audio_play_sound(bgm, 1000, true);
} else {
    // if "bgm" is already playing, just keep playing it
}
 

Alice

Darts addict
Forum Staff
Moderator
I just decided to use the code above, does this look right?

Create Event:
GML:
asset_sound = bgm
bgm = undefined
Not sure what the Create Event is for, especially since now "bgm" variable will be undefined every time you setup the object, making things silent. So this Create Event needs to be removed.

The "bgm" variable instead would be set in Instance Variables/Instance Creation Code, depending on whether room music is aways the same or it may vary (based on global variables, or maybe it's just randomised).

1. If the room music is always the same - use Instance Variable "bgm', that you can set from the Room Editor.
You can read more about these here in Variables section, or in "Object Variables" page in new manual.

2. If you need to decide the room music based on the current game state (e.g. some global variable) - set "bgm" variable in Instance Creation Code.

The Room Start event is fine.
 
Top