Crash with Hime Untargetable State whenever I use an item! Please Help!

I know that this has been posted before but my issue is different.

I already applied the fix outlined here: https://forums.rpgmakerweb.com/inde...th-himes-untargetable-state-script-ace.68964/

and here:
But I am STILL crashing with error whenever I use an item
Capture.JPG

This is the script that I am using
Code:
=begin
#===============================================================================
 Title: Untargetable State
 Author: Hime
 Date: Sep 25, 2015
--------------------------------------------------------------------------------
 ** Change log
 Sep 25, 2015
   - when selecting actors, the correct actor index needs to be provided
 Oct 17, 2013
   - compatiblity patch with yanfly's Ace Battle Engine
 Jun 7, 2013
   - bug fix: game crashes when there are no valid targets
 May 29, 2013
   - fixed bug where enemies that are hidden appear in the target window
 May 25, 2013
   - added support for untargetable actors
   - Initial release
--------------------------------------------------------------------------------   
 ** Terms of Use
 * Free to use in non-commercial projects
 * Contact me for commercial use
 * No real support. The script is provided as-is
 * Will do bug fixes, but no compatibility patches
 * Features may be requested but no guarantees, especially if it is non-trivial
 * Credits to Hime Works in your project
 * Preserve this header
--------------------------------------------------------------------------------
 ** Description
 
 This script allows you to create a state that prevents you from targeting
 a battler. When the state is applied, the battler cannot be targeted or
 affected by any skills or items.
 
--------------------------------------------------------------------------------
 ** Installation
 
 Place this script below Materials and above Main
 
--------------------------------------------------------------------------------
 ** Usage
 
 In the configuration below, enter the ID's of all states that should have the
 untargetable property
 
--------------------------------------------------------------------------------
 ** Compatibility
 
 This script overwrites the following
 
   Window_BattleEnemy
     item_max
     enemy
     draw_item
 
#===============================================================================
=end
$imported = {} if $imported.nil?
$imported["TH_UntargetableState"] = true
#===============================================================================
# ** Configuration
#===============================================================================
module TH
  module Untargetable_State
    
    # List of state ID's that will have untargetable effect
    States = [20]
  end
end
#===============================================================================
# ** Rest of script
#===============================================================================
class Game_Action
 
  alias :th_untargetable_state_make_targets :make_targets
  def make_targets
    th_untargetable_state_make_targets.select {|target| target && target.can_target? }
  end
end

class Game_Battler < Game_BattlerBase
  def can_target?
    return false if untargetable_state?
        return false unless exist?
    return true
  end
 
  #-----------------------------------------------------------------------------
  # Check if untargetable state is applied
  #-----------------------------------------------------------------------------
  def untargetable_state?
    !(@states & TH::Untargetable_State::States).empty?
  end
end

class Game_Enemy < Game_Battler
 
  #-----------------------------------------------------------------------------
  # Lots of hardcoded conditions based on default scripts
  #-----------------------------------------------------------------------------
  alias :th_untargetable_state_can_target? :can_target?
  def can_target?
    return false if dead?
    th_untargetable_state_can_target?
  end
end

class Game_Unit
 
  #-----------------------------------------------------------------------------
  # New. Return an array of battlers that can be targeted
  #-----------------------------------------------------------------------------
  def targetable_members
    members.select {|member| member.can_target? }
  end
end

#-------------------------------------------------------------------------------
# Updated to only draw enemies that can be selected.
# Basically replaces everything
#-------------------------------------------------------------------------------
class Window_BattleEnemy < Window_Selectable
  def item_max
    $game_troop.targetable_members.size
  end
 
  def enemy
    $game_troop.targetable_members[@index]
  end
 
  def draw_item(index)
    change_color(normal_color)
    name = $game_troop.targetable_members[index].name
    draw_text(item_rect_for_text(index), name)
  end
 
  alias :th_untargetable_state_current_item_enabled? :current_item_enabled?
  def current_item_enabled?
    return false if $game_troop.targetable_members[@index].nil?
    th_untargetable_state_current_item_enabled?
  end
