Hi everyone, I'm working on an item bar window. I realized that in the code, "$gameParty._items" is saved as type "Object".
When I get the list of items from "Game_Party.prototype.items()", it returns me an array of items in ascending order of their IDs.
Since I want to get a list of items sorted in insertion order, I added an “_orderItems” attribute to Game_Party, it is an attribute of type Map. I assign it a value in the "gainItem" method, it's essentially just an insertion-ordered copy of "_items".
Before I tried to save the game, this method accomplished what I was looking for. But when I save the game and then wish to continue it, I found an error in the game. "_orderItems" becomes "{}", so I checked the MZ save game method,
I searched on Google and found that "JSON.stringify" doesn't support "Map" type objects, and it seems that "JsonEx._encode" can't handle the "Map" type. Has anyone ever implemented getting a list of items in insertion order? Any suggestions are welcome!
When I get the list of items from "Game_Party.prototype.items()", it returns me an array of items in ascending order of their IDs.
JavaScript:
Game_Party.prototype.initAllItems = function() {
this._items = {};
this._weapons = {};
this._armors = {};
};
JavaScript:
Game_Party.prototype.items = function() {
return Object.keys(this._items).map(id => $dataItems[id]);
};
JavaScript:
Game_Party.prototype.gainItem = function(item, amount, includeEquip) {
const container = this.itemContainer(item);
if (container) {
const lastNumber = this.numItems(item);
const newNumber = lastNumber + amount;
container[item.id] = newNumber.clamp(0, this.maxItems(item));
if (container[item.id] === 0) {
delete container[item.id];
}
if (includeEquip && newNumber < 0) {
this.discardMembersEquip(item, -newNumber);
}
$gameMap.requestRefresh();
}
};
Since I want to get a list of items sorted in insertion order, I added an “_orderItems” attribute to Game_Party, it is an attribute of type Map. I assign it a value in the "gainItem" method, it's essentially just an insertion-ordered copy of "_items".
Before I tried to save the game, this method accomplished what I was looking for. But when I save the game and then wish to continue it, I found an error in the game. "_orderItems" becomes "{}", so I checked the MZ save game method,
JavaScript:
JsonEx.stringify = function(object) {
return JSON.stringify(this._encode(object, 0));
};
JavaScript:
JsonEx._encode = function(value, depth) {
// [Note] The handling code for circular references in certain versions of
// MV has been removed because it was too complicated and expensive.
if (depth >= this.maxDepth) {
throw new Error("Object too deep");
}
const type = Object.prototype.toString.call(value);
if (type === "[object Object]" || type === "[object Array]") {
const constructorName = value.constructor.name;
if (constructorName !== "Object" && constructorName !== "Array") {
value["@"] = constructorName;
}
for (const key of Object.keys(value)) {
value[key] = this._encode(value[key], depth + 1);
}
}
return value;
};
I searched on Google and found that "JSON.stringify" doesn't support "Map" type objects, and it seems that "JsonEx._encode" can't handle the "Map" type. Has anyone ever implemented getting a list of items in insertion order? Any suggestions are welcome!