Solid understanding to the default RMVXA battle flow implementations

This topic aims to talk about the more advanced stuffs of the default RMVXA battle flow implementations, which is mainly about actual codes instead of core concepts. You're assumed to have at least:

- Some scripting proficiency(inexperienced scripters having written at least few simple yet complete scripts)

- Basic knowledge to the default RMVXA battle flow implementations

Again, I'll focus on the Action Input Phase, Action Execution Phase and their transitions, Turn Start and Turn End(the rest of the battle flow should be easy enough for you to be figured out yourselves 🙂 ). But before that, I want to cover some background information first.

MVC and the default RGSS3 script architecture

Spoiler
The default RGSS3 script architecture at least closely resembles to(if not just uses) MVC. For instance:

- BattleManager is mainly Model although it's some Controller features

- Game Objects(except Game_Interpreter) closely resemble to Model

- Sprites closely resemble to View

- Windows are mainly View although it's some Model features

- Scenes closely resemble to Controller

Scene_Battle

It mainly defines what the default RMVXA battle system does but not how it works. It basically constructs its core framework by specifying what to do under what circumstances. How those things are actually done and how those circumstances are actually checked isn't the main concern of Scene_Battle.

As the default RMVXA battle system receives players' commands, it needs to have an handler for each command. For instance:

#-------------------------------------------------------------------------- # * Create Party Commands Window #-------------------------------------------------------------------------- def create_party_command_window @party_command_window = Window_PartyCommand.new @party_command_window.viewport = @info_viewport @party_command_window.set_handler🙂fight, method🙂command_fight)) @party_command_window.set_handler🙂escape, method🙂command_escape)) @party_command_window.unselect end
Code:
  #--------------------------------------------------------------------------  # * Create Actor Commands Window  #--------------------------------------------------------------------------  def create_actor_command_window    @actor_command_window = Window_ActorCommand.new    @actor_command_window.viewport = @info_viewport    @actor_command_window.set_handler(:attack, method(:command_attack))    @actor_command_window.set_handler(:skill,  method(:command_skill))    @actor_command_window.set_handler(:guard,  method(:command_guard))    @actor_command_window.set_handler(:item,   method(:command_item))    @actor_command_window.set_handler(:cancel, method(:prior_command))    @actor_command_window.x = Graphics.width  end
Code:
  #--------------------------------------------------------------------------  # * Create Skill Window  #--------------------------------------------------------------------------  def create_skill_window    @skill_window = Window_BattleSkill.new(@help_window, @info_viewport)    @skill_window.set_handler(:ok,     method(:on_skill_ok))    @skill_window.set_handler(:cancel, method(:on_skill_cancel))  end
Code:
  #--------------------------------------------------------------------------  # * Create Item Window  #--------------------------------------------------------------------------  def create_item_window    @item_window = Window_BattleItem.new(@help_window, @info_viewport)    @item_window.set_handler(:ok,     method(:on_item_ok))    @item_window.set_handler(:cancel, method(:on_item_cancel))  end
Code:
  #--------------------------------------------------------------------------  # * Create Actor Window  #--------------------------------------------------------------------------  def create_actor_window    @actor_window = Window_BattleActor.new(@info_viewport)    @actor_window.set_handler(:ok,     method(:on_actor_ok))    @actor_window.set_handler(:cancel, method(:on_actor_cancel))  end
Code:
  #--------------------------------------------------------------------------  # * Create Enemy Window  #--------------------------------------------------------------------------  def create_enemy_window    @enemy_window = Window_BattleEnemy.new(@info_viewport)    @enemy_window.set_handler(:ok,     method(:on_enemy_ok))    @enemy_window.set_handler(:cancel, method(:on_enemy_cancel))  end
When a command's sent, its handler will call its corresponding handling method to process that command. Those methods only specify what needs to be done but not how those things are done for those commands.

BattleManager

It basically sets how the battle flow of the default RMVXA battle system works. It mainly defines how things are done and how circumstances are checked.


Action Input Phase

Spoiler
Start of the phase

The Action Input Phase starts from showing the party command window letting players input inputable actors' actions or escape the battle:

