Calling a battle with a script

Mon, 14 Jul 2008 23:16:56 -0400 - Author:
The following allows battles to be called from script in RPG Maker XP and VX.

pbStartBattle(troopID, canEscape, canLose) - Starts a battle with the specified troop ID ("troopID"). The player may or may not escape (set "canEscape" to true or false), and may or may not be allowed to lose (set "canLose" to true or false). An optional block specifies the result of the battle. See the code for an example.

The code follows.
def pbStartBattle(troopID, canEscape, canLose, &block)
 return if $game_temp.in_battle
 if $game_temp.respond_to?("next_scene")
  # For RPGVX style
  $game_troop.setup(troopID)
  $game_troop.can_escape = canEscape
  $game_troop.can_lose = canLose
  $game_temp.next_scene = "battle"
 else
  # For RPGXP style
  $game_temp.battle_abort = true
  # Set battle calling flag
  $game_temp.battle_calling = true
  $game_temp.battle_troop_id = troopID
  $game_temp.battle_can_escape = canEscape
  $game_temp.battle_can_lose = canLose
 end
 if block_given?
#  The block given takes a single parameter, n, which contains the result of the battle.
#  Example:
#  pbStartBattle(20, true, true) {|n|
#   case n
#    when 0;print "Player won"
#    when 1;print "Player escaped"
#    when 2;print "Player lost"
#   end
#  }
  $game_temp.battle_proc = block
 else
  $game_temp.battle_proc = nil
 end
end