Item Class Restriction script causing crashes "NoMethodError"

There is a script I'm trying to use for my project, Szyu Item Class Restrictions, but I keep getting a "NoMethodError" crash.

NME.png


Line 113 in the Szyu script is causing the crash.

Line 113 is this:
Code:
      BattleManager.actor.input.item.forbid_classes.include?(BattleManager.actor.input.subject.class_id) ||

These are the surrounding lines.
Code:
class Window_BattleActor < Window_BattleStatus
  #--------------------------------------------------------------------------
  # * Get Activation State of Selection Item
  #--------------------------------------------------------------------------
  def current_item_enabled?
    return false if !BattleManager.actor.input.item.is_a?(RPG::UsableItem) ||
      BattleManager.actor.input.item.forbid_classes.include?(BattleManager.actor.input.subject.class_id) ||
      BattleManager.actor.input.item.forbid_actors.include?(BattleManager.actor.input.subject.id)
    return true
  end
end
When I take the script out, there are no crashes. So the script must be interacting with them in a way that's not intended.

Before anyone asks, I did start a new game to test this.
That means there is no such thing as forbid_classes on the item being used.

It doesn't appear to be a script that requires a new game.

Do you have any other scripts that do things with weapons or armors (assuming the crash is happening in the equip screen) or items (if the crash happens when selecting an item in battle)? Could you post a screenshot of your script editor, showing the names of any extra scripts you've added?

Is this happening when you try to equip or unequip something, or are you not even getting that far? If you are getting that far, is it happening with all items, or only specific ones? If only specific ones, can you show a screenshot of that item (weapon/armor) in the database, so we can see the note box?

If it's in the battle screen and you're trying to use an item, is it a specific item, some of them, or all of them? Can you show a screenshot of that item in the database?
The script looks alright. I will assume that it's a problem from other scripts that loads notetag or similar, and it will work if you only use the script in a fresh project. Yes, that's a lot of possibilities.

There's lazy fool proof though. Try to put this below the script
Code:
class RPG::BaseItem
  def forbid_classes
    load_iucr_notetags_sz unless @forbid_classes
    return @forbid_classes
  end
end

Edit: fixing typo
@TheoAllen typo in method name
That means there is no such thing as forbid_classes on the item being used.

It doesn't appear to be a script that requires a new game.

Do you have any other scripts that do things with weapons or armors (assuming the crash is happening in the equip screen) or items (if the crash happens when selecting an item in battle)? Could you post a screenshot of your script editor, showing the names of any extra scripts you've added?

Is this happening when you try to equip or unequip something, or are you not even getting that far? If you are getting that far, is it happening with all items, or only specific ones? If only specific ones, can you show a screenshot of that item (weapon/armor) in the database, so we can see the note box?

If it's in the battle screen and you're trying to use an item, is it a specific item, some of them, or all of them? Can you show a screenshot of that item in the database?
The crash happens when I use skills during a battle, but only on skills that target the user/allies. No crash on skills that affect/target enemies. Weirdly, it doesn't crash when I use the same skills outside of a battle.

Pics of my scripts
script list 1.png
script list 2.png


The script looks alright. I will assume that it's a problem from other scripts that loads notetag or similar, and it will work if you only use the script in a fresh project. Yes, that's a lot of possibilities.

There's lazy fool proof though. Try to put this below the script
Code:
class RPG::BaseItem
  def forbid_classed
    load_iucr_notetags_sz unless @forbid_classes
    return @forbid_classes
  end
end
No crashes after I added this. I did change "forbid_classed" to "forbid_classes".
Looks like I was wrong. Now, I am getting crashes when I use items that deal damage.

This is the crash log:

Szyu - Item Class Restriction:71:in `item_test': undefined method `class_id' for #<Game_Enemy:0xeba0bb8>, NoMethodError
from Kyutaru - D&D: Core Rules:455:in `item_apply'
from Yanfly - Ace Battle Engine:1415:in `item_apply'
from Yanfly - Skill Restrictions:796:in `item_apply'
from Yanfly - Ace Battle Engine:3001:in `apply_item_effects'
from MA - DW Individual Strikes:63:in `apply_item_effects'
from Scene_Battle:597:in `invoke_item'
from Yanfly - Ace Battle Engine:3109:in `invoke_item'
from Yanfly - Ace Battle Engine:3093:in `block (2 levels) in use_item'
from Yanfly - Ace Battle Engine:3093:in `times'
from Yanfly - Ace Battle Engine:3093:in `block in use_item'
from Yanfly - Ace Battle Engine:3089:in `each'
from Yanfly - Ace Battle Engine:3089:in `use_item'
from Yanfly - Ace Battle Engine:2990:in `execute_action'
from Yanfly - Visual Battlers:655:in `execute_action'
from Yanfly - Instant Cast:303:in `execute_action'
from Scene_Battle:550:in `process_action'
from Scene_Battle:48:in `update'
from Scene_Base:14:in `main'
from SceneManager:23:in `run'
from Main:10:in `block in <main>'

That points to this part of the script:

Code:
#==============================================================
#   * Game_Battler
#==============================================================
class Game_Battler < Game_BattlerBase
  alias sz_iucr_item_test item_test

  def item_test(user, item)
    return false if item.is_a?(RPG::Item) &&
      (item.forbid_classes.include?(self.class_id) || item.forbid_actors.include?(self.id))
    return sz_iucr_item_test(user, item)
  end
end

Line 71 being
Code:
(item.forbid_classes.include?(self.class_id) || item.forbid_actors.include?(self.id))


EDIT: Nevermind, I found a solution that solved my issue.
Well, the scripter seems to be a newbie 😛 at least that would be the case at the moment he finished coding this script. Jokes aside, he or she forgot to take in consideration enemies also need to heal themselves at some point. They usually don't but it wasn't impossible to make it ever happen...

I would rewrite that method.

Code:
  def item_test(user, item)
    if item.is_a?(RPG::Item)
      if user.is_a?(Game_Actor)
        return false if (item.forbid_classes.include?(self.class_id) or item.forbid_actors.include?(self.id))
      end
    end
    return sz_iucr_item_test(user, item)
  end

This way it won't affect enemies using items to get healed or hurt or anything else they might come up with. If you were planning to add an exception where enemies' attempts to use an item should fail, you'd need to include a new condition like...

if user.is_a?(Game_Enemy)
return some code
end

Original thread: https://forums.rpgmakerweb.com/threads/item-class-restriction-script-causing-crashes-nomethoderror.120618/