#-------------------------------------------------------------------------- # * Start Party Command Selection #-------------------------------------------------------------------------- def start_party_command_selection unless scene_changing? refresh_status @status_window.unselect @status_window.open if BattleManager.input_start @actor_command_window.close @party_command_window.setup else @party_command_window.deactivate turn_start end end end
Edge cases asides, BattleManager.input_start will return true if at least 1 actor's inputable and false otherwise(it also sets the phase as input and makes new action slots for all battlers if the phase changes):

Spoiler
#-------------------------------------------------------------------------- # * Start Command Input #-------------------------------------------------------------------------- def self.input_start if @phase != :input @phase = :input $game_party.make_actions $game_troop.make_actions clear_actor end return !@surprise && $game_party.inputable? end
If at least 1 actor's inputable, the party command window will be opened to let players choose to fight or escape. If they choose to fight, the party window handler for the fight command will trigger command_fight, which calls next_command.

Doubly Linked List

If you know what a doubly linked list is and can still recall this ultra ugly flowchart(it's just so ugly lol):

Spoiler
Default%20RMVXA%20Action%20Input%20Phase.jpg
You might feel/think all action slots of all inputable actors are linked by a doubly linked list(or at least analogous data structures).

The fight command of the party command window points to the head, which is the 1st action slot of the 1st inputable actor. Its "previous pointer" does the reverse.

The tail, which is the last action slot of the last inputable actor, points to the end of the Action Input Phase.

The "next pointer" and "previous pointer "of the node storing the ith action slot of the jth inputable actor points to the node storing the (i + 1)th and (i - 1)th action slot of the same actor respectively.

The "next pointer" of the node storing the last action slot of the ith inputable actor points to the node storing the 1st action slot of the (i + 1)th inputable actor. The "previous pointer" of the latter node does the reverse.

next_command and prior_command under SceneBattle traverse that doubly linked list via the "next pointer" and the "previous pointer" of the currently pointed node respectively.

next_command traverses to the next node if the current node isn't the tail, otherwise it ends the Action Input Phase by calling turn_start.

Spoiler
#-------------------------------------------------------------------------- # * To Next Command Input #-------------------------------------------------------------------------- def next_command if BattleManager.next_command start_actor_command_selection else turn_start end end
BattleManager.next_command checks if the current node isn't the tail and points the "current node pointer" to the "next pointer" of the current node:

Spoiler
#-------------------------------------------------------------------------- # * To Next Command Input #-------------------------------------------------------------------------- def self.next_command begin if !actor || !actor.next_command @actor_index += 1 return false if @actor_index >= $game_party.members.size end end until actor.inputable? return true end
It checks if the currently pointed actor is indeed an actor and that actor's last action slot's reached. If at least one of them are false, then the next inputable actor, if any, will be selected. If the currently selected actor is the last inputable actor and the next inputable actor's requested, the method will return false instead, indicating the tail's reached.

next_command under Game_Actor checks if that actor's last action slot's reached and points to the next action slot if that's not the case:

Spoiler
#-------------------------------------------------------------------------- # * To Next Command Input #-------------------------------------------------------------------------- def next_command return false if @action_input_index >= @actions.size - 1 @action_input_index += 1 return true end
Similarly, for the "previous pointer":

Spoiler
#-------------------------------------------------------------------------- # * To Previous Command Input #-------------------------------------------------------------------------- def prior_command if BattleManager.prior_command start_actor_command_selection else start_party_command_selection end end
Code:
  #--------------------------------------------------------------------------  # * To Previous Command Input  #--------------------------------------------------------------------------  def self.prior_command    begin      if !actor || !actor.prior_command        @actor_index -= 1        return false if @actor_index < 0      end    end until actor.inputable?    return true  end
Code:
  #--------------------------------------------------------------------------  # * To Previous Command Input  #--------------------------------------------------------------------------  def prior_command    return false if @action_input_index <= 0    @action_input_index -= 1    return true  end
Inputting Actions

When an inputable actor's selected, that actor's command window will be setup:

Spoiler
#-------------------------------------------------------------------------- # * Start Actor Command Selection #-------------------------------------------------------------------------- def start_actor_command_selection @status_window.select(BattleManager.actor.index) @party_command_window.close @actor_command_window.setup(BattleManager.actor) end
If players presses cancel instead of inputting actions, prior_command will be called; Otherwise respective command handlers will be called.

For the attack command:

Spoiler
#-------------------------------------------------------------------------- # * [Attack] Command #-------------------------------------------------------------------------- def command_attack BattleManager.actor.input.set_attack select_enemy_selection end
The item part of the currently pointed action slot of that actor will be set as the attack skill. Players will then select enemy targets for that attack skill by calling select_enemy_selection(So it always targets enemies and needs target selections regardless of what's set in the database).

For each skill type's command:

Spoiler
#-------------------------------------------------------------------------- # * [Skill] Command #-------------------------------------------------------------------------- def command_skill @skill_window.actor = BattleManager.actor @skill_window.stype_id = @actor_command_window.current_ext @skill_window.refresh @skill_window.show.activate end
The skill window will set its user as that actor and its skill type as that of the command calling this. Then its contents will be refreshed and shown.

If players selected a skill, on_skill_ok will be called(otherwise on_skill_cancel will be called instead):

Spoiler
#-------------------------------------------------------------------------- # * Skill [OK] #-------------------------------------------------------------------------- def on_skill_ok @skill = @skill_window.item BattleManager.actor.input.set_skill(@skill.id) BattleManager.actor.last_skill.object = @skill if [email protected]_selection? @skill_window.hide next_command elsif @skill.for_opponent? select_enemy_selection else select_actor_selection end end
The item part of the currently pointed action slot of that actor will be set as that selected skill.

If no targets need to be selected, then next_command will be called.

If it needs to select enemies or actors, then select_enemy_selection or select_actor_selection will be called respectively. They setups the enemy window and actor window respectively.

If the targets are selected, the target part of the currently pointed action slot of that actor will be set as the index of those selected targets, and next_command will be called, otherwise the skill/item/actor command window will be opened depending on which are opened before opening the target selection window.

Similarly, for the item command:

Spoiler
#-------------------------------------------------------------------------- # * [Item] Command #-------------------------------------------------------------------------- def command_item @item_window.refresh @item_window.show.activate end
Code:
  #--------------------------------------------------------------------------  # * Item [OK]  #--------------------------------------------------------------------------  def on_item_ok    @item = @item_window.item    BattleManager.actor.input.set_item(@item.id)    if [email protected]_selection?      @item_window.hide      next_command    elsif @item.for_opponent?      select_enemy_selection    else      select_actor_selection    end    $game_party.last_item.object = @item  end
For the guard command:

Spoiler
#-------------------------------------------------------------------------- # * [Guard] Command #-------------------------------------------------------------------------- def command_guard BattleManager.actor.input.set_guard next_command end
The item part of the currently pointed action slot of the currently selected actor is set as guard, then next_command will be called(so it always targets self regardless of what's set in the database).

When inputting actions for inputable actors, the item part of any of their action slots won't be cleared, so the old ones can only be replaced by the new ones. As actions can only be executed by ending the Action Input Phase, which can only be reached by using the "next pointer" of the current node(the currently pointed action slot of the currently selected actor) via next_command, which can only be called after setting its item part, it's assured that the old ones will always be replaced by the new ones before they're executed.


Action Execution Phase

Spoiler
The battle transits from the Action Input Phase to the Action Execution Phase by calling turn_start under Scene_Battle:

#-------------------------------------------------------------------------- # * Start Turn #-------------------------------------------------------------------------- def turn_start @party_command_window.close @actor_command_window.close @status_window.unselect @subject = nil BattleManager.turn_start @log_window.wait @log_window.clear end
BattleManager.turn_start changes the phase to turn, increases the battle turn number by 1, and makes the action execution order queue:

Spoiler
#-------------------------------------------------------------------------- # * Start Turn #-------------------------------------------------------------------------- def self.turn_start @phase = :turn clear_actor $game_troop.increase_turn make_action_orders end
make_action_orders queues all battlers according to their speed(the faster one is the one more up front):

Spoiler
#-------------------------------------------------------------------------- # * Create Action Order #-------------------------------------------------------------------------- def self.make_action_orders @action_battlers = [] @action_battlers += $game_party.members unless @surprise @action_battlers += $game_troop.members unless @preemptive @action_battlers.each {|battler| battler.make_speed } @action_battlers.sort! {|a,b| b.speed - a.speed } end
update(a method called per frame) under Scene_Battle calls process_event and process_action under Scene_Battle if the battle is in the Action Execution Phase:

Spoiler
#-------------------------------------------------------------------------- # * Frame Update #-------------------------------------------------------------------------- def update super if BattleManager.in_turn? process_event process_action end BattleManager.judge_win_loss end
It also checks if victory or defeat should come.

process_event under Scene_Battle is the method that processes all battle and common events(including forced actions):

Spoiler
#-------------------------------------------------------------------------- # * Event Processing #-------------------------------------------------------------------------- def process_event while !scene_changing? $game_troop.interpreter.update $game_troop.setup_battle_event wait_for_message wait_for_effect if $game_troop.all_dead? process_forced_action BattleManager.judge_win_loss break unless $game_troop.interpreter.running? update_for_wait end end
It also checks if victory or defeat should come.

process_action under Scene_Battle is the method that processes all action slots of all alive battlers:

Spoiler
#-------------------------------------------------------------------------- # * Battle Action Processing #-------------------------------------------------------------------------- def process_action return if scene_changing? if !@subject || [email protected]_action @subject = BattleManager.next_subject end return turn_end unless @subject if @subject.current_action @subject.current_action.prepare if @subject.current_action.valid? @status_window.open execute_action end @subject.remove_current_action end process_action_end unless @subject.current_action end
@subject is the action execution subject which is the battler currently processing his/her/its actions.

If there's no action execution subject or that subject has no unprocessed actions(otherwise that subject will be unchanged), the subject be set as the most up front battler in the action execution order queue(A FIFO data structure), if there's any(otherwise the Action Execution Phase will end):

Spoiler
#-------------------------------------------------------------------------- # * Get Next Action Subject # Get the battler from the beginning of the action order list. # If an actor not currently in the party is obtained (occurs when index # is nil, immediately after escaping in battle events etc.), skip them. #-------------------------------------------------------------------------- def self.next_subject loop do battler = @action_battlers.shift return nil unless battler next unless battler.index && battler.alive? return battler end end
The most up front battler will be removed from @action_battlers(recall that the faster the battler, the more up front that battler will be in that queue), and return that battler if it's alive. This process will be repeated until an alive battler's found, and that battler will be returned. If no such battler's found, nil will be returned instead.

If there's already an action execution subject having unprocessed actions, that battler will continue to process actions until they're all processed. Only valid actions will be executed via execute_action.

When all actions of the action execution subject's processed, process_action_end will be called:

Spoiler
#-------------------------------------------------------------------------- # * Processing at End of Action #-------------------------------------------------------------------------- def process_action_end @subject.on_action_end refresh_status @log_window.display_auto_affected_status(@subject) @log_window.wait_and_clear @log_window.display_current_state(@subject) @log_window.wait_and_clear BattleManager.judge_win_loss end
BattleManager.judge_win_loss checks if victory or defeat should come:

Spoiler
#-------------------------------------------------------------------------- # * Determine Win/Loss Results #-------------------------------------------------------------------------- def self.judge_win_loss if @phase return process_abort if $game_party.members.empty? return process_defeat if $game_party.all_dead? return process_victory if $game_troop.all_dead? return process_abort if aborting? end return false end
As long as the battle isn't already about to be ended:

It simply abort the battle if it's explicitly called to be aborted or no party member remains in the battle.

It ends the battle with defeat if all party members in the battle are dead.

It ends the battle with victory if all enemies are dead.

If the battle's to be ended, it returns true, otherwise it returns false.

When the Action Execution Phase ends without ending the battle, turn_end under Scene_Battle will be called to transit the battle to Action Input Phase:

Spoiler
#-------------------------------------------------------------------------- # * End Turn #-------------------------------------------------------------------------- def turn_end all_battle_members.each do |battler| battler.on_turn_end refresh_status @log_window.display_auto_affected_status(battler) @log_window.wait_and_clear end BattleManager.turn_end process_event start_party_command_selection end
All effects triggered on all battlers on turn end, and all battle and common events triggered on turn end will be triggered now.

The party command window will be shown, after calling BattleManager.turn_end, which changes the phase to turn end.


That's all for now. I hope this can help you grasp these more advanced stuffs. For those comprehending the default RMVXA battle flow implementations, feel free to correct me if there's anything wrong 😀

Original thread: https://forums.rpgmakerweb.com/threads/solid-understanding-to-the-default-rmvxa-battle-flow-implementations.42342/