[Help] Storing data on Items

So I'm pretty stuck on my Object-Based variables plugin and trying to search for some new perspectives. The Actor-based one is working, but for some reason Items are just being a ... PAIN! I'm looking through the Game_Item() class and it just stores or retrieves data based on a string and integer... the actual item objects are stored elsewhere.

What Are You Trying To Do?
Attempt to store 2 arrays onto each individual item picked up or initialized by each actor (or party inventory in the event of "picked up"). With Plugin Commands to add, remove, increase, reduce, or set a specific variable or switch by ID and value. Similar to the RMMV global switches and variables (default max is 5000 but can be changed in the plugin parameters)
_switches = [];
_variables = [];

What Have You Tried So Far?
I have initialized Game_Item() putting in the 2 arrays, and when I attempt to set a variable - use the following code as an example - then I get an TypeError: cannot set property "1" of undefined:
Code:
LordV.cmdSetItemVariable = function(args) {
    var actor  = $gameParty.leader();
    var type   = args[0];            // item type (not used -yet-)
    var typeID = Number(args[1]);    // 4  Armor Slot (SlotID = 3)
    var ID       = Number(args[2]);   // 1
    var value  = Number(args[3]);   // 5
    if (args[4] !== undefined) {
        actor = $gameActors.actor(Number(args[4]));
    }
    var item = actor.equips()[typeID - 1];
    // Now set the variable (and hope this works)
    item._variables[ID] = value;
};
^ the plugin command is: "SetItemVariable armor 4 1 5"

What Seems To Be The Problem?
The item.. is not an object apparently. Or it is but the arrays are not being added to the item.

What Do You Want From Us?
I'm hoping the community can give me some fresh eyes/perspective. I'm still but an amateur programmer and to overcome this obstacle would be a great boon! Object-based switches and variables.. many many applications for that 😛 As I've mentioned, I already have actor-based variables and switches, and I'm sure the Party, Events, and Maps will be a lot easier as they're actual objects... but Items seems to be a different ballpark for some reason.
Edit @lordvalinar: oh wait no never mind, I was wrong. 😴

Edit 2 @lordvalinar: maybe try pressing F8 during test-play and entering something like this into the console window? 😅
Code:
$gameParty.leader().equips()[3]

Edit 3: (3rd time lucky!) OK it looks like equips() returns the respective $dataItems array entries rather than the actual Game_Item objects. Just quickly tested this and it seems good to me, maybe it'll fix the problem?
Code:
var typeID = 4;
var item = actor._equips[typeID - 1];
EDIT: Attached screenshots and the WIP code
EDIT2: Also I see what you're saying. Tried that, but instead of returning "Leather Armor", the console returned "Item: undefined" using
Code:
var item = actor._equips[typeID - 1];
^ I think actor.equips() /has/ to be used or else it doesn't reference the correct object.

Edit @lordvalinar: oh wait no never mind, I was wrong. 😴
Edit 3: (3rd time lucky!) OK it looks like equips() returns the respective $dataItems array entries rather than the actual Game_Item objects. Just quickly tested this and it seems good to me, maybe it'll fix the problem?
Code:
var typeID = 4;
var item = actor._equips[typeID - 1];

Getting the item is not the problem. As my code above I get my body armor and the console confirms this
Code:
console.log("Item: "+item.name);
SHOWS: "Item: Leather Armor"

The problem comes with assigning a variable ID and value to the item iteself. ( item._variables[ID] = value ) That is when it gives the TypeError.
OK I've actually spent some time testing now and can reproduce the error; seems my third edit was correct. (Got there eventually! Sorry about that.) Explanation to follow~

...instead of returning "Leather Armor", the console returned "Item: undefined" using
Code:
var item = actor._equips[typeID - 1];
^ I think actor.equips() /has/ to be used or else it doesn't reference the correct object.
Did you remember to define typeID? $gameActors.actor(1)._equips[3], for example, returns the expected object for me. 😅

As stated, equips() will return the database object from $dataItems, not the Game_Item instance:
Code:
Game_Actor.prototype.equips = function() {
    return this._equips.map(function(item) {
        return item.object();
    });
};
The latter is stored in save files, the former is reloaded from the data files every time the game is run. Put another way: $dataItems is not saved, $gameActor and $gameParty are saved and contain equipped/carried items respectively. 😮‍💨

$dataItems contains all the static item stuff: name, formula, effect list, notetags, etc. Game_Item objects literally have only two properties by default: _dataClass and _itemId; these properties are purely used to identify each item's associated database entry. 🙂

Getting the item is not the problem. As my code above I get my body armor and the console confirms this
Code:
console.log("Item: "+item.name);
SHOWS: "Item: Leather Armor"

The problem comes with assigning a variable ID and value to the item iteself. ( item._variables[ID] = value ) That is when it gives the TypeError.
Yes. _variables does not exist on the database object, only on Game_Item. Attempting to define a member of a non-existent array will return your "TypeError: Cannot set property '1' of undefined" (property 1 because ID = 1).