end

#-------------------------------------------------------------------------------
# Updated to only draw actors that can be selected.
# Basically replaces everything
#-------------------------------------------------------------------------------
class Window_BattleActor < Window_BattleStatus
 
  def actor_index
    $game_party.targetable_members[@index].ID
  end
 
  def item_max
    $game_party.targetable_members.size
  end
 
  def draw_item(index)
    actor = $game_party.targetable_members[index]
    draw_basic_area(basic_area_rect(index), actor)
    draw_gauge_area(gauge_area_rect(index), actor)
  end
 
  alias :th_untargetable_state_current_item_enabled? :current_item_enabled?
  def current_item_enabled?
    return false if $game_party.targetable_members[index].nil?
    th_untargetable_state_current_item_enabled?
  end
end


class Scene_Battle < Scene_Base
 
  # Overwrite. Need the proper index of the selected actor.
  def on_actor_ok
    BattleManager.actor.input.target_index = @actor_window.actor_index
    @actor_window.hide
    @skill_window.hide
    @item_window.hide
    next_command
  end
end

if $imported["YEA-BattleEngine"]
  class Window_BattleActor < Window_BattleStatus
    def draw_item(index)
      return if index.nil?
      clear_item(index)
      actor = $game_party.targetable_members[index] ### changed this
      rect = item_rect(index)
      return if actor.nil?
      draw_actor_face(actor, rect.x+2, rect.y+2, actor.alive?)
      draw_actor_name(actor, rect.x, rect.y, rect.width-8)
      draw_actor_action(actor, rect.x, rect.y)
      draw_actor_icons(actor, rect.x, line_height*1, rect.width)
      gx = YEA::BATTLE::BATTLESTATUS_HPGAUGE_Y_PLUS
      contents.font.size = YEA::BATTLE::BATTLESTATUS_TEXT_FONT_SIZE
      draw_actor_hp(actor, rect.x+2, line_height*2+gx, rect.width-4)
      if draw_tp?(actor) && draw_mp?(actor)
        dw = rect.width/2-2
        dw += 1 if $imported["YEA-CoreEngine"] && YEA::CORE::GAUGE_OUTLINE
        draw_actor_tp(actor, rect.x+2, line_height*3, dw)
        dw = rect.width - rect.width/2 - 2
        draw_actor_mp(actor, rect.x+rect.width/2, line_height*3, dw)
      elsif draw_tp?(actor) && !draw_mp?(actor)
        draw_actor_tp(actor, rect.x+2, line_height*3, rect.width-4)
      else
        draw_actor_mp(actor, rect.x+2, line_height*3, rect.width-4)
      end
    end
  end
end

The error seems to be coming from the same line that the other thread said was the fix

Does anybody know what the problem could be and how to fix it?
ID should be id - lower case
I changed it to lower case and now the item is being used on a different party member than what I selected every time.
You gotta have edited the script to make it look like the one below...

Ruby:
class Window_BattleActor < Window_BattleStatus
 
  def actor_index
    $game_party.targetable_members[@index].id
  end

Add a test by adding this command:

Ruby:
class Window_BattleActor < Window_BattleStatus
  def actor_index
    msgbox @index
    $game_party.targetable_members[@index].id
  end

@index should include any value from 0 through 3 if there are just 4 party members available in battle. 0 stands for the 1st hero, etc. unless they cannot be picked (untargetable).

Does the value of @index match the position where the cursor was during the target selection stage or not?
hmmm ... actor_index should be returning the position of the actor, but in the above it's returning the actor id. So if you had actor 1 in position 3, actor_index would return 1 rather than 3. This doesn't seem right.

I don't think it's correct to use .id at all.

Unfortunately I'm not familiar with this script and I don't have time to look at it this morning. Hopefully someone else will be able to look at it with my thoughts in mind and figure it out for you.

Moved to: RGSSx Script Support
If the script needs the actor position (which it could - depends on how what index BattleManager.actor.input.target_index is using), then the script could be changed to
Ruby:
  def actor_index
    @index
  end
