There may be situations where you want to apply a script to make something happen mid-battle, but you can't use the "midbattleScript" Battle Rule to apply it. For example, perhaps you want something to occur during every wild battle, or in every battle on a specific map, or after a switch is flipped.
In this case, the best course of action may be to create a global midbattle script. A global script is one that applies at all times, and will be called by every battle. These scripts can stack on top of existing midbattle scripts without overwriting them, too.
Here's an example of a global midbattle script that this plugin utilizes to add low HP music to play whenever the HP of the player's Pokemon is low:
MidbattleHandlers.add(:midbattle_global, :low_hp_music,proc { |battle, idxBattler, idxTarget, trigger|nextif!Settings::PLAY_LOW_HP_MUSIC battler = battle.battlers[idxBattler]nextif!battler ||!battler.pbOwnedByPlayer? track = pbGetBattleLowHealthBGMnextif!track.is_a?(RPG::AudioFile) playingBGM = battle.playing_bgmcase trigger#---------------------------------------------------------------------------# Restores original BGM when HP is restored to healthy.when"BattlerHPRecovered_player"nextif playingBGM != track.namenextif battle.pbAnyBattlerLowHP?(idxBattler) battle.pbResumeBattleBGMPBDebug.log("[Midbattle Global] low HP music ended.")#---------------------------------------------------------------------------# Restores original BGM when battler is fainted.when"BattlerHPReduced_player"nextif playingBGM != track.namenextif battle.pbAnyBattlerLowHP?(idxBattler)nextif!battler.fainted? battle.pbResumeBattleBGMPBDebug.log("[Midbattle Global] low HP music ended.")#---------------------------------------------------------------------------# Plays low HP music when HP is critical.when"BattlerHPCritical_player"nextif playingBGM == track.name battle.pbPauseAndPlayBGM(track)PBDebug.log("[Midbattle Global] low HP music begins.")#---------------------------------------------------------------------------# Restores original BGM when sending out a healthy Pokemon.# Plays low HP music when sending out a Pokemon with critical HP.when"AfterSendOut_player"if battle.pbAnyBattlerLowHP?(idxBattler)nextif playingBGM == track.name battle.pbPauseAndPlayBGM(track)PBDebug.log("[Midbattle Global] low HP music begins.")elsif playingBGM == track.name battle.pbResumeBattleBGMPBDebug.log("[Midbattle Global] low HP music ended.")endend })
Unlike regular midbattle scripts, global midbattle scripts can only be implemented by hardcoding a midbattle script handler. So I would advise that you refer to the "Advanced: Hardcoding" section for a general breakdown on how to set up one of these handlers. The only difference with a global midbattle script handler is that it uses the :midbattle_global handler ID, instead of the usual :midbattle_scripts. Otherwise, they're structured in the same way.