MV Retaining equipment with an empty party causes an error (item.object is not a function)

Hi!

(I know the title is confusing, but I couldn't find a better way to summarize it)

The thing is:
ChatGPT has created a script for my game to retain party equipment when removing/adding members. So, when I remove a member from the party temporarily and this member returns later, their equipment is still with them (this can't seem to be done without this script).

The problem is that during the early stage of development, I used this to solve another issue:
1728262880740.png


Without removing and re-adding the protagonist first, when I added a new character, it would become the main character (the first in the party order). I just wanted to avoid that and this was an easy way to do it.

Now that I have this new script, a player noticed that error when playing the first moment when a new character is added to the party like that. Here is the error:
Spoiler: Main Error
1728263000126.png

rpg_managers.js:1949 TypeError: item.object is not a function
at rpg_objects.js:3600
at Array.map (<anonymous>)
at Game_Actor.equips (rpg_objects.js:3599)
at Game_Actor.equips (YEP_EquipCore.js:469)
at Game_Actor.traitObjects (rpg_objects.js:3810)
at Game_Actor.Game_BattlerBase.allTraits (rpg_objects.js:2375)
at Game_Actor.Game_BattlerBase.traits (rpg_objects.js:2381)
at Game_Actor.Game_BattlerBase.traitsSet (rpg_objects.js:2411)
at Game_Actor.Game_BattlerBase.slotType (rpg_objects.js:2539)
at Game_Actor.Game_BattlerBase.isDualWield (rpg_objects.js:2544)
SceneManager.catchException @ rpg_managers.js:1949

Here is the script I created with ChatGPT (it can't seem to solve the issue, even after adding extra checks):

Spoiler: ZOE_RetainEquipment.js
JavaScript:
//=============================================================================
// ZOE_RetainEquipment.js
//=============================================================================

/*:
 * @plugindesc Retain equipment for actors when removed from the party and added again.
 * Also provides options to control functionality through plugin commands, parameters, and notetags.
 *
 * <RetainEquipment>
 * @param Initial Plugin Status
 * @desc Initial status of the plugin functions. Set to 'true' to enable by default, 'false' to disable. Default: true
 * @default true
 *
 * @help ZOE_RetainEquipment.js
 * Version: 1.0
 * Credits to ZoeZero (Zoe Miranda)
 * Free for both commercial and non-commercial use.
 *
 * This plugin ensures that equipment is not automatically unequipped from
 * an actor when they are removed from the party. They will have the same
 * equipment when added back to the party again unless specified otherwise.
 *
 * Plugin Commands:
 *   EnableRetainEquipment    - Enable the plugin functions.
 *   DisableRetainEquipment   - Disable the plugin functions.
 *   EnableNotetagFunctions   - Enable only the notetag-related functions.
 *   DisableNotetagFunctions  - Disable only the notetag-related functions.
 *
 * Script Calls:
 *   $gameSystem.enableRetainEquipment() - Enable the plugin functions.
 *   $gameSystem.disableRetainEquipment() - Disable the plugin functions.
 *   $gameSystem.enableNotetagFunctions() - Enable only the notetag-related functions.
 *   $gameSystem.disableNotetagFunctions() - Disable only the notetag-related functions.
 *
 * Notetags:
 *   <RetainEquipment>       - Add this notetag to actors, equipment, weapons, classes, or states to retain equipment for them.
 *   <DisableRetainEquipment> - Add this notetag to actors, equipment, weapons, classes, or states to disable equipment retention for them.
 */

(function() {
    var parameters = PluginManager.parameters('ZOE_RetainEquipment');
    var initialStatus = parameters['Initial Plugin Status'] === 'true';

    var retainEquipmentEnabled = initialStatus;
    var notetagFunctionsEnabled = initialStatus;

    var pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        pluginCommand.call(this, command, args);
        if (command === 'EnableRetainEquipment') {
            retainEquipmentEnabled = true;
        } else if (command === 'DisableRetainEquipment') {
            retainEquipmentEnabled = false;
        } else if (command === 'EnableNotetagFunctions') {
            notetagFunctionsEnabled = true;
        } else if (command === 'DisableNotetagFunctions') {
            notetagFunctionsEnabled = false;
        }
    };

    var _Game_System_initialize = Game_System.prototype.initialize;
    Game_System.prototype.initialize = function() {
        _Game_System_initialize.call(this);
        this._retainEquipmentEnabled = initialStatus;
        this._notetagFunctionsEnabled = initialStatus;
    };

    Game_System.prototype.enableRetainEquipment = function() {
        this._retainEquipmentEnabled = true;
    };

    Game_System.prototype.disableRetainEquipment = function() {
        this._retainEquipmentEnabled = false;
    };

    Game_System.prototype.enableNotetagFunctions = function() {
        this._notetagFunctionsEnabled = true;
    };

    Game_System.prototype.disableNotetagFunctions = function() {
        this._notetagFunctionsEnabled = false;
    };

    var _Game_Party_removeActor = Game_Party.prototype.removeActor;
    Game_Party.prototype.removeActor = function(actorId) {
        if ($gameSystem._retainEquipmentEnabled && this._actors.includes(actorId)) {
            var actor = $gameActors.actor(actorId);
            if (actor) {  // Ensure actor exists
                actor._lastEquipments = JSON.parse(JSON.stringify(actor._equips));
            }
        }
        _Game_Party_removeActor.call(this, actorId);
    };

    var _Game_Party_addActor = Game_Party.prototype.addActor;
    Game_Party.prototype.addActor = function(actorId) {
        _Game_Party_addActor.call(this, actorId);
        if (!$gameSystem._retainEquipmentEnabled) return;

        var actor = $gameActors.actor(actorId);
        if (actor && actor._lastEquipments) {  // Ensure actor exists and has saved equipment
            actor._equips = JSON.parse(JSON.stringify(actor._lastEquipments));
            actor.refresh();
        }
        if (actor) actor._lastEquipments = null;  // Reset saved equipment only if actor exists
    };

    var _Game_Actor_setup = Game_Actor.prototype.setup;
    Game_Actor.prototype.setup = function(actorId) {
        _Game_Actor_setup.call(this, actorId);
        if ($gameSystem._notetagFunctionsEnabled) {
            this.checkRetainEquipmentNotetag();
        }
    };

    Game_Actor.prototype.checkRetainEquipmentNotetag = function() {
        var actor = this.actor();
        var classData = this.currentClass();
        var equips = [actor].concat(this.equips()).concat(this.weapons()).concat(this.armors()).concat(classData);

        equips.forEach(function(data) {
            if (data && data.meta) {
                if (data.meta.RetainEquipment) {
                    this._lastEquipments = JSON.parse(JSON.stringify(this._equips));
                } else if (data.meta.DisableRetainEquipment) {
                    this._lastEquipments = null;
                }
            }
        }, this);
    };

})();

Just adding/removing members doesn't cause the error. It only occurs when the party is empty. Disabling the script solves the issue. Here is the game's full plugin list:
Spoiler: Full plugin list
1728263224845.png

1728263248788.png

1728263262676.png

1728263275764.png

1728263297624.png


My questions are:

Can this plugin be edited to work without causing this error when there are no members in the party? This is a recurrent instance in my game and having to remember all moments when that happens would be a headache at this point.

Is there an existing plugin that prevents equipment removal on member removal? That would help too.

I can provide any other information necessary. Thanks in advance!
So, when I remove a member from the party temporarily and this member returns later, their equipment is still with them (this can't seem to be done without this script)
Is there an existing plugin that prevents equipment removal on member removal?
By default, any actor that leaves the party keeps their gear with them and still has it when they return unless you have the initialize checkbox checked when re-adding them. if actors leave and their gear is being removed from them, either you have a plugin doing it or youre reinitializing them
By default, any actor that leaves the party keeps their gear with them and still has it when they return unless you have the initialize checkbox checked when re-adding them. if actors leave and their gear is being removed from them, either you have a plugin doing it or youre reinitializing them
It's probably a plugin, then. I'll take a look at them.

What happens to me is that some battles in my game are 1v1. Then I remove the party and add them back later. When that happens, players (and I, too, when testing) have reported having all the equipment returned to the inventory and having to re-equip it. The members aren't reinitialized, I've double-checked that. All the equipment is constantly removed and this is the cause that seemed most logical to me. So far, I haven't noticed it happen again after adding this script, but now it has caused this new error when the party is empty. Perhaps another plugin is involved. Thanks for pointing that out.
1) the equipment behaviour is not default, so something already has changed the engine.
Please check the order of your Yanfly plugins against his main list, I'm pretty sure that especially several of the ones lower on your list are in the wrong order.
Yanfly's plugins have to be in exactly the order of his main list, top to bottom. you can place other plugins between them where needed for compatibility, but if you mess up Yanfly's order they are known to cause strange behaviour and bugs.

please remember to start a new playtest-game after reordering, older saves will become incompatible.


2) ChatGPT is "famous" for creating nonfunctional, buggy or convoluted plugins.
In fact you need to have a good basic programming knowledge to even guide the bot to create a functional plugin as most results on first try are absolute nonsense.
As a result most programmers here on the forum refuse to bughunt plugins created by that bot (and the few others require you to state the source first, but you already did that).
1) the equipment behaviour is not default, so something already has changed the engine.
Please check the order of your Yanfly plugins against his main list, I'm pretty sure that especially several of the ones lower on your list are in the wrong order.
Yanfly's plugins have to be in exactly the order of his main list, top to bottom. you can place other plugins between them where needed for compatibility, but if you mess up Yanfly's order they are known to cause strange behaviour and bugs.

