VXAce Yami's Invert Target script issue

I use Yami's Invert Targets script in my game, and ran into a problem I have no idea what to do about (not being a scripter). It happens when an actor has more than one action per turn. For example, you choose to use an item, which is usually used on allies, on the enemy. The target is inverted, action 1 is confirmed. Then there is action 2, let's say you choose to guard. The result is that the item is not used on the enemy, but on an ally.

I tried various combinations, and it seems the second choice determines the outcome. So if I chose to use one item on enemy and one on user, the first was used on the first party member, and the second on the user. And if I chose one item on user and one on enemy, both went to the same enemy. Same happens with attack (and probably magic/special attacks, I haven't tried those).

Is there any way to remedy this? I'm not sure what other info might be relevant (maybe that I use Yami's Battle Symphony, I don't think I have any other scripts that would influence it). I understand that this might be a big ask, but maybe someone is familiar with the script, or had a similar problem in the past that they successfully dealt with and can help me. I would enormously appreciate that.
I tried various combinations, and it seems the second choice determines the outcome. So if I chose to use one item on enemy and one on user, the first was used on the first party member, and the second on the user. And if I chose one item on user and one on enemy, both went to the same enemy. Same happens with attack (and probably magic/special attacks, I haven't tried those).
As you have guessed, the last action determines if the actions were inverted or not. This behavior happens even without any battle script, so this is definitely a bug.

Here is a fix I wrote, I haven't tested all possibilities but it should hold. Originally the inverted property was stored in the actor, here I chose to save it in each action when the target is confirmed.

Ruby:
#==============================================================================
# Yami Invert Target multiactions fix
#------------------------------------------------------------------------------
# Author: Timtrack
# date: 26/08/2025
# Requires: YSA Battle Add-On: Invert Targets
#==============================================================================
# Description: fixes a bug with Yami Invert Target script that was not working
# properly with actors having multiple actions: the 'inversion' picked was
# the inversion boolean of the last action
#==============================================================================
# Installation: plug and play, put it below Yami's script
#==============================================================================

$imported = {} if $imported.nil?
$imported["TIM-YSA-InvertTargets-fix"] = true

if $imported["YSA-InvertTargets"]
  
#==============================================================================
# Game_Action
#==============================================================================

class Game_Action
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :invert_target
  #--------------------------------------------------------------------------
  # new method: invert_target?
  #--------------------------------------------------------------------------
  def invert_target?
    @invert_target
  end
  #--------------------------------------------------------------------------
  # overwrite method: friends_unit -> cancels Yami's behavior and links 
  # to action's inverted property
  #--------------------------------------------------------------------------
  def friends_unit
    invert_target? ? subject.opponents_unit : invert_target_friends_unit
  end
  #--------------------------------------------------------------------------
  # overwrite method: opponents_unit -> cancels Yami's behavior and links 
  # to action's inverted property
  #--------------------------------------------------------------------------
  def opponents_unit
    invert_target? ? subject.friends_unit : invert_target_opponents_unit
  end
  #--------------------------------------------------------------------------
  # alias method: clear -> remove invert_target attribute
  #--------------------------------------------------------------------------
  alias invert_target_clear clear
  def clear
    invert_target_clear
    @invert_target = false
  end   
end # Game_Action

#==============================================================================
# Scene_Battle
#==============================================================================

class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # alias method: on_enemy_ok -> saves the inverted status into the action
  #--------------------------------------------------------------------------
  alias invert_target_on_enemy_ok on_enemy_ok
  def on_enemy_ok
    BattleManager.actor.input.invert_target = BattleManager.actor.invert_target
    invert_target_on_enemy_ok
  end
  #--------------------------------------------------------------------------
  # alias method: on_actor_ok -> saves the inverted status into the action
  #--------------------------------------------------------------------------
  alias invert_target_on_actor_ok on_actor_ok
  def on_actor_ok
    BattleManager.actor.input.invert_target = BattleManager.actor.invert_target
    invert_target_on_actor_ok
  end
end # Scene_Battle
  
  
end #$imported["YSA-InvertTargets"]
Thank you so much! I tried it out on item usage and it works (will have to test all other possibilities, but now it's bedtime :LZSsmile:). You're a lifesaver.

Original thread: https://forums.rpgmakerweb.com/threads/yamis-invert-target-script-issue.179706/