This scriptlet seems to work with the default battle system so feel free to try it in your game project.
Set the value of the STATE Constant to your Untargetable State ID.

Look for KBlockStates ACE script instead!

Outdated Version of the Script

Ruby:
# * KBlockTarget ACE * #
#   Scripter : Kyonides Arkanthes
#   2023-04-30

# This script prevents you or your enemy from choosing a target that is still
# afflicted by a Blocked Target alias Untargetable State.

# * Script Calls to Apply the Block Target State Randomly * #

# $game_party.apply_block_state_randomly
# $game_troop.apply_block_state_randomly - In Battle

# * New Methods:
#   Game_Battler#block_target?
#   Game_Unit#apply_block_state_randomly - For Party & Troop
#   Game_Party#targetable_battle_members

# * Overwritten Methods * #
# In Game_Unit class:
#   random_target, random_dead_target, smooth_target, smooth_dead_target

# In Game_Action class:
#   targets_for_opponents, targets_for_friends, item_target_candidates

# In Window_BattleActor class:
#   item_max, draw_item

# In Window_BattleEnemy class:
#   item_max, enemy

module KBlockTarget
  STATE = 26 # Pick your favorite Blocked Target State ID
end

class Game_Battler
  def block_target?
    state?(KBlockTarget::STATE)
  end
end

class Game_Unit
  def apply_block_state_randomly
    state_id = KBlockTarget::STATE
    team = members
    pos = rand(team.size)
    this_member = team[pos]
    this_member.add_state(state_id) unless this_member.nil?
  end

  def targetable_alive_members
    alive_members.reject{|m| m.block_target? }
  end

  def targetable_dead_members
    dead_members.reject{|m| m.block_target? }
  end

  def random_target
    tgr_rand = rand * tgr_sum
    group = targetable_alive_members
    group.each do |member|
      tgr_rand -= member.tgr
      return member if tgr_rand < 0
    end
    group[0]
  end

  def random_dead_target
    group = targetable_dead_members
    group.empty? ? nil : group[rand(group.size)]
  end

  def smooth_target(index)
    group = targetable_alive_members
    member = group[index]
    (member && member.alive?) ? member : group[0]
  end

  def smooth_dead_target(index)
    group = targetable_dead_members
    member = group[index]
    (member && member.dead?) ? member : group[0]
  end
end

class Game_Party
  def targetable_battle_members
    group = all_members[0, max_battle_members]
    group.select {|actor| actor.exist? and !actor.block_target? }
  end
end

class Game_Action
  def targets_for_opponents
    if item.for_random?
      Array.new(item.number_of_targets) { opponents_unit.random_target }
    elsif item.for_one?
      num = 1 + (attack? ? subject.atk_times_add.to_i : 0)
      if @target_index < 0
        [opponents_unit.random_target] * num
      else
        [opponents_unit.smooth_target(@target_index)] * num
      end
    else
      opponents_unit.targetable_alive_members
    end
  end

  def targets_for_friends
    if item.for_user?
      [subject]
    elsif item.for_dead_friend?
      if item.for_one?
        [friends_unit.smooth_dead_target(@target_index)]
      else
        friends_unit.targetable_dead_members
      end
    elsif item.for_friend?
      if item.for_one?
        [friends_unit.smooth_target(@target_index)]
      else
        friends_unit.targetable_alive_members
      end
    end
  end

  def item_target_candidates
    if item.for_opponent?
      opponents_unit.targetable_alive_members
    elsif item.for_user?
      [subject]
    elsif item.for_dead_friend?
      friends_unit.targetable_dead_members
    else
      friends_unit.targetable_alive_members
    end
  end
end

class Window_BattleActor
  def item_max
    $game_party.targetable_battle_members.size
  end

  def draw_item(index)
    actor = $game_party.targetable_battle_members[index]
    draw_basic_area(basic_area_rect(index), actor)
    draw_gauge_area(gauge_area_rect(index), actor)
  end
end

class Window_BattleEnemy
  def item_max
    $game_troop.targetable_alive_members.size
  end

  def enemy
    $game_troop.targetable_alive_members[@index]
  end
