So after going through some tutorials on Ruby scripting for RPG Maker Ace, I wrote a ruby script to play music when the menu is selected. However I ran into a few issues. Here is my code so far:
#Menu Music Scriptclass Scene_Map < Scene_Base alias bg_menumusic_call_menu call_menu def call_menu $last_bgm = RPG::BGM.last $last_bgs = RPG::BGS.last RPG::BGM.new("Town5", 100, 125).play bg_menumusic_call_menu endendclass Scene_Menu < Scene_MenuBase alias bg_menumusic_menu_terminate terminate def terminate if SceneManager.scene_is?(Scene_Map) $last_bgm.replay rescue nil $last_bgs.replay rescue nil end bg_menumusic_menu_terminate endendThe problem I run into is this: If I don't use $ for global, the code in Scene_Menu < Scene_Menu_Base does not know the variables exist for the sound that played before the menu was called, so it is nil every time. However, if I use globals, well, I can run into obvious problems later.
So far the global solution is working for my project, but I'd like to get around it to avoid possible later issues. Suggestions on how to make this work without using globals? I assume I need to store the variables somewhere else, but is there such a place that both classes can access?
Menu Music Script Issues, RPG Maker ACE
● ARCHIVED · READ-ONLY
-
-
It isn't really necessary to use last bgm if you want to play a BGM when you hit the menu, IMHO. There's a function called $game_map.autoplay that plays the map BGM when you exit the menu as well.
Here's an example of how to play a BGM when you hit the menu:
Code:module Soulpour module Menu_BGM PLAY_NEW_BGM = true #play different music on menu? def self.play_menubgm RPG::BGM.new("Theme1", 100, 100).play #BGM Name, Volume, Pitch end end endclass Scene_Menu < Scene_MenuBase alias :menu_bgm_play_start :start #-------------------------------------------------------------------------- # * Start Processing #-------------------------------------------------------------------------- def start menu_bgm_play_start return if !Soulpour::Menu_BGM::PLAY_NEW_BGM Soulpour::Menu_BGM.play_menubgm end #-------------------------------------------------------------------------- # * Termination Processing #-------------------------------------------------------------------------- def terminate super dispose_background return if !Soulpour::Menu_BGM::PLAY_NEW_BGM $game_map.autoplay end end -
Doesn't $game_map_autoplay though go to the value you set for the map in the editor? If so I can't use that, in some of my maps I force change the music due to scenes/events (for example, in one map when a bad guy shows up, I switch the music from something calm to something tense for the map), and if someone saves during that point it would go back to the wrong music. Or am I wrong on that?