Altering initial enemy selection to default to the last target selected

Hi all,

Ordinarily, when more than one enemy is present on the battlefield, enemy A (occupying the first troop position), is always initially highlighted when choosing an attack. I'd like it so if there's two enemies, enemy A and enemy B, and the player has just previously attacked enemy B, initial selection favors enemy B.

Not sure it matters since it doesn't change the order of selection, but I am using Yanfly's battle engine
Would this do?
I have tried it for both vanilla rpg maker and vanilla + YBE, but I am not sure about the behavior for skills with random targeting or all targeting, could you test it?

And yes, using Yanfly's battle engine matters as it changes Window_BattleEnemy (the window used to selecte enemies) 🙂

Ruby:
#==============================================================================
# Last enemy select
#------------------------------------------------------------------------------
# Author: Timtrack (no need for credit)
# date: 21/03/2025
#==============================================================================
# The enemy window will save the last enemy targeted for future uses,
# if the enemy is not in the list anymore, the window goes back to the first enemy
#==============================================================================
class Window_BattleEnemy < Window_Selectable
  #--------------------------------------------------------------------------
  # * alias method: hide
  #--------------------------------------------------------------------------
  alias hide_wbe_tles hide
  def hide
    @last_enemy = enemy
    hide_wbe_tles
  end
 
if $imported["YEA-BattleEngine"]
  #--------------------------------------------------------------------------
  # * new method: select_last_enemy
  #--------------------------------------------------------------------------
  def select_last_enemy
    i = @data.index(@last_enemy)
    i = 0 if i.nil?
    select(i)
  end
  #--------------------------------------------------------------------------
  # * alias method: create_flags
  #--------------------------------------------------------------------------
  alias create_flags_wbe_tles create_flags
  def create_flags
    create_flags_wbe_tles
    select_last_enemy
  end
else #vanilla rpg maker
  #--------------------------------------------------------------------------
  # * new method: select_last_enemy
  #--------------------------------------------------------------------------
  def select_last_enemy
    i = $game_troop.alive_members.index(@last_enemy)
    i = 0 if i.nil?
    select(i)
  end
  #--------------------------------------------------------------------------
  # * overwrite method: show
  #--------------------------------------------------------------------------
  def show
    if @info_viewport
      width_remain = Graphics.width - width
      self.x = width_remain
      @info_viewport.rect.width = width_remain
      select_last_enemy #select(0)
    end
    super
  end
end #$imported["YEA-BattleEngine"]
end #Window_BattleEnemy
Would this do?
I have tried it for both vanilla rpg maker and vanilla + YBE, but I am not sure about the behavior for skills with random targeting or all targeting, could you test it?

Tried testing with all kinds of random target, doesn't seem like it's consistent with whether the enemy that was randomly attacked is highlighted in your next turn. Also fighting against more enemies than two seems to throw the script off a little bit it seems.

Other than that it works great when against two enemies using regular attacks, definitely on the right track
doesn't seen like it's consistent with whether the enemy that was randomly attacked is highlighted in your next turn
That is because the random targets are chosen when the action will be performed (see method use_item in Scene_Battle), so my change to the window does not catch them.

For information, what I did was simply catch the last enemy 'targeted' in the selection window and retrieve it the next time the window is opened. Skills/Items that target all/random_targets do not actually choose an enemy, so the last target in that case is probably the last 'single' target.
Also, this window is not specific to an actor, so if actor1 chooses enemy2 then actor2's window will select enemy2, but if you change it to enemy1, then the next time the window is opened (say, for actor3 or actor1 next turn), it will select enemy1.

What you want seems to be a saved selection per actor that might be updated by random target skills instead, this would be a bit longer to write.

Also fighting against more enemies than two seems to throw the script off a little bit it seems.
Can you be more precise? I've tested it on groups with 4 or even more enemies and I don't see any issue.
You're right, it seems like the error I was having comes from a problem with a totally different script, Hime's untargetable state, not this one.

Unrelated to that, would it be possible to attach a script call to this script to manually change which enemy/troop index the player is targeting in battle? By that I mean dynamically in battle, something for example like


Turn 1 - player.target.enemy(0)

Turn 3 - player.target.enemy(1)
I updated the script, now you may use script call:
target_enemy(i)
To target enemy with index i in the troop

Ruby:
#==============================================================================
# Last enemy select
#------------------------------------------------------------------------------
# Author: Timtrack (no need for credit)
# date: 23/03/2025
#==============================================================================
# The enemy window will save the last enemy targeted for future uses,
# if the enemy is not in the list anymore, the window goes back to the first enemy
#
# You may set the enemy targeted by calling
# target_enemy(i)
# with i the index of the enemy in the troop
#==============================================================================
class Game_Interpreter
  #--------------------------------------------------------------------------
  # * new method: target_enemy
  #--------------------------------------------------------------------------
  def target_enemy(index)
    SceneManager.scene.enemy_window.select_by_index(index) if SceneManager.scene_is?(Scene_Battle)
  end
end

class Scene_Battle < Scene_Base
  attr_reader :enemy_window #to access the enemy window
end

class Window_BattleEnemy < Window_Selectable
  #--------------------------------------------------------------------------
  # * alias method: hide
  #--------------------------------------------------------------------------
  alias hide_wbe_tles hide
  def hide
    @last_enemy = enemy
    hide_wbe_tles
  end
 
  #--------------------------------------------------------------------------
  # * new method: select_last_enemy
  #--------------------------------------------------------------------------
  def select_last_enemy
    i = enemy_list.index(@last_enemy)
    i = 0 if i.nil?
    select(i)
  end
 
  #--------------------------------------------------------------------------
  # * new method: select_by_index
  #--------------------------------------------------------------------------
  def select_by_index(enemy_index)
    i = enemy_list.index {|en| en.index == enemy_index}
    if i
      @last_enemy = enemy_list[i]
      select(i)
    end
  end
 
if $imported["YEA-BattleEngine"]
  #--------------------------------------------------------------------------
  # * new method: enemy_list
  #--------------------------------------------------------------------------
  def enemy_list
    @data
  end
  #--------------------------------------------------------------------------
  # * alias method: create_flags
  #--------------------------------------------------------------------------
  alias create_flags_wbe_tles create_flags
  def create_flags
    create_flags_wbe_tles
    select_last_enemy
  end
else #vanilla rpg maker
  #--------------------------------------------------------------------------
  # * new method: enemy_list
  #--------------------------------------------------------------------------
  def enemy_list
    $game_troop.alive_members
  end
  #--------------------------------------------------------------------------
  # * overwrite method: show
  #--------------------------------------------------------------------------
  def show
    if @info_viewport
      width_remain = Graphics.width - width
      self.x = width_remain
      @info_viewport.rect.width = width_remain
      select_last_enemy #select(0)
    end
    super
  end
end #$imported["YEA-BattleEngine"]
end #Window_BattleEnemy
Thank you, working perfectly

Original thread: https://forums.rpgmakerweb.com/threads/altering-initial-enemy-selection-to-default-to-the-last-target-selected.176425/