MV this.changeEquip adding or removing items from inventory?

Is the .changeEquip function supposed to operate independently of inventory?

I am using the following code in my plugin and I noticed that every time it runs, I receive an additional copy of the item in the slot specified.

Code:
if (this.equips()[0])
{ $gameVariables.setValue(81, this.equips()[0].id);
    this.changeEquip(0, $dataWeapons[0]);
}

I had thought .changeEquip was dependent on inventory and that .forceChangeEquip was the function that operated independently of inventory.

Am I misunderstanding something, or is this not supposed to happen? The three weapons with the qty of x44 start out at 10 each, but when running the script they just keep adding up. Any thoughts?

1709964499839.png




Is it adding the items to my inventory because they are attached to a variable perhaps? Or is this just how changeEquip functions. I notice when I use forceChangeEquip instead, the stock doesn't increase when there is no item to remove, but neither does it when actual items are removed. :|

From what I am seeing, I am going to have to put every changeEquip function inside an if statement for if the item is present and the else if with forceChangeEquip being for when the item isn't present.

Or perhaps an extra if statement so that changeEquip only runs if there is an item to be exchanged with (as well as an item currently equipped?)

Please correct me if I am mistaken or doing something incorrectly/inefficiently.
changeEquips is supposed to only swap out the slot if the party has the target item, and then return the item its replacing back into the inventory. forceChangeEquips is supposed to ignore if the player has the item and overwrites the equipment in the slot.

does this still happen when you disable all your plugins?
Hey Robro, you know I actually just disabled what I thought was all of the code I wrote and for some reason I am getting duplicate items still. So this may be something else entirely. I am going to have to mess around and see if I can isolate this.

