Adding a "Sell All" option to the shop screen.

Hello there! I am having some trouble adding a feature to my game. I am trying to add a "Sell All" option, in which all the items currently being displayed on the item list will be sold. Here's what I have so far:


(function() {
var _Scene_Shop_createSellWindow = Scene_Shop.prototype.createSellWindow;
Scene_Shop.prototype.createSellWindow = function() {
_Scene_Shop_createSellWindow.call(this);
this.addCustomSellOption();
};

Scene_Shop.prototype.addCustomSellOption = function() {
var sellWindow = this._sellWindow;
if (sellWindow) {
var commandName = "Sell All";
sellWindow.setHandler('sellAll', this.commandSellAll.bind(this));
sellWindow.refresh();
}
};

Scene_Shop.prototype.commandSellAll = function() {
var items = this._itemWindow.data();
for (var i = 0; i < items.length; i++) {
var item = items;
if (item) {
var quantity = $gameParty.numItems(item);
if (quantity > 0) {
var price = this.sellingPrice(item);
$gameParty.gainGold(price / 2 * quantity);
$gameParty.loseItem(item, quantity);
}
}
}
this._sellWindow.refresh();
this._sellWindow.activate();
this._statusWindow.refresh();
this._statusWindow.activate();
this._itemWindow.refresh(); // Refresh the item list after selling all items
};

var _Scene_Shop_createCommandWindow = Scene_Shop.prototype.createCommandWindow;
Scene_Shop.prototype.createCommandWindow = function() {
_Scene_Shop_createCommandWindow.call(this);
this._commandWindow.setHandler('sellAll', this.commandSellAll.bind(this));
};

var _Window_ShopSell_drawItem = Window_ShopSell.prototype.drawItem;
Window_ShopSell.prototype.drawItem = function(index) {
if (index === this.maxItems() - 1) {
var rect = this.itemRect(index);
var align = 'center';
this.resetTextColor();
this.drawText("Sell All", rect.x, rect.y, rect.width, align);
} else {
_Window_ShopSell_drawItem.call(this, index);
}
};

var _Window_ShopSell_maxItems = Window_ShopSell.prototype.maxItems;
Window_ShopSell.prototype.maxItems = function() {
return _Window_ShopSell_maxItems.call(this) + 1;
};

var _Window_ShopSell_itemRect = Window_ShopSell.prototype.itemRect;
Window_ShopSell.prototype.itemRect = function(index) {
if (index === this.maxItems() - 1) {
var rect = _Window_ShopSell_itemRect.call(this, index - 1);
rect.y += rect.height;
return rect;
} else {
return _Window_ShopSell_itemRect.call(this, index);
}
};
})();


What this does is adds an option to the item list in the "Sell" screen of the shop menu, that should check the item list and compare it to what the player has.

What I'm running into is that anytime I try and select the option added, I just hear the game's "buzzer" sound, and none of the functionality is being executed at all, no matter what it is. Does anyone have any ideas?
I haven't analysed the whole thing, but this line looks like a potential mistake:
var item = items;
I'm guessing you probably meant var item = items[i];
I’ve moved this to JavaScript/Plug-in Support.
I haven't analysed the whole thing, but this line looks like a potential mistake:
var item = items;
I'm guessing you probably meant var item = items[i];
Right, yes I did. So far I haven't been able to test out the actual function at all since something seems to be preventing it from running at all when I select the option in a playtest.
Alright, I've read through the rest of it. So you're basically just changing the text of the last element in the list of items to "Sell All" - but you've not bound the handler to it in any way, so the engine doesn't have any way to know what you want it to do.

I'm not entirely sure how to do this - it seems like Window_ShopSell is not a command window, so you can't use the usual way of just adding new commands to it. You'd probably need to find the function that handles selecting an item, and overwrite that, so that if your option is selected it will trigger your custom functionality.
Alright, I've read through the rest of it. So you're basically just changing the text of the last element in the list of items to "Sell All" - but you've not bound the handler to it in any way, so the engine doesn't have any way to know what you want it to do.

I'm not entirely sure how to do this - it seems like Window_ShopSell is not a command window, so you can't use the usual way of just adding new commands to it. You'd probably need to find the function that handles selecting an item, and overwrite that, so that if your option is selected it will trigger your custom functionality.
I may have bit off more than I can chew. I didn't think a Sell All button would be hard to implement, but I've tried a number of different ways and it's proved troublesome.

Original thread: https://forums.rpgmakerweb.com/threads/adding-a-sell-all-option-to-the-shop-screen.159118/