Saving the ID of an Weapon/Armor in first inventory Slot to Variable?

Not the ID of equipped Weapons or Armor, but in the inventory.

Context: A system I want to create shall work as follows
Player can buy up to ONE item from a shop -> Shop closes -> Game checks if inventory has Weapon or Armor -> Saves ID of that as variable -> If variable == ID, equip Weapon/Armor with ID -> Clear all Weapons/Armor in inventory (Not equipped)
Through this system, the player should never have any Weapon or Armor in inventory as new ones replace the old equipment immediately upon buying and then the old ones get deleted.
What I specifically need:
A script call that checks Equipment in Inventory and saves its ID into a variable (I chose variable 17)
Due to my attempt to stay away from plug-ins as much as possible (to some extend to hopefully learn ab it) I try to get by with script calls and events.
if no plugins, than the best approach is a mini shop based on choices.

selecting the choice can set the id or weapon into a variable.
than clear the inventory of equips and weapon, and force equip the item.

force equip is probably a trait I believe.

I did a mini tutorial how to obtain it, so to save the item inside a variable,
you can do "$gameVariables.setValue(17, $dataWeapons[id])"

than clear the inventory for weapons, you can do: $gameParty._weapons = {};
same for _armors or _items. not sure yet of equiped (but you can do it probably
after force equiping around: $gameActors.actor(id).forceChangeEquip(0, null);
Depending on the actor (if you use a single actor) you can force equip it first,
than erase the inventory, so no variable required by than.
Making a shop via "show choice" event would function, but be quite annoying for players as they could not see what their options would actually give them due to the lack of item description and equipment stat display.
I kind of figured out how to get everything to work as long as I can somehow get the ID of a bought Weapon or Armor into a variable somehow.

After some trials and tribulations (mostly tribulations), I have actually given and and am at least currently working that shop out using BurningOrcas_CustomShop as its ability to apply conditions to what items show up in it being perfect for how I specify and randomize what items can appear in the shop.
I forgot to mention that as I completely stumbled on explaining on trying to explain what I need, sorry.

I figured that, as the game offers an event for checking if a specific item/weapon/armor is in the inventory, the game must have some sort of call to retrieve the IDs to check them against the ID the player is asking to check for.

if no plugins, than the best approach is a mini shop based on choices.

selecting the choice can set the id or weapon into a variable.
than clear the inventory of equips and weapon, and force equip the item.

force equip is probably a trait I believe.

I did a mini tutorial how to obtain it, so to save the item inside a variable,
you can do "$gameVariables.setValue(17, $dataWeapons[id])"

than clear the inventory for weapons, you can do: $gameParty._weapons = {};
same for _armors or _items. not sure yet of equiped (but you can do it probably
after force equiping around: $gameActors.actor(id).forceChangeEquip(0, null);
Depending on the actor (if you use a single actor) you can force equip it first,
than erase the inventory, so no variable required by than.
Closing a shop directly after buying require a plugin.

DreamX has a choiceHelpWindow (caethyril has a patch for it too).

but if you want no plugins, its gonna be hard, to check stats.
so it would limit the ways.

however, choice window as shop can also be challenging to buy or not to buy.
and count it as a gamble.

another way if you select an choice, show another choice with a window message,
telling what it gain (+10 attack, +5 def) so they can keep track, than show choice
for buying or not.

so you need to just a bit of jump to label as well, so it can be tricky.
I guess the easiest solution would be to give in and use a plug_in that would allow me to close the shop window after a purchase. Maybe one that would react to he player pressing "OK" while in he shop?

I think I at least stumbled my way into getting the weapon and armor IDs!

if ($gameParty.weapons().length > 0) {

$gameVariables.setValue(17, $gameParty.weapons()[0].id);

} else {

$gameVariables.setValue(17, 0);

}


for weapons and

if ($gameParty.armors().length > 0) {

$gameVariables.setValue(2, $gameParty.armors()[0].id);

} else {

$gameVariables.setValue(17, 0); // No armor in inventory

}


for armor.

It is far from elegant, I have to put whichever I put second into a conditional branch for Variable 17 = 0, but it works as there should never be more than one weapon and armor to equip in the players inventory.
Closing a shop directly after buying require a plugin.

DreamX has a choiceHelpWindow (caethyril has a patch for it too).

but if you want no plugins, its gonna be hard, to check stats.
so it would limit the ways.

however, choice window as shop can also be challenging to buy or not to buy.
and count it as a gamble.

another way if you select an choice, show another choice with a window message,
telling what it gain (+10 attack, +5 def) so they can keep track, than show choice
for buying or not.

so you need to just a bit of jump to label as well, so it can be tricky.
can be easier, I try to find the function for a snippet for you, but you need
to alter thing function correctly.

JavaScript:
Scene_Shop.prototype.doBuy = function(number) {
    $gameParty.loseGold(number * this.buyingPrice());
    $gameParty.gainItem(this._item, number);
};

so you can check for weapons to force equip it,
if armor force equip armor + correct slots
close shop,
empty inventory for weapons/armor

but I need some testing to do so, as number might be or something to
force 1, so you need to by-pass that as well to make it clean.
Could I not add SceneManager.pop(); to this whole thing and either find a way to add it into the actual maker code or somehow turn that into a plug in?
Unless that command works differently from how I understand it that should just close the scene/window of the shop
can be easier, I try to find the function for a snippet for you, but you need
to alter thing function correctly.

JavaScript:
Scene_Shop.prototype.doBuy = function(number) {
    $gameParty.loseGold(number * this.buyingPrice());
    $gameParty.gainItem(this._item, number);
};

so you can check for weapons to force equip it,
if armor force equip armor + correct slots
close shop,
empty inventory for weapons/armor

but I need some testing to do so, as number might be or something to
force 1, so you need to by-pass that as well to make it clean.
mostly on the scene itself, this.pop() should work, but it also
depending how the entire shop scene, so you might to check
some shop plugins how they work.

cancel mostly use the pop scene there, so you basicly use the force cancel.
I see! I guess I will play around with this then. My only concern is breaking something that BO_CustomShop calculates after stuff has been bought by closing the window before these things were done. I guess the adjustment of stocks could be in that area.
if you use BS_CustomShop, then link to it as I know a BO_CustomShop too.
so it might be different, so if I can check the BS one, maybe me or someone
can build a patch or how to alternate it to fit your needs.

doBuy can force cancel so it closes, and the rest of the function inside a new one.

so the function is a bit cleaner and readable too.
I meant BO_CustomShop. I just keep mistyping it for some reason.

if you use BS_CustomShop, then link to it as I know a BO_CustomShop too.
so it might be different, so if I can check the BS one, maybe me or someone
can build a patch or how to alternate it to fit your needs.

doBuy can force cancel so it closes, and the rest of the function inside a new one.

so the function is a bit cleaner and readable too.
I can check on it to close it after buying, have some time in 3 hours or so.
I think I managed to Frankenstein a solution together, though I am not sure if it could cause problems in some way.

(function() {
const alias_doBuy = Scene_Shop.prototype.doBuy;

Scene_Shop.prototype.doBuy = function(number) {
alias_doBuy.call(this, number);

if ($gameParty.gold() >= 0 && number > 0) {
SceneManager.pop();
};
}
)();


I understand like half of what this actually does and am not sure if the first line is really necessary, but it at least seems to work on the few tests I did. Obviously, there is no way to just turn it off this way, but with all my shops being single buy anyway, it should work out... Maybe.
if you do the gold part, when it might give trouble 😛

you could rather try this one:
JavaScript:
let alias_SceneShop_doBuy = Scene_Shop.prototype.doBuy
        Scene_Shop.prototype.doBuy = function() {
            alias_SceneShop_doBuy.call(this)
                this.onBuyCancel();
                console.log("this run when shop is closed!")
        };

untested, but if the console.log runs correctly.
you can do force equip of the item bought, empty the inventory on weapons/armor.

because you check the buy item, once complete, it should close.
which does onBuyCancel does ( so see if it works on this, and if the log is fired).

from there, the script snippet should work too.
if you do the gold part, when it might give trouble 😛

you could rather try this one:
JavaScript:
let alias_SceneShop_doBuy = Scene_Shop.prototype.doBuy
        Scene_Shop.prototype.doBuy = function() {
            alias_SceneShop_doBuy.call(this)
                this.onBuyCancel();
                console.log("this run when shop is closed!")
        };

untested, but if the console.log runs correctly.
you can do force equip of the item bought, empty the inventory on weapons/armor.

because you check the buy item, once complete, it should close.
which does onBuyCancel does ( so see if it works on this, and if the log is fired).

from there, the script snippet should work too.
Upon testing, this turned my amount of gold to NaN
JavaScript:
    let alias_SceneShop_doBuy = Scene_Shop.prototype.doBuy
        Scene_Shop.prototype.doBuy = function() {
            alias_SceneShop_doBuy.call(this)
                this.popScene();
                console.log("this run when shop is closed!")
        };

this seems to work half, as the scene hides it, but I do some test to close it.
as for your NaN problem, buy default, it works, so I need to patch that too.
Sorry for the late reply. Did not have the time to really do anything in the past days.
Anyway, the stitched together abomination somehow has not broken yet, so I will keep going with that for now^^

Original thread: https://forums.rpgmakerweb.com/threads/saving-the-id-of-an-weapon-armor-in-first-inventory-slot-to-variable.176596/