please remember to start a new playtest-game after reordering, older saves will become incompatible.


2) ChatGPT is "famous" for creating nonfunctional, buggy or convoluted plugins.
In fact you need to have a good basic programming knowledge to even guide the bot to create a functional plugin as most results on first try are absolute nonsense.
As a result most programmers here on the forum refuse to bughunt plugins created by that bot (and the few others require you to state the source first, but you already did that).
Thanks for the insight.

I've reordered it according to Yanfly's website list (I thought I'd already done that, but anyway).

Fortunately, the old saves are still compatible, but I tested it with new saves, too.

I've done some testing and sometimes one or two characters have their equipment removed after an individual battle (as described above) and others don't. That's weird, I'll have to investigate it further.

I'll wait a bit to see if anyone is willing to deal with this code. I know ChatGPT isn't good for RMMV plugins and I stopped using it some time ago and only came back to it to try to fix this code.

This is the new plugin list:

Spoiler: Reordered plugin list
1728271415248.png

1728271441466.png
1728271455370.png
1728271468043.png
1728271479009.png
Troubleshooting suggestions:
  1. Turn off all plugins (Ctrl+A to select all, right-click -> Turn OFF), save your project to apply Plugin Manager changes, then test from New Game, not from a previous save. If it still happens in that case, then check for Equip Weapon/Armor traits: if a trait gets removed (e.g. because it's on a state that gets removed on battle end) then any related equipment will be unequipped.

  2. If it's OK with all plugins turned off, try turning them on in small batches. Save your project each time to apply Plugin Manager changes before testing. Given the problem, I'd consider starting with YEP_X_EquipRequirements and/or YEP_AutoPassiveStates.
This general approach applies to basically any unidentified problem in a project using plugins. The goal is to simplify the situation as much as possible before attempting to check remaining possible causes.

Also, please note that General Rule 4.4 prohibits posting of AI-generated responses without your own input:

4.4 Do NOT post ChatGPT or other AI generated responses without including your own opinions, experiences, and input. Additionally, sources if AI is used to state something as factual. Posting a generated response that could be obtained by anyone using these tools is low quality and will be considered spam. This may warrant your post being removed or other actions on a case-by-case basis.
(Based on your questions here I don't think you understand the code, and as others have said, this shouldn't require a plugin anyway.)

Original thread: https://forums.rpgmakerweb.com/threads/retaining-equipment-with-an-empty-party-causes-an-error-item-object-is-not-a-function.172415/