This line in the console works for me, even after saving, exiting, and continuing from that save: 😤
Code:
$gameActors.actor(1)._equips[3]._variables[1] = true
With no plugins, this line will prompt the TypeError unless you have first initialised the array on that item:
Code:
$gameActors.actor(1)._equips[3]._variables = []


As mentioned, using equips() will cause the array to be defined on $dataItems instead, meaning the array will not persist after closing the game. 👋 Simple console example if you'd like to test for yourself:
Code:
var actor = $gameActors.actor(1);
var eId = 3, vId = 1, value = 500;
actor.equips()[eId]._variables = [];  // initialise array
actor.equips()[eId]._variables[vId] = value;
EDIT: Alright I tried it and.... omg it worked. I didn't even have to initialize it though, ((EDIT 2: I didn't need to initialize it because it was already set up in Game_Item()... duh)) I ended up using:
Code:
var equips = actor._equips[typeID - 1]._variables;
equips[ID] = value;
And then of course some debug var to ensure we could display its value
Code:
var dValue = actor._equips[typeID - 1]._variables[ID];
And everything displayed correctly... Leather Armor ID(1) Value(5)

You... are... a genius! *Sends Virtual Hugs* I may or may not still have tons of questions and problems with solutions to figure out but THIS... this my friend was a huge step of progress!!!! AND THOU SHALT BE CREDITED!!! <3 <3 <3

----------------
typeID is defined via the Plugin Command "SetItemVariable armor 4 1 5"
- SetItemVariable = the main command
- armor = determines where to look for the item (item = itemContainer(), armor/weapon = equips())
- 4 = the itemId (used across all item types)
- 1 = the variable ID
- 5 = the value to store

So then.. if initializing on Game_Item() does nothing, I can just take that part out? I will test your method as well and get back to you.

Alternatively if this method isn't working I was trying to figure out if making a new JSON file would do the trick. You know, storing like:
Code:
 [
null,
{"id":1,"type":"armor":{"etypeId":4,"switches":[],"variables":[]}},
{"id":2,"type":"armor":{"etypeId":2,"switches":[],"variables":[]}}
]

Or SOMETHING like that. But I'll try your method first 🙂
EDIT: Alright I tried it and.... omg it worked. I didn't even have to initialize it though, ((EDIT 2: I didn't need to initialize it because it was already set up in Game_Item()... duh)) I ended up using:
Code:
var equips = actor._equips[typeID - 1]._variables;
equips[ID] = value;
And then of course some debug var to ensure we could display its value
Code:
var dValue = actor._equips[typeID - 1]._variables[ID];
And everything displayed correctly... Leather Armor ID(1) Value(5)

You... are... a genius! *Sends Virtual Hugs* I may or may not still have tons of questions and problems with solutions to figure out but THIS... this my friend was a huge step of progress!!!! AND THOU SHALT BE CREDITED!!! <3 <3 <3
Yes, _variables should already be present (initialised) on all Game_Item instances thanks to your plugin, so you shouldn't need to check/define it before assignment. Happy to help! 😂

Also, just for reference: the JSON is basically $dataItems, i.e. the "static" database values as defined in the editor. In most cases I would strongly recommend not editing the existing JSON files manually. 😅
Yes, _variables should already be present (initialised) on all Game_Item instances thanks to your plugin, so you shouldn't need to check/define it before assignment. Happy to help! 😂

Also, just for reference: the JSON is basically $dataItems, i.e. the "static" database values as defined in the editor. In most cases I would strongly recommend not editing the existing JSON files manually. 😅

Yeah actually I was just going to say I realize it now. initEquips() function is setting all the actor._equips slots with = new Game_Item(); hence why they're initialized; however, I noticed there was a problem with that method. Just because a slot was initialized, does not mean the item itself was. Example: Slot[0] (a weapon) could have variables saved to it even if you do not have a weapon equipped.

At the moment I have it set like this (But I'm just going to get rid of the Game_Item() initialization and do a initEquips() one instead, and when gainItem think as well):
Code:
LordV.cmdSetItemVariable = function(args) {
    var actor  = $gameParty.leader();
    var type   = args[0];
    var typeID = Number(args[1]);
    var ID       = Number(args[2]);
    var value  = Number(args[3]);
    if (args[4] !== undefined) {
        actor = $gameActors.actor(Number(args[4]));
    }
    var item = null;
    switch (type) {
        case 'item': {
            item = $gameParty.items()[typeID - 1];
            if (item && item._variables === undefined) {
                item._variables = []; // initialize
            }
        } break;
        case 'equips': {
            var isEquipped = actor.equips()[typeID - 1];
            item = isEquipped ? actor._equips[typeID - 1] : null;
        } break;
    }
    if (item) this.setVariable(item, ID, value);
};

And it works, but I'm going to do some re-arranging now that I understand it a bit better now (with your help of course) ^_^
With initEquips() I can initialize blank switches and variables array to items that EXIST in those slots, and when the party gains an item, they'll gain a switches and variables arrays 🙂

Original thread: https://forums.rpgmakerweb.com/threads/help-storing-data-on-items.109384/