Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Menu music script?

Okay, right now I'm just asking for a simple script. I need a script so that, when you go to the menu the background music either gets muted or pauses until you exit the menu again.

I'm sure there's a script for this already but I'm not sure what to look for. :\
 

Atoa

Member

Code:
#==============================================================================

# To change the music just change the value of de variable with

# the BGM_VARIABLE.

# An value = 0 means no music change

#==============================================================================

 

class Scene_Menu

  #--------------------------------------------------------------------------

  BGM_VARIABLE = 5000 #Add here de ID of the variable

  #--------------------------------------------------------------------------

  alias main_bgm_music main

  def main

    if $game_variables[BGM_VARIABLE] > 0 

      $game_temp.map_bgm = $game_system.playing_bgm

      case $game_variables[BGM_VARIABLE]

      when 1

        Audio.bgm_play("Audio/BGM/012-Theme01", 100, 100)

      when 2

        Audio.bgm_play("Audio/BGM/013-Theme02", 100, 100)

    # You can add more music

    # when X

    #   Audio.bgm_play("Audio/BGM/Audio_Name", 100, 100)

      end

    end

    main_bgm_music   

    if $game_variables[BGM_VARIABLE] > 0 and $scene.is_a?(Scene_Map)

      $game_system.bgm_stop

      $game_system.bgm_play($game_temp.map_bgm)

    end

  end

end
 

Atoa

Member

lol...
Did you read this:

BGM_VARIABLE = 5000 #Add here de ID of the variable
Just set the ID of the variable (i hope you know how to do this, because if not, it's hopeless) and then change the value of this variable. the result will depends on what you set on this part:
Code:
 

    when 1

      Audio.bgm_play("Audio/BGM/012-Theme01", 100, 100)

    when 2

      Audio.bgm_play("Audio/BGM/013-Theme02", 100, 100)

# You can add more music

# when X

#   Audio.bgm_play("Audio/BGM/Audio_Name", 100, 100)
 
Alright I had completely given up on this for a while, but I decided to look at it again And I think I figured something out.

when it says:
when 1
Audio.bgm_play("Audio/BGM/012-Theme01", 100, 100)
when 2
Audio.bgm_play("Audio/BGM/013-Theme02", 100, 100)
Does that mean if I change the BGM_Variable to 1 or 2 and the song on a map is either of those songs, then when i go to the menu it'll will mute the music? If that's the case then that's what I tried and it didn't work. I don't know much about this but i think im slowly learning this scripting thing.
 
Skimming through the script and what our good friend Atoa told us, I'm gonna re-word what he told ya maybe it'll be easier to understand.

Basically, what you need to do is pick a Variable ID from the Database, ie Variable 0042 : "Menu Music". Just for the example, we picked variable #42 as the variable of choice, so we'll set...

Code:
BGM_VARIABLE = 42

Now, when you start your game and open your menu, nothing will be playing if the variable's value is 0. Press F9 to go into your Debug menu, and change that variable's value to 1, then go back in your menu and "012-Theme01" should be playing. Exit the menu, again press F9 to go to Debug menu again and change the variable's value to 2 and "013-Theme02" should be playing next time you open your menu.

Hope I've cleared up that question, have fun with your new script! :thumb:

I fixed a bug that occured with the script; when you go from menu to another scene besides the map, sometimes the BGM will still play when you go back to the map later. This is because $game_temp.map_bgm is set no matter what scene you just exited from. Here's the fix...

Code:
#==============================================================================

# To change the music just change the value of de variable with

# the BGM_VARIABLE.

# An value = 0 means no music change

#==============================================================================

 

class Scene_Menu

  @@menu_bgm = false # Leave this alone...

  #--------------------------------------------------------------------------

  BGM_VARIABLE = 5000 #Add here de ID of the variable

  #--------------------------------------------------------------------------

  alias main_bgm_music main

  def main

    if $game_variables[BGM_VARIABLE] > 0

      unless @@menu_bgm

        $game_temp.map_bgm = $game_system.playing_bgm

        @@menu_bgm = true

      end

      case $game_variables[BGM_VARIABLE]

      when 1

        Audio.bgm_play("Audio/BGM/012-Theme01", 100, 100)

      when 2

        Audio.bgm_play("Audio/BGM/013-Theme02", 100, 100)

    # You can add more music

    # when X

    #   Audio.bgm_play("Audio/BGM/Audio_Name", 100, 100)

      end

    end

    main_bgm_music   

    if $game_variables[BGM_VARIABLE] > 0 and $scene.is_a?(Scene_Map)

      @@menu_bgm = false

      $game_system.bgm_stop

      $game_system.bgm_play($game_temp.map_bgm)

    end

  end

end
 
Alright thanks you definitely cleared that up for me. I just have one problem :[

I can't figure out how to save the variables number.

I did what you said and I switched the variable to 1 (as the script says) and it played theme 01 or w/e it's set to, so I set the volume in the script to 00 and it worked fine, but like when I close the game and I open it back up again the variable is reset to zero. How do I fix that?

If I save the game as an encrypted archive for someone else to play, after I set the variable, would it save like that?
 

Atoa

Member

Omg!
Variables are saved automatically. but it's always starts at zero on an new game, so just make an event on the start of the game that sets the variable the value you want.
Man don't you even know how to use events?
Also some creativity would be great.
 
Yeah it sounds like he's new so lets try and be nice to him =P

Alright, basically if you want the variable to be 1 when you start the game, then you'll want to make an event, set it at parallel process and then use the Control Variables command from your event commands list and set the assigned variable's value to 1, and then use an Erase Event command so the event isn't running afterwards. It should look something like this...

Control Variables [0042 : Menu BGM] = 1
Erase Event
 
Sorry Atoa, I've seen some of your posts on other forums and I know you hate spoon feeding people when it's something so easy.

Thanks Kain, I'm not /that/ new at this but I feel stupid for not realizing what the constant option meant. :\
Earlier I thought you meant I could change the values through the database so I kept checking in there.

Sorry you guys I got it now, didn't mean to be a bother :P
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top