What is causing the duplicate items is when I am going through the class change option via Yanfly script. Even just scrolling through the classes is causing this, so it probably has something to do with the very part I was editing under here, but even commenting out my code I am still getting duplicates for some reason.
Yanfly.CCC.Game_Actor_changeClass = Game_Actor.prototype.changeClass;
Game_Actor.prototype.changeClass = function(classId, keepExp) {


The only other thing I can think of is that the duplication is occurring when classes are being compared, and this is a plugin interaction. I am going to replace my edited versions with the defaults and see if this will still happen. From what it looks like though, it still will.
I am going to replace my edited versions with the defaults and see if this will still happen. From what it looks like though, it still will.
I suggest this. Its usually better to make your own edits through separate plugins and not by directly changing the plugin itself unless you made it or you know nothing else depends on it. Its easier to keep track of changes that way, especially when multiple plugins alias the same function.

Just to be safe though, your yanfly plugin order matches the one on the website right? if reverting the changes you made doesnt fix it, id check that next
That makes sense, yea I will try that as well as disabling what plugins I can to work on this in more isolation. Going to try just reverting to the default versions first and continue with this tomorrow or so. I really thought my code was pristine until I saw this. No matter, I am sure I can fix it either way.

Update
Wow, yea this was occurring before I made any of these changes. I just tested in a 3 month old backup and the same thing happens. I suppose that is a good thing as most of the changes I have made to the core system has been during these 3 months. Going to try disabling as many plugins as I can to isolate this now.

Think I found it.
Item duplication ceases when HIME_EquipSlotsCore is deactivated. It must have some interaction with YanflyClassChangeCore. Well this just won't do at all. Bug persists if below YanflyClassChangeCore, but game will not open if placed above. So there is definitely an interaction here.

The Hime plugin must change how ChangeEquip or similar functions is my thinking, as it has plugin commands to remove and add eTypes, so it makes sense that the equip functions would be modified somehow. This will be fun.

May have actually found a working arrangement for it, didn't notice bug this time nor did game refuse to open.

Update, I think I may have fixed it surprisingly. HimeEquipSlotsCore needs to go above Yanfly plugins, but the other Hime plugins I have need to be below Yanfly. A bit unintuitive, but this looks like it might be the way. Will test this on the current version of my game tomorrow.
Update, I think I may have fixed it surprisingly. HimeEquipSlotsCore needs to go above Yanfly plugins, but the other Hime plugins I have need to be below Yanfly. A bit unintuitive, but this looks like it might be the way. Will test this on the current version of my game tomorrow.
i was about to mention that the order those get loaded in might produce different results. Depending on what hime's one changes overall there might be other effects.

A thing you can try to do is to trace your way through Game_Actor.prototype.changeEquip and Game_Actor.prototype.tradeItemWithParty with the console and see where all its used. without looking into hime's plugin and knowing what all you have installed, there may be an instance of one or both of them being overwritten or being called more than once that could feasibly lead to item duping

if placing that plugin above the others doesnt have any negative impacts though, no need to change anything outside of curiosity
Thank you, will try that out next. Really hoping this got it solved, because there was no chance I was willing to give up the functionality of either mod.

Ok, so I just tried in the main project and while Hime_EquipCore moved above Yanfly prevents duplication from just scrolling through the class menu, the moment I add
if (this.equips()[0])
{ $gameVariables.setValue(81, this.equips()[0].id);
this.changeEquip(0, $dataWeapons[0]);
}

to the changeClass function, the same thing happens again. Even with Hime_EquipCore completely disabled. So there is something else going on here. Guess I will keep removing plugins and see if that changes anything, or see if this issue is specific to where I am putting my code in the Yanfly scripts.

Code:
 Yanfly.CCC.Game_Actor_changeClass = Game_Actor.prototype.changeClass;
Game_Actor.prototype.changeClass = function(classId, keepExp) {

>>> my code inserted here
 keepExp = Yanfly.Param.CCCMaintainLv;
    if (keepExp) {
      this._exp[classId] = this._exp[this._classId];
      keepExp = false;
    }
    Yanfly.CCC.Game_Actor_changeClass.call(this, classId, keepExp);
    //this.updateLearnedSkills(classId);
    if (!$gameParty.unlockedClasses().contains(classId)) {
      this.unlockClass(classId);
    }
>>> my code inserted here
    $gamePlayer.refresh();
};

This seems to occur with every plugin turned off except for Yanfly's, so this has something to do with the Yanfly ClassChangeCore itself, and the location where I put my code inside the changeClass function.

Perhaps I could piggyback my code off of the core version of changeClass/changeSubclass, which might be found in rpg_scenes/rpg_windows instead? Not sure if these functions are even in the default .js files though.

So the question is then, how can I set this.changeEquip and other functions to occur during the class change menu (or after class change), without being affected by whatever in the Yanfly code is causing this to occur?

Next step is to try attaching my code to a state instead and running it through the state, and to see if other instances of .changeEquip cause this same issue when applied elsewhere.

Duping still occurs when .changeEquip happens through a state that is activated by the classChange menu scrolling... hmm. Doesn't matter where I put the addState function either as long as it is inside the changeClass function, still dupes.

But running .changeEquip on its own does not cause this issue.

I think I may just have to use forceChangeEquip when there is not a valid swap to be made, and changeEquip when there is, or some combination of the two depending on the specific factors that lead to a dupe. Gonna be a long weekend.


Spoiler: Debug Log
Code:
this.lastClass = this.currentClass().id;

if (this.lastClass == this.currentClass().id)
{
this.forceChangeEquip(0, $dataWeapons[0]);
}
//When placed at top, this prevents item duping during class compare phase, but causes item deletion during class change phase. (condition = true)
//Placed at end of classChange function, item duping does not occur and neither does item deletion, but item is not removed from inventory either.  (condition = false)


else if (this.lastClass !== this.currentClass().id)
{
this.changeEquip(0, $dataWeapons[0]);
};
//This does not seem to be applied in this spot at the top (!== condition = false)
//This when applied at bottom causes duplication error while scrolling (!== condition = true)


-> Conclusion from tests: this.lastClass compared to this.currentClass does not distinguish between class compare and class change functions.
//Going to try other functions in the classChange script to see if there is one that is more specific to classChange vs updateCompare
-----
this seems to work specific to the actual class change and does not cause duplication/deletion issues. 
Needs changeEquip as forceChange will delete items here.  Will be working from here.

Scene_Class.prototype.onItemOk = function() {
   
if (this.actor().equips()[0])
{
$gameVariables.setValue(81, this.actor().equips()[0].id);
}
else
{
$gameVariables.setValue(81, 0);
};         
this.actor().changeEquip(0, $dataWeapons[0]); 

While:

Window_ClassList.prototype.updateCompare = function() {

Is specific for the class compare scene (where forceChangeEquip should be called instead to prevent duplicates, if it even needs to be called at all)

Think this should do it.

Original thread: https://forums.rpgmakerweb.com/threads/this-changeequip-adding-or-removing-items-from-inventory.166611/