Optimize single specific equipment slot SOLVED

Hey, just about done setting up my class change script, but I need one more thing to make it as smooth as butter. When swapping classes I remove all equipped items, save these to a variable, and then re-equip the same items if the actor's class can equip the matching eType. This all works fine and dandy. However, for the slots that are unable to be filled due to eType differences between classes, the slot will obviously be empty after the class change. So I figure the best way to handle this is to optimize the empty gear slots.

The code I am thinking of is something like this:


Code:
if (!this.equips()[0])
{
this.optimizeEquipments()
};

However this will optimize all equipment slots. I didn't find any matches for 'optimize' in the MV script call section, so I was wondering what the correct syntax is to optimize the slot in question. For this example case I will be using slot [0].

I am guessing the proper code is something close to:
Code:
this.optimizeEquipments()[0],

Is this correct?

I wasn't sure if this was supposed to go in another section since it involves javascript, but I think this is the right place as this is a default function in RMMV afaik. If I am mistaken I apologize in advance.

Come to think of it, I could probably just run the optimize call first, and then run the code that equips the previously worn gear afterwards. Still, if this can be done with proper formatting rather than a "shotgun" approach, I think it would be best.

Thank you.
From rpg_objects.js:
JavaScript:
Game_Actor.prototype.optimizeEquipments = function() {
    var maxSlots = this.equipSlots().length;
    this.clearEquipments();
    for (var i = 0; i < maxSlots; i++) {
        if (this.isEquipChangeOk(i)) {
            this.changeEquip(i, this.bestEquipItem(i));
        }
    }
};
So I would suggest something like:
JavaScript:
var actor = $gameActors.actor(1);
var slot  = 0;
if (actor.isEquipChangeOk(slot))
  actor.changeEquip(slot, actor.bestEquipItem(slot));
You'd want to do

this.changeEquip(i, this.bestEquipItem(i));

where i is the slot ID

Be aware, though, that "optimize" in MV and MZ are garbage, to put it politely. They only look at raw stat increases, so you could have a piece of armour with 5 DEF and one with 3 DEF that triples your HP, makes you immune to every status effect in the game, cuts fire damage by 75% and adds a new skill and the command will go for the one with 5 DEF. Luckily, there's a plugin for that. Unfortunately it doesn't work on MV yet. 😛 Give me a bit and that won't be the case.
That is awesome, and thank you both of you for the help. I think I have it working I just need to set up a little if condition to only re-equip with the previous gear if variable 81 is not equal to 0.

Code:
if (
    $gameVariables.value(81) !== 0)
{ 
this.changeEquip(0, $dataWeapons[$gameVariables.value(81)]);
}
;

I think I have that syntax right

(edit: removed brackets from '0', and cleaned up syntax. So confusing lol)
https://forums.rpgmakerweb.com/index.php?threads/trilobytes-mv-optimize-plus.166596/ here's the plugin. 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/optimize-single-specific-equipment-slot-solved.166593/