Randomize BGM in 3 simple steps
In this tutorial:- Play random music when entering a map
- Songs are not played again, unless all songs have been played
- For projects with numbered files, or named files
- Works in MV, untested in MZ
Spoiler: IntroIf you are like me, you have probably more BGM files than maps to play them on. Or you might have a game that doesn't even use a map, like a card game or a board game. Then listening to the same music over and over might drive you completely nuts! But you have to test your game, so you have to listen to it. And if you get bored of your own BGM, then so might your players.
Okay, I realize this is a problem mostly based on my experience, and I can't really say I use RPG maker like the most of you do. Most of you will have maps with different BGM per region. But those who do have the same problem like I do, might want to share in my solution.
I made a few tiny script calls, which solve our problem instantly. Trigger those in a parallel event when entering a map where you want random music, and you're good too go. And the beauty of it all is, we can set this up in just a few minutes.
There are 2 different ways explained in this mini tutorial:
[*]One way to do this with numbered files (for games that only use random music)
[*]One way to do this with named files (for games that also have maps with selected music)
If you have other maps with selected music, or battles with selected music, I would advice to use named files. Otherwise you have to remember which song is what number. If you have only random music, like in a 1-map game or a puzzle game, you might prefer to number your music. That would save you a lot of work, specially if you have many files.Spoiler: Should I use named or numbered files?Whatever you prefer. If all my boxes check for numbered, but you still want to use named files, then use named files. The result is the same.
Since the result of both methods are the same, let's see what the difference is.
[*]Numbered files are useful when:
[*]You mainly use random BGM and not that much pre-selected BGM
[*]You have many music files. The more files you have, the more I lean to numbered files
[*]You make anything else than an RPG. (Card games, board games, puzzles, VN, etc etc)
[*]Named files are useful when:
[*]You also make use of pre-selected BGM in maps, battles and cut scenes
[*]You have not that many files. (With named files, more files means more work.)
[*]You make an RPG.
If you are not sure which to choose, then just go for the named files. This might be a lot of work when setting up the array in step 2b, but if you are not sure, than this is the safe way to go.
The reason why I say RPG's should go for named files, is because in an RPG you have battle BGM, different BGM for different area's etc etc. If you as a dev have to select the right music for the right scene, it is just annoying if the files are numbered instead of named.
We use one variable in this method, and in this example we use variable 11.
Replace 11 in the codes below to the id of the variable you want to use.
Make sure this variables isn't used by anything else in the entire game.
Step 1: Set up a condition
First you want to set up a conditional branch, to see if the variable we use has an array, and if so, if the array contains anything. For those who know even less about JavaScript than me, this might seem complicated, but if you copy the code below exactly, you should be good to go.
Copy this code:
Code:
!$gameVariables.value(11).length
Now go to a new (common) event and find the function "Conditional Branch".
Near the bottom of the 4th page you see "Script". Click it and paste the code behind it.
Don't forget: Change 11 to the id of your own variable!
Step 2: Set up your array
So, now we know if our variables contains an empty array. If so, we will fill it up.
First we control our variable inside that conditional branch. If you use numbered files, continue in step 2a. If you use named files, continue in step 2b.
Step 2a: For NUMBERED files
For this part, your files need to be named "1.ogg", "2.ogg", "3.ogg", "4.ogg", "5.ogg" etc, etc.
If your files are named "Theme.ogg", "Field1.ogg", "MasterOfPuppets.ogg" etc, etc, you should scroll down to step 2b.
Go to "Control Variable", select our variable and near the bottom, behind "script" you type:
Your event should now look like this:
Now, also in the conditional branch, make a script call, and copy the following code into it:
JavaScript:
for (let n = 1; n < 144; n++) { $gameVariables.value(11).push(n);}
Remember! Replace 11 with the id of your variable!
Do you see that 144 there? That number should be 1 higher than the number of files in your list.
I have 143 files, so I have set it to 144. If you have 12 files, you set this to 13.
Your event should now look like this:
Step 2b: For NAMED files
For this part, your files need to be named "Theme.ogg", "Field1.ogg", "MasterOfPuppets.ogg" etc, etc.
If your files are named "1.ogg", "2.ogg", "3.ogg", "4.ogg", "5.ogg"etc, etc, you should scroll down to step 3.
Go to "Control Variable", select our variable and near the bottom, behind "script" you type:
Replace filenames with your actual filename, but leave the quotes and the commas. You can add as many file names as you want.
Your event should now look like this.
Step 3: Lets play that sweet music!
We're almost there! This is the last step for this event!
Take this following code, and use this in a script call BELOW the condition:
Code:
var array = $gameVariables.value(11);var r = Math.randomInt(array.length);var n = array.splice(r, 1);AudioManager.playBgm( { name: n, volume: 90, pitch: 100, pan: 0 } );
I said it before, and I'll say it again... Replace 11 with the id of your own variable. You can adjust the volume and the pitch, if you like. Although, since the music is random, you might want to leave the pitch alone, but that is up to you.
And... We're actually done. The only thing left, is to make sure the event doesn't loop, by putting an "Erase Event" command at the bottom.
Your event is done and should look like this:
When using numberedfiles:
Or when you are using named files:
Final step: Run it!
Set the trigger to this event to parallel, and then copy/paste the event to each map on which you want to have random BGM. It will automatically start playing a new song, each time you enter the map. Songs will play in a random order, but each song will be played only once, till all the songs have been played.
I hope you all enjoyed my little tutorial, and even though it is just a few little steps, I hope this changes someone's RPG making experience forever.
If you would try this out, I would love to hear about your experience with his little trick. This has been a secret of mine for years and is used in projects like my card games. Every game you play, you hear a different song.
本贴来自国际rpgmaker官方论坛作者:JohnDoeGames处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/randomize-bgm-in-3-simple-steps.170011/
页:
[1]