How to detect/capture if items bought or sold in shop

● ARCHIVED · READ-ONLY
Started by tasyo 2 posts View original ↗
  1. Hi Everyone,

    May i ask some help, I'm building a custom javascript functions to call when something bought or sold in a shop, my goal is to trigger a javascript function and pass the item variables when the player bought or sold something in shop.

    Code:
    //list items in a varialble ["ItemType","ID","CustomPrice","CustomPriceValue"]
    var goods = [
       [ 0, 12, 1, 400 ],//potion
       [ 2, 30, 1, 800 ]//superpotion
    ]
    //then i use scenemanager to list the goods inside the shop
    SceneManager.push(Scene_Shop);
    SceneManager.prepareNextScene(goods, false);

    whenever the player bought or sold it will trigger the functions i created to capture what item is it (pass variables) and do some procedures.

    //this function will trigger when they buy something in shop
    Code:
    boughtItem("ItemType","ID","CustomPrice","CustomPriceValue")

    //this function will trigger when they sell something in shop
    Code:
    soldItem("ItemType","ID","CustomPrice","CustomPriceValue")

    Can somebody here can help me please how to trigger function or call a script when something is bought or sold in shop. I'm still new in RRMV and need to learn more thanks.
  2. Long story short, you'll need to overwrite the method on the Scene_Shop prototype. This is really verbose, involves a bunch of unidiomatic misdirection and is generally a huge pain in the butt.

    Code:
    const myPlugin = {
      Scene_Shop: {
        // implement additional actions here... 
        actions: {
          boughtItem(item) {
            // do stuff with item
          },
          soldItem(item) {
            // do stuff with item
          }
        },
        // we need to store the original methods so we can invoke them in our overwrite
        original: {
          doBuy: Scene_Shop.prototype.doBuy,
          doSell: Scene_Shop.prototype.doSell
        }
      }
    };
    
    // overwrite doBuy on prototype.
    Scene_Shop.prototype.doBuy = function (number) {
      // need to `call` the methods to preserve the `this` argument
      myPlugin.Scene_Shop.original.doBuy.call(this, number);
      myPlugin.Scene_Shop.actions.boughtItem.call(this, this._item);
    }
    
    // overwrite doSell on prototype
    Scene_Shop.prototype.doSell = function (number) {
      // need to `call` the methods to preserve the `this` argument
      myPlugin.Scene_Shop.original.doSell.call(this, number);
      myPlugin.Scene_Shop.actions.soldItem.call(this, this._item);
    }

    Shameless plug -- feel free to ignore if you're not interested -- I've written a plugin that abstracts away much of this, but you'l need to have a pretty solid grounding in JS development to get much out of it.

    Code:
    import { Plugsy, shimmer } from 'plugsy';
    
      export default class MyPlugin extends Plugsy {
    
        // overwritten actions (with original object, function and args)
    
        doBuy(shop, doBuy, number) {
          doBuy(number);
          // buy stuff
        }
    
        doSell(shop, doSell, number) {
          doSell(number);
          // sell stuff
        }
    
        // ...and then some build-up and tear-down.
    
        // remove method overwrites when we're done with the plugin
        uninstall() {
          shimmer(Scene_Shop.prototype, this.handle);
        }
    
        // add method overwrites when we start using the plugin
        install() {
          const methods = { doBuy: this.doBuy, doSell: this.doSell };
          this.handle = shimmer(Scene_Shop.prototype, methods);
        }
    }
    
    $plugsy.install(MyPlugin);

    Let me know if you have questions about anything. :)