How do I prevent the player from backing out after a specific battle action is used?

Hi,

I've been hitting some trouble with trying to get Yanfly's Command Party script to work cleanly as something meant for the Actor Command window, rather than the Party Command window. While my tweaked version of the script works correctly, it has some flaws that I'm not sure how to troubleshoot.

Here is a Pastebin of my copy: Link.

I have successfully managed to get it to respond correctly to backing out of the menu when there are no changes to the party lineup (that's what the Scene_Battle.prior_command is for). However, I can still hit back and return to the character that I successfully (meaning I made changes) used Switch on, essentially giving them two turns.

TL;DR: How do I prevent the player from hitting back after a specific command is used, and not cancelled?

Additionally, if there's any way to make it so the game checks for alive party members waiting in the wings and swaps them out for dead ones, that would be great.
This partly works.
I can't see a way to control blocking the commands if the actor was swapped out.
But I'll work on it more later.

*update - almost perfect, I think.
Spoiler: block action on swap
Ruby:
module R2_Battle_Swap
  Var = 11
end

module BattleManager
  def self.next_command
    begin
      if !actor || !actor.next_command
        loop do
          @actor_index += 1
          break if @actor_index >= $game_party.members.size
          break if $game_variables[R2_Battle_Swap::Var] == 0
          nextact = $game_party.battle_members[@actor_index].id
          if $game_variables[R2_Battle_Swap::Var] != 0 &&
            !$game_variables[R2_Battle_Swap::Var].include?(nextact)
            break
          end
          break if @actor_index >= $game_party.members.size
        end
        return false if @actor_index >= $game_party.members.size
      end
    end until actor.inputable?
    return true
  end
  def self.prior_command
    begin
      if !actor || !actor.prior_command
        loop do
          @actor_index -= 1
          prevact = $game_party.battle_members[@actor_index].id
          if $game_variables[R2_Battle_Swap::Var] != 0 &&
            $game_variables[R2_Battle_Swap::Var].include?(prevact)
            @actor_index += 1
            break
          end
          break if @actor_index < 0
        end
        return false if @actor_index < 0
      end
    end until actor.inputable?
    return true
  end
end

class Scene_Battle < Scene_Base
  def command_party
    $game_variables[R2_Battle_Swap::Var] = []
    Graphics.freeze
    @info_viewport.visible = false
    hide_extra_gauges if $imported["YEA-BattleEngine"]
    SceneManager.snapshot_for_background
    previous_party = $game_party.battle_members.clone
    index = @party_command_window.index
    oy = @party_command_window.oy
    #---
    SceneManager.call(Scene_Party)
    SceneManager.scene.main
    SceneManager.force_recall(self)
    #---
    show_extra_gauges if $imported["YEA-BattleEngine"]
    if previous_party != $game_party.battle_members
      $game_party.battle_members.each { |a|
        $game_variables[R2_Battle_Swap::Var].push(a.id) if !previous_party.include?(a)
      }
      $game_party.set_party_cooldown
    else
      BattleManager.prior_command
    end
    @info_viewport.visible = true
    @status_window.refresh
    @party_command_window.setup
    @party_command_window.select(index)
    @party_command_window.oy = oy
    perform_transition
  end
  alias r2_battle_start_reset_swap_var  battle_start
  def battle_start
    $game_variables[R2_Battle_Swap::Var] = []
    r2_battle_start_reset_swap_var
  end
  alias r2_battle_switch_reset_var_turn_end  turn_end
  def turn_end
    $game_variables[R2_Battle_Swap::Var] = []
    r2_battle_switch_reset_var_turn_end
  end
end

Additionally, if there's any way to make it so the game checks for alive party members waiting in the wings and swaps them out for dead ones, that would be great.

Do you mean automatically or when you access the party scene?
Automatically at the end of a turn or the start? not sure what you want.
This partly works.
I can't see a way to control blocking the commands if the actor was swapped out.
But I'll work on it more later.

*update - almost perfect, I think.
Spoiler: block action on swap
Ruby:
module R2_Battle_Swap
  Var = 11
end

module BattleManager
  def self.next_command
    begin
      if !actor || !actor.next_command
        loop do
          @actor_index += 1
          break if @actor_index >= $game_party.members.size
          break if $game_variables[R2_Battle_Swap::Var] == 0
          nextact = $game_party.battle_members[@actor_index].id
          if $game_variables[R2_Battle_Swap::Var] != 0 &&
            !$game_variables[R2_Battle_Swap::Var].include?(nextact)
            break
          end
          break if @actor_index >= $game_party.members.size
        end
        return false if @actor_index >= $game_party.members.size
      end
    end until actor.inputable?
    return true
  end
  def self.prior_command
    begin
      if !actor || !actor.prior_command
        loop do
          @actor_index -= 1
          prevact = $game_party.battle_members[@actor_index].id
          if $game_variables[R2_Battle_Swap::Var] != 0 &&
            $game_variables[R2_Battle_Swap::Var].include?(prevact)
            @actor_index += 1
            break
          end
          break if @actor_index < 0
        end
        return false if @actor_index < 0
      end
    end until actor.inputable?
    return true
  end
end

class Game_Actor
  attr_accessor   :actor_id
end

class Scene_Battle < Scene_Base
  def command_party
    $game_variables[R2_Battle_Swap::Var] = []
    Graphics.freeze
    @info_viewport.visible = false
    hide_extra_gauges if $imported["YEA-BattleEngine"]
    SceneManager.snapshot_for_background
    previous_party = $game_party.battle_members.clone
    index = @party_command_window.index
    oy = @party_command_window.oy
    #---
    SceneManager.call(Scene_Party)
    SceneManager.scene.main
    SceneManager.force_recall(self)
    #---
    show_extra_gauges if $imported["YEA-BattleEngine"]
    if previous_party != $game_party.battle_members
      $game_party.battle_members.each { |a|
        $game_variables[R2_Battle_Swap::Var].push(a.id) if !previous_party.include?(a)
      }
      $game_party.set_party_cooldown
    else
      BattleManager.prior_command
    end
    @info_viewport.visible = true
    @status_window.refresh
    @party_command_window.setup
    @party_command_window.select(index)
    @party_command_window.oy = oy
    perform_transition
  end
  alias r2_battle_start_reset_swap_var  battle_start
  def battle_start
    $game_variables[R2_Battle_Swap::Var] = []
    r2_battle_start_reset_swap_var
  end
  alias r2_battle_switch_reset_var_turn_end  turn_end
  def turn_end
    $game_variables[R2_Battle_Swap::Var] = []
    r2_battle_switch_reset_var_turn_end
  end
end

Additionally, if there's any way to make it so the game checks for alive party members waiting in the wings and swaps them out for dead ones, that would be great.

Do you mean automatically or when you access the party scene?
Automatically at the end of a turn or the start? not sure what you want.
I'd love a bit of documentation in regards to how you're doing this, because I'd rather learn than punch stuff in and not know why it works. No pressure, of course. Thank you for what you've done already.

I mean as soon as they go down, much like how Pokémon will go "MON fainted!" and instantly pull up the screen to select a non-fainted party member.
documentation in regards to how you're doing this,
You want every step that the script does explained? I suppose but unless you know the code then it wouldn't matter. But if you knew the code, it would not be too hard to follow along just by reading the lines. If you want to make modifications, go ahead.
But basically I had to modify the next command and previous command. I injected a variable to control the data for what actors were swapped out and to check that list when processing commands. Then resetting the variable at battle start and turn end.
You want every step that the script does explained? I suppose but unless you know the code then it wouldn't matter. But if you knew the code, it would not be too hard to follow along just by reading the lines. If you want to make modifications, go ahead.
But basically I had to modify the next command and previous command. I injected a variable to control the data for what actors were swapped out and to check that list when processing commands. Then resetting the variable at battle start and turn end.
Just the gist of it, like you did, is *perfect*. Sometimes these things fly over my head and I need plain text to understand.

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-prevent-the-player-from-backing-out-after-a-specific-battle-action-is-used.148056/