end
This scriptlet seems to work with the default battle system so feel free to try it in your game project.
Set the value of the STATE Constant to your Untargetable State ID.

Ruby:
# * KBlockTarget ACE * #
#   Scripter : Kyonides Arkanthes
#   2023-04-30

# This script prevents you or your enemy from choosing a target that is still
# afflicted by a Blocked Target alias Untargetable State.

# * Script Calls to Apply the Block Target State Randomly * #

# $game_party.apply_block_state_randomly
# $game_troop.apply_block_state_randomly - In Battle

# * New Methods:
#   Game_Battler#block_target?
#   Game_Unit#apply_block_state_randomly - For Party & Troop
#   Game_Party#targetable_battle_members

# * Overwritten Methods * #
# In Game_Unit class:
#   random_target, random_dead_target, smooth_target, smooth_dead_target

# In Game_Action class:
#   targets_for_opponents, targets_for_friends, item_target_candidates

# In Window_BattleActor class:
#   item_max, draw_item

# In Window_BattleEnemy class:
#   item_max, enemy

module KBlockTarget
  STATE = 26 # Pick your favorite Blocked Target State ID
end

class Game_Battler
  def block_target?
    state?(KBlockTarget::STATE)
  end
end

class Game_Unit
  def apply_block_state_randomly
    state_id = KBlockTarget::STATE
    team = members
    pos = rand(team.size)
    this_member = team[pos]
    this_member.add_state(state_id) unless this_member.nil?
  end

  def targetable_alive_members
    alive_members.reject{|m| m.block_target? }
  end

  def targetable_dead_members
    dead_members.reject{|m| m.block_target? }
  end

  def random_target
    tgr_rand = rand * tgr_sum
    group = targetable_alive_members
    group.each do |member|
      tgr_rand -= member.tgr
      return member if tgr_rand < 0
    end
    group[0]
  end

  def random_dead_target
    group = targetable_dead_members
    group.empty? ? nil : group[rand(group.size)]
  end

  def smooth_target(index)
    group = targetable_alive_members
    member = group[index]
    (member && member.alive?) ? member : group[0]
  end

  def smooth_dead_target(index)
    group = targetable_dead_members
    member = group[index]
    (member && member.dead?) ? member : group[0]
  end
end

class Game_Party
  def targetable_battle_members
    group = all_members[0, max_battle_members]
    group.select {|actor| actor.exist? and !actor.block_target? }
  end
end

class Game_Action
  def targets_for_opponents
    if item.for_random?
      Array.new(item.number_of_targets) { opponents_unit.random_target }
    elsif item.for_one?
      num = 1 + (attack? ? subject.atk_times_add.to_i : 0)
      if @target_index < 0
        [opponents_unit.random_target] * num
      else
        [opponents_unit.smooth_target(@target_index)] * num
      end
    else
      opponents_unit.targetable_alive_members
    end
  end

  def targets_for_friends
    if item.for_user?
      [subject]
    elsif item.for_dead_friend?
      if item.for_one?
        [friends_unit.smooth_dead_target(@target_index)]
      else
        friends_unit.targetable_dead_members
      end
    elsif item.for_friend?
      if item.for_one?
        [friends_unit.smooth_target(@target_index)]
      else
        friends_unit.targetable_alive_members
      end
    end
  end

  def item_target_candidates
    if item.for_opponent?
      opponents_unit.targetable_alive_members
    elsif item.for_user?
      [subject]
    elsif item.for_dead_friend?
      friends_unit.targetable_dead_members
    else
      friends_unit.targetable_alive_members
    end
  end
end

class Window_BattleActor
  def item_max
    $game_party.targetable_battle_members.size
  end

  def draw_item(index)
    actor = $game_party.targetable_battle_members[index]
    draw_basic_area(basic_area_rect(index), actor)
    draw_gauge_area(gauge_area_rect(index), actor)
  end
end

