Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Created November 8, 2016 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KinoAR/a8f47c9f4e85e04cc4b9938371bf7a05 to your computer and use it in GitHub Desktop.
Save KinoAR/a8f47c9f4e85e04cc4b9938371bf7a05 to your computer and use it in GitHub Desktop.
RPGMakerMV pattern for observing changes to player inventory.
//=============================================================================
// Observer Pattern
//=============================================================================
//This creates an observer in RPGMaker MV
//This is an example of checking whether an item exists on an observer
(function() {
var parameters = PluginManager.parameters("ObserverPattern");
function Setup() {
'use strict';
//Static class for monitoring items
class ItemObserver {
static startObserver() {
this._itemList = [];
this.update();
}
static update() {
this.monitorUpdates();
this.requestUpdate();
}
static requestUpdate() {
requestAnimationFrame(this.update.bind(this));
}
static monitorUpdates() {
if(!this.listMatch()) {
console.log("Item List Updated");
this.itemList = $gameParty.allItems();
}
}
static listMatch() {
if($gameParty !== null) {
let items = $gameParty.allItems();
for(let i = 0; i < items.length; i++) {
if(this.itemList[i] === undefined || this.itemList[i] !== items[i])
return false;
}
return true;
}
return true;
}
static set itemList(itemList) {
this._itemList = itemList;
}
static get itemList() {
return this._itemList;
}
}
//Start Observer Update Method
ItemObserver.startObserver();
}
Setup();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment