Help with Alternating Background Music for Location and Time of Day

Hi there.

So recently I have been trying to get background music to change for each location based on time of day. For example, I am trying to have background music for inside of a specific shop to change based on whether or not it is morning, day, dusk, night, etc. I would also like a different set of music to play for outside of the shop along with other areas, which alternates between morning, day, dusk, and night. So basically, if the player enters the shop during the morning, the music would be lively and upbeat. But if they enter the shop at night, it will be a different music track, which is more calm and relaxing.

So far, I have been using a common event set to run parallel to try to get this to work, which checks for whether or not the player is in this specific shop, and what time of day it is. This almost works, except that the common event will just repeat the first part of the song over and over (as in only the first note or so). I tried to use a loop to fix this, and it seemed to work, except that it seemed to cause the game to run very slowly and the music sounded poor quality as well.

I then tried to use various wait commands... however, the music track I am using is 2 minutes and 41 seconds long. Wait time only goes up to 999 frames, so I used nine different 999 wait times, plus one 789 long wait time. This of course was very inefficient, and also made the game crash when I tried to run it.

I have tried to find information on a way to do this elsewhere, but so far have not found any good solutions which seem to work for my particular circumstances.

In case any of this information is relevant, the common event I am using here ("Soothing Breeze Music") requires a switch to be active called "Soothing Breeze," which just checks if the player has entered the "Soothing Breeze" shop. Additionally, I am using a common event for keeping track of time as well, which also runs parallel. A screenshot of this is included below.

Here are some screenshots of my attempts. I want to note that I am only showing my attempts for the first track of background music, which is for the "dawn" music (from hours 5 to 7).

Here is a screenshot of the common event without the loop or wait times, which only plays the first note of the music on repeat:

Screenshot (1689).png


Here is a screenshot of the common event with the loop, which caused the game to run very slow and seemed to degrade the quality of the music:

Screenshot (1690).png


Here is a screenshot of the common event after unwisely attempting to use over 9 wait time commands:

Screenshot (1688).png


And here is a screenshot of my time common event, in case it is relevant:

Screenshot (1691).png


If anyone has any suggestions of what I might be able to do to get this to work correctly (without making the game run very slowly), I would very grateful. I am grateful for any and all advice/help offered.

Thank you for your time!



Update: Thank you everyone for your responses! Thanks to your advice, I finally have it working correctly! The player can now enter this specific shop at any time of day, and the music will smoothly play based on what time of day it is, without any noticeable issues with quality. Additionally, if the player stays within the shop long enough for the time of day to change again, the music will change accordingly as well. So if the player enters the shop at night, the night music will play. If the player stays in the shop until dawn, the night music will change to dawn music.

For anyone else who may have had this problem, I did the following to get it to work:

Step 1:

I added a background music check to the common event which is responsible for changing the music within the Soothing Breeze shop. This common event already checked for the time of day, and if the player was inside of a building. I just had to add a variable as well (called it varBGM) which checked for whether or not that specific music track was playing.

In the varBGM variable, I chose for 1 to correspond with Dawn, 2 to correspond with Day, 3 to correspond with Dusk, 4 to correspond with Night, and 5 to correspond with Midnight.

To do the BGM check, I set the conditional to "If : varBGM =/= #" where the # is the BGM track playing (1 for Dawn music, 2 for Day music, etc).

Then, I made sure to set varBGM to the correct music track that is currently playing within this common event.

Additionally, I added a "Wait 20 Frames" command at the very end of this common event, though you cannot see it in the screenshot.

Here is a screenshot of the code of this common event:

Screenshot (1692).png


Step 2: I added a regular event set to autorun in the top left corner of the Soothing Breeze shop map. This event sets the varBGM variable to 0, then it erases itself using the Erase Event command. This event means that each time you enter the Soothing Breeze shop, the varBGM will be set to 0, so then the Soothing Breeze Music common event will play whatever music track is designated for the time of day, and then will set varBGM to the number assigned to this track, so that it will not repeat the first note over and over.

Screenshot (1693).png


Additionally, here is a screenshot of the code for the door to go into the Soothing Breeze shop, in case anyone finds this helpful:

Screenshot (1694).png


The Soothing Breeze switch is responsible for turning on the Soothing Breeze Music common event.