class Window_BattleEnemy
  def item_max
    $game_troop.targetable_alive_members.size
  end

  def enemy
    $game_troop.targetable_alive_members[@index]
  end
end
Do I just replace the Hime one completely, or do I add to it or have both scripts?
Well, my script is a stand alone one so yes, you'd have to replace Hime's with mine.
If you're using a custom battle system, I'd have to know that in advance to be able to adapt it to that script if needed. Even so, test it as it is right now and don't forget to come back to provide me with some useful feedback.
Well, my script is a stand alone one so yes, you'd have to replace Hime's with mine.
If you're using a custom battle system, I'd have to know that in advance to be able to adapt it to that script if needed. Even so, test it as it is right now and don't forget to come back to provide me with some useful feedback.


Your script has the SAME issue as Hime's! Items are being used on the WRONG characters!
Not at all for it won't even let you pick the one that has been excluded. Anyway, I won't try to help you from now on.
Your script has the SAME issue as Hime's! Items are being used on the WRONG characters!
It is much more likely that you have something else that is conflicting.
Make a new project and set it up with the script and share that for some of us to look at.
It is much more likely that you have something else that is conflicting.
Make a new project and set it up with the script and share that for some of us to look at.


No, because removing his script fixes the issue.

I select an item, choose the third character from the list, and the game uses the item on the second character in the list instead—JUST like Hime's script which if you look at the page for Hime's script, and do a few Google searches, you'll find that other people are having the same issue with Hime's script.


This bug is also present in kyonides's script

And kyonides, I NEVER said it is letting me choose a character that is not targetable. Items are just being used on a different character than what I selected—even when nobody is in an untargetable state


The only other custom script I have is a terrain generator and that would not conflict with something like this
OK so that was an easy test. My suggestion above is almost correct.
when using items and an actor is inflicted with the untargetable state the actor selected will not be the one it is used on. Hold on.

Got it.
Ruby:
class Window_BattleActor < Window_BattleStatus
  def actor_index
    id = $game_party.targetable_members[@index].id
    $game_party.battle_members.each_with_index do |bat, i|
      return i if bat.id == id
    end
    return nil
  end
end
@MoltresRider
Where do I put that in and what do I replace in the original script?
Using Hime's script and you can add this below that.
Below what? You did not give me any context.

And why Hime's and not kyonides's script?
Not I can see why Kyonides stopped helping.

You said
Below what?
I said
Using Hime's script
you said
You did not give me any context
I said
Using Hime's script and you can add this below that.
you said
And why Hime's and not kyonides's script?
and the answer, because you originally were working with Hime's script and I was using that to find a solution. I would not use Kyonides script as a base, because Kyonides would make changes to it himself if needed.


You must know how to use scripts, you added them into your project.
Unless you actually don't. I did see one person once before actually put all scripts into the one slot.
That's not how it's done.
If you are unsure about using scripts, then click on the link in my signature block and read the tutorial from Andar.
here
↓
Just in case it still isn't clear for somebody here, both scripts are mutually Exclusive. Plus, Ron has never commented anything on my script.

Now people, please notice that I have posted an updated version of my KBlockStates ACE script. So I'm encouraging all of you to use the up-to-date version posted in the RGSS3 subforum instead.
Not I can see why Kyonides stopped helping.

You said

I said

you said

I said

you said

and the answer, because you originally were working with Hime's script and I was using that to find a solution. I would not use Kyonides script as a base, because Kyonides would make changes to it himself if needed.


You must know how to use scripts, you added them into your project.
Unless you actually don't. I did see one person once before actually put all scripts into the one slot.
That's not how it's done.
If you are unsure about using scripts, then click on the link in my signature block and read the tutorial from Andar.
here
↓
I know how to use scripts but the code itself is Zulu to me. Modifying and editing code is not something I am familiar unless I am changing specific values or true and false modifiers given in instructions, so when I do not get the entire thing it one piece, I am clueless as to where to put the modified code within the "foreign" language.

Original thread: https://forums.rpgmakerweb.com/threads/crash-with-hime-untargetable-state-whenever-i-use-an-item-please-help.157055/