Advanced: Global Scripts

Section 4.C.5

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|
    next if !Settings::PLAY_LOW_HP_MUSIC
    battler = battle.battlers[idxBattler]
    next if !battler || !battler.pbOwnedByPlayer?
    track = pbGetBattleLowHealthBGM
    next if !track.is_a?(RPG::AudioFile)
    playingBGM = battle.playing_bgm
    case trigger
    #---------------------------------------------------------------------------
    # Restores original BGM when HP is restored to healthy.
    when "BattlerHPRecovered_player"
      next if playingBGM != track.name
      next if battle.pbAnyBattlerLowHP?(idxBattler)
      battle.pbResumeBattleBGM
      PBDebug.log("[Midbattle Global] low HP music ended.")
    #---------------------------------------------------------------------------
    # Restores original BGM when battler is fainted.
    when "BattlerHPReduced_player"
      next if playingBGM != track.name
	  next if battle.pbAnyBattlerLowHP?(idxBattler)
      next if !battler.fainted?
      battle.pbResumeBattleBGM
      PBDebug.log("[Midbattle Global] low HP music ended.")
    #---------------------------------------------------------------------------
    # Plays low HP music when HP is critical.
    when "BattlerHPCritical_player"
      next if 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)
        next if playingBGM == track.name
        battle.pbPauseAndPlayBGM(track)
        PBDebug.log("[Midbattle Global] low HP music begins.")
      elsif playingBGM == track.name
        battle.pbResumeBattleBGM
        PBDebug.log("[Midbattle Global] low HP music ended.")
      end
    end
  }
)

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.

Last updated