Thank you again to everyone for all of your useful suggestions! I am very appreciative, and I am so happy to have finally gotten this to work thanks to you.
Is there a reason why it needs to be a parallel event that is never erased or ended?
You can always just use the "end event processing" or "erase event" command to end processing after the conditional has been triggered. Here is my audio controller, feel free to take a look (it changes the music based on the town's "rank" - basically a town level up mechanic)
doOnce.PNG
AudioController.PNG

*It's worth noting that it would be better to replace the "Erase Event" seen in the common event with "End Event Processing", the only reason it's like that in my screenshot is vestigial left-over from when this audio controller used to be handled via a parallel event on the map, rather than a common event.
Is there a reason why it needs to be a parallel event that is never erased or ended?
You can always just use the "end event processing" or "erase event" command to end processing after the conditional has been triggered. Here is my audio controller, feel free to take a look (it changes the music based on the town's "rank" - basically a town level up mechanic)
View attachment 236937View attachment 236938
*It's worth noting that it would be better to replace the "Erase Event" seen in the common event with "End Event Processing", the only reason it's like that in my screenshot is vestigial left-over from when this audio controller used to be handled via a parallel event on the map, rather than a common event.

Thank you very much for your response! That is a useful suggestion, and definitely not something I had considered. However, one issue I seem to have with this method (along with calling up the event when I enter the shop, instead of having it parallel) is that I cannot figure out a way to have the music change if I stay within the shop long enough for the time of day to change. By that I mean, if I enter the shop at night, the night time music will play. If I then stay there until dawn, I would like for the dawn music to play - without having to leave and come back in. Otherwise, it seems the night time music will continue to play even if the time of day is no longer night. I assume that I have to have the event set to parallel in order for the music to change with the time while the player stays inside the building. I do not firmly understand everything in RPG Maker though, so I do not know if perhaps I am simply overlooking something with this. Thank you again for your advice! I appreciate the help.
I mean, if it's just a shop, I don't see the harm in letting the night time music play until dawn. I would imagine most players would go in, buy what they want, and get out. So over complicating things for edge cases seems unnecessary.
HOWEVER, I do understand if you want to use this system for something like a dungeon or world map, where changing the music might be more appropriate.

So, there are some veterans on here who might give me flak for this. Ideally, you don't want a bunch of parallel events running all at the same time. So this is an inefficient way of creating the behavior, but should work.

You'll need to tick the "switch" box on the DoOnce Controller. Set the DoOnce controller to be enabled by a switch such as "DoOnceSwitch". On the DoOnce controller, remove the "erase event" command originally there. Replace "erase event" with "Controll Switch: DoOnceSwitch "OFF".
So now, the DoOnce event will turn itself off.
Next we need to create a new parallel event that is constantly checking the time of day.
Decide where your times of day begin. So if the Hours variable is equal to 7, let's call that Dawn (decide the other times of day as well)
The parallel event will check:
If Hours = 7
Turn on "DoOnceSwitch"
(read below about some problems with this switch being infinitely turned back on)
If Hours = 12
Turn on "DoOnceSwitch"
if Hours = 17
Turn on "DoOnceSwitch"
(I'm a bit crazy about commenting things because I easily forget what I was doing, so I'd add comments about what those hours mean so future me knows the significance of these numbers)
This parallel event will just run infinitely looping in the background just waiting for the hour to change. Once the clock strikes the hour, it will turn on the switch.
Now, there is 1 problem with this. For as long as the "Hours" variable is = 7, it will repeatedly execute the command "Turn On DoOnceSwitch" until the clock changes to 8.
So, there are 2 options to over come this:
You could break your "Hours" variable into smaller ticks. For example, if 1 hour is comprised of 100 ticks, then you could just set the conditional to check
if Hours = 700
then it's looking only for when the hour changes, and if it's 1 tick over the hour than it will bypass this conditional.

Alternatively, if you know exactly how many frames are in an hour, you could just apply a "wait" command at the bottom of the parallel event just after it checks what hour it is.
So that would look like this:
If Hours = 7
Turn on "DoOnceSwitch"
If Hours = 12
Turn on "DoOnceSwitch"
If Hours = 17
Turn on "DoOnceSwitch"
wait 120

(hopefully it's not 120 frames between hours, otherwise the days would fly by like bullets, but you get the idea)
This way, it is only checking if the hour has changed once every hour. Of course that has it's own problems. If it's a parallel event on the map, then the check will begin happening any time the player enters the map, and in that case, they could enter the map midway through the hour. So if the player enters at Hours 6.5, the parallel event will check the time, see it's not yet 7, then wait an hour until 7.5 and run the check again.
So I suppose in that case you could just make the parallel event a parallel common event, but you don't want too many of those running in the background either.
So, it's a trade off you might need to think through. Take some time considering your options and see if you can't come up with some better solutions yourself. I am by no means saying my method is definitive.


(also forgive my use of DoOnce - I don't think it's a typical term other RPG Maker devs use, it's just a remnant of my days modding Oblivion & Skyrim, in which case it was very common to define a DoOnce variable)
The RPG Maker Script Interpretor
reads one line of code after each other, in a parallel Event it starts all over again from the top to the bottom. Up to 60 Times per Second if there are no Waits in the Code. 60 Frames = 1 Second.
That is why your Music just Plays the first Note, its starting the song from beginning again and again.

If you Insert a Wait 60 Frames, than the Song restarts every Second.

You would need a Condition if the Song is allready playing or not. That should fix your Setup.

You could try:
-Deleted- my Answer, was to quick.

Edit: Sorry Textwall deleted.

You could also try my 2nd thought, which got much less conditions and is better readable:

if hour is >= 5
if hour is <= 7
if indoor is on
If VariableBob is not Equal to 1
Set Variable Bob to 1
Play Morning Sound

if hour is >= 8
if hour is <= 12
if indoor is on
If VariableBob is not Equal to 2
Set Variable Bob to 2
Play Evening Sound
@Grassfur
From your latter attempts, it seems like you probably grasped that the crux of the problem with your first attempt is the "Fadeout" commands--since the parallel event is constantly looping, it's continually fading out and then re-starting the BGM every loop. The easiest way to deal with this is to add one more conditional check, to determine whether or not the desired BGM is already playing, and to only execute the commands if it is not already playing.

This is just pseudo-code, to demonstrate the concept, so you'll obviously want to translate this into event commands:

Code:
if (morning)
{
    if (inside)
    {
        if (playingBGM != 1)
        {
            playingBGM = 1
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: morningBGM
        }
    }
}
if (day)
{
    if (inside)
    {
        if (playingBGM != 2)
        {
            playingBGM = 2
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: dayBGM
        }
    }
}
if (evening)
{
    if (inside)
    {
        if (playingBGM != 3)
        {
            playingBGM = 3
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: eveningBGM
        }
    }
}
if (night)
{
    if (inside)
    {
        if (playingBGM != 4)
        {
            playingBGM = 4
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: nightBGM
        }
    }
}
Wait 19 Frames

The playingBGM variable will keep it from trying to play the same BGM again if it's already playing. You should initialize playingBGM to 0 once every time the player enters the map--i.e. make an autorun map event that sets playingBGM to 0 and then erases itself with the "Erase Event" command.

The "Wait 19 Frames" at the very end is just to keep the parallel event from looping more often than it actually needs to. There's not really a good reason to be checking the BGM 60 times every second--three times per second should be more than sufficient. Adding a Wait at the end of a parallel event, so it only executes as often as necessary, helps reduce the performance impact of the parallel event. It's a good practice to do this with all parallel events, as situations where you actually *need* a parallel event to run 60 times per second are extremely rare. I don't mean that you should use a 19 Frame Wait for every parallel event, but definitely *some* type of Wait. Even adding a 1 Frame Wait can yield deceptively large performance savings, since it cuts the number of loops in half.

If this doesn't make sense, feel free to let me know, and I'll try to explain it better.
I mean, if it's just a shop, I don't see the harm in letting the night time music play until dawn. I would imagine most players would go in, buy what they want, and get out. So over complicating things for edge cases seems unnecessary.
HOWEVER, I do understand if you want to use this system for something like a dungeon or world map, where changing the music might be more appropriate.

So, there are some veterans on here who might give me flak for this. Ideally, you don't want a bunch of parallel events running all at the same time. So this is an inefficient way of creating the behavior, but should work.

You'll need to tick the "switch" box on the DoOnce Controller. Set the DoOnce controller to be enabled by a switch such as "DoOnceSwitch". On the DoOnce controller, remove the "erase event" command originally there. Replace "erase event" with "Controll Switch: DoOnceSwitch "OFF".
So now, the DoOnce event will turn itself off.
Next we need to create a new parallel event that is constantly checking the time of day.
Decide where your times of day begin. So if the Hours variable is equal to 7, let's call that Dawn (decide the other times of day as well)
The parallel event will check:
If Hours = 7
Turn on "DoOnceSwitch"
(read below about some problems with this switch being infinitely turned back on)
If Hours = 12
Turn on "DoOnceSwitch"
if Hours = 17
Turn on "DoOnceSwitch"
(I'm a bit crazy about commenting things because I easily forget what I was doing, so I'd add comments about what those hours mean so future me knows the significance of these numbers)
This parallel event will just run infinitely looping in the background just waiting for the hour to change. Once the clock strikes the hour, it will turn on the switch.
Now, there is 1 problem with this. For as long as the "Hours" variable is = 7, it will repeatedly execute the command "Turn On DoOnceSwitch" until the clock changes to 8.
So, there are 2 options to over come this:
You could break your "Hours" variable into smaller ticks. For example, if 1 hour is comprised of 100 ticks, then you could just set the conditional to check
if Hours = 700
then it's looking only for when the hour changes, and if it's 1 tick over the hour than it will bypass this conditional.

Alternatively, if you know exactly how many frames are in an hour, you could just apply a "wait" command at the bottom of the parallel event just after it checks what hour it is.
So that would look like this:
If Hours = 7
Turn on "DoOnceSwitch"
If Hours = 12
Turn on "DoOnceSwitch"
If Hours = 17
Turn on "DoOnceSwitch"
wait 120

(hopefully it's not 120 frames between hours, otherwise the days would fly by like bullets, but you get the idea)
This way, it is only checking if the hour has changed once every hour. Of course that has it's own problems. If it's a parallel event on the map, then the check will begin happening any time the player enters the map, and in that case, they could enter the map midway through the hour. So if the player enters at Hours 6.5, the parallel event will check the time, see it's not yet 7, then wait an hour until 7.5 and run the check again.
So I suppose in that case you could just make the parallel event a parallel common event, but you don't want too many of those running in the background either.
So, it's a trade off you might need to think through. Take some time considering your options and see if you can't come up with some better solutions yourself. I am by no means saying my method is definitive.


(also forgive my use of DoOnce - I don't think it's a typical term other RPG Maker devs use, it's just a remnant of my days modding Oblivion & Skyrim, in which case it was very common to define a DoOnce variable)

The RPG Maker Script Interpretor
reads one line of code after each other, in a parallel Event it starts all over again from the top to the bottom. Up to 60 Times per Second if there are no Waits in the Code. 60 Frames = 1 Second.
That is why your Music just Plays the first Note, its starting the song from beginning again and again.

If you Insert a Wait 60 Frames, than the Song restarts every Second.

You would need a Condition if the Song is allready playing or not. That should fix your Setup.

You could try:
-Deleted- my Answer, was to quick.

Edit: Sorry Textwall deleted.

You could also try my 2nd thought, which got much less conditions and is better readable:

if hour is >= 5
if hour is <= 7
if indoor is on
If VariableBob is not Equal to 1
Set Variable Bob to 1
Play Morning Sound

if hour is >= 8
if hour is <= 12
if indoor is on
If VariableBob is not Equal to 2
Set Variable Bob to 2
Play Evening Sound

@Grassfur
From your latter attempts, it seems like you probably grasped that the crux of the problem with your first attempt is the "Fadeout" commands--since the parallel event is constantly looping, it's continually fading out and then re-starting the BGM every loop. The easiest way to deal with this is to add one more conditional check, to determine whether or not the desired BGM is already playing, and to only execute the commands if it is not already playing.

This is just pseudo-code, to demonstrate the concept, so you'll obviously want to translate this into event commands:

Code:
if (morning)
{
    if (inside)
    {
        if (playingBGM != 1)
        {
            playingBGM = 1
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: morningBGM
        }
    }
}
if (day)
{
    if (inside)
    {
        if (playingBGM != 2)
        {
            playingBGM = 2
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: dayBGM
        }
    }
}
if (evening)
{
    if (inside)
    {
        if (playingBGM != 3)
        {
            playingBGM = 3
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: eveningBGM
        }
    }
}
if (night)
{
    if (inside)
    {
        if (playingBGM != 4)
        {
            playingBGM = 4
            Fadeout: BGS
            Fadeout: BGM
            Play BGM: nightBGM
        }
    }
}
Wait 19 Frames

The playingBGM variable will keep it from trying to play the same BGM again if it's already playing. You should initialize playingBGM to 0 once every time the player enters the map--i.e. make an autorun map event that sets playingBGM to 0 and then erases itself with the "Erase Event" command.

The "Wait 19 Frames" at the very end is just to keep the parallel event from looping more often than it actually needs to. There's not really a good reason to be checking the BGM 60 times every second--three times per second should be more than sufficient. Adding a Wait at the end of a parallel event, so it only executes as often as necessary, helps reduce the performance impact of the parallel event. It's a good practice to do this with all parallel events, as situations where you actually *need* a parallel event to run 60 times per second are extremely rare. I don't mean that you should use a 19 Frame Wait for every parallel event, but definitely *some* type of Wait. Even adding a 1 Frame Wait can yield deceptively large performance savings, since it cuts the number of loops in half.

If this doesn't make sense, feel free to let me know, and I'll try to explain it better.

Thank you everyone for all of these very useful suggestions! 🙂 Following the advice I was given, I have finally gotten it to work, without any noticeable lag or issue with quality. I updated my post describing what I did for anyone else who may have the same struggle. Thank you all again!
In case the Music does not start early enough, change the Autorun event to Parallel Event.

Autorun Events tend to start after the map is visible for a split second. paralell ones play a bit earlier if no autoruns are active on that map.
In case the Music does not start early enough, change the Autorun event to Parallel Event.

Autorun Events tend to start after the map is visible for a split second. paralell ones play a bit earlier if no autoruns are active on that map.
Thank you for the tip! So far it has not given me any issue that I can notice, but I will definitely keep this in mind.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-alternating-background-music-for-location-and-time-of-day.150191/