Object-based Switches and Variables (Part 2)

Alright so with the release of the actor-based switches and variables plugin I made, I was thinking of expanding it...
- Items
- Maps
- Events
- Troops

However on the first of the list (items) I'm already running into a snag. For most of the actor-based ones I was going off of the Game_Actors() object (( Game_Actor.prototype.setActorVariable = function(ID, value) {} )) and it was pretty straight forward, however I tried to make the functions more broad. (see partial code below)

-- EDIT --
I do seem to notice the problem lies with the initialization.... the _switches and _variables arrays are not being assigned to the items themselves, perhaps?

Code:
var LordV = LordV || {};
// ======================================
// * Naturally we setup the Game_Item alias, yes?
// ======================================

LordV.Game_Item_initialize = Game_Item.prototype.initialize;
Game_Item.prototype.initialize = function(item) {
    LordV.Game_Item_initialize.call(this, item);
    this._switches = [];
    this._variables = [];
}


LordV.setVariable = function(object, variableId, value) {  
    if (variableId > 0 && variableId <= LordV.maxVariables) { // maxVariables = plugin parameter
        if (typeof value === 'number') {
            value = Math.floor(value);
        }
        object._variables[variableId] = value;
    }
}

So I try to setup a simple plugin command "SetArmorVariable 4 1 5" (4 = armor ID; 1 = Variable ID; 5 = Value).
Inside the plugin, it gets the armor item:

Code:
LordV.cmdSetArmorVariable = function(args) {
    var actor = $gameParty.leader();
    var itemId = Number(args[1]);
    var ID = Number(args[2]);
    var value = Number(args[3]);
    var item = actor.equips()[itemId - 1];
    this.setVariable(item, ID, value);
}

The item registers normally, I can retrieve its details, it's valid, etc, but it won't pass onto the setVariable function. Any ideas?
( see attached screen for current errors at the time of this posting )

=== For those familiar with Neverwinter Nights, or possibly some other toolsets for games, that you can place variables on objects (creatures, placeables, items, etc.) and I'm trying to replicate the same thing here 😛
Still no progress -frustrated- I beseech thee! Community!
You're aliasing Game_Item.prototype.initialize - have you checked to make sure that function exists / is called in the core code?
Hard to say from the information here...

I am noticing conflicting bits in the error log, that may or may not be issues. The error log comes from LordV.cmdSetItemVariable, but you're showing us cmdSetArmorVariable (though your plugin command may be redirecting based on item type). The log also points to setVariable being at line 202, but your image shows it starting at line 208.

Just bits to double check?
You're aliasing Game_Item.prototype.initialize - have you checked to make sure that function exists / is called in the core code?

Yup. (rpg_objects.js) line 1109 ->
Code:
Game_Item.prototype.initialize = function(item) {
    this._dataClass = '';
    this._itemId = 0;
    if (item) {
        this.setObject(item);
    }
};

But I'm starting to think this isn't the issue. I originally created all Game_Actor.prototype.<function> methods for getting the actor-based variables and switches to work, but I made them into private functions so they can be used across all of the objects listed in my OP. Hence why I've setup the plugin commands to handle all the private methods. Actors still work with this method.


Hard to say from the information here...

I am noticing conflicting bits in the error log, that may or may not be issues. The error log comes from LordV.cmdSetItemVariable, but you're showing us cmdSetArmorVariable (though your plugin command may be redirecting based on item type). The log also points to setVariable being at line 202, but your image shows it starting at line 208.

Just bits to double check?

Yeah I've been modifying the script constantly since I posted the original thumbnail (the bottom pic was actually a redo on top of the screenshot so they're not in sync). Basically the lines were telling me on the highlighted part "object._variables[variableId] = value;"
All the errors kept coming back to that specific part, so I thought something's gotta be wrong with the object (or initializing the variables[] and switches[] of that object).

I can post my current WIP script if you think that might help? Some ideas I was thinking of doing:
- Alias/Use the Game_Party() function/class instead, maybe can link to the $gameParty._lastItem ?
- Copying the private functions specifically to work with Game_Item.prototype.<function> as I did originally with my actor-based variables plugin. But then the plugin gets really long if I end up using them this way for each object, and again the private functions still work with actors, so I don't feel I /need/ to do it this way.
The full script can be useful, if you're willing to put it out there. Let's us test things I instead of simple suggestions.

The fact that you got it to work for actors but are having trouble with items tells me it could be an object reference issue - MV is weird sometimes. I'll pull up you're previous plugin when I get home and see if I can better figure out how you do things.

Beyond that, clueless for now XD.
EDIT: Oh dear... I've already noticed another problem. I can't do these as private methods... not if I intend to make this available as an "include" for other plugins.
So that means I most likely HAVE to impliment these into other classes (specifically Game_Actor for one... Game_Party... and so on). Alternatively make their own class? Game_ObjectVars() ? Then will have to include it in the game objects
$gameObjVar = new Game_ObjectVars();

Or something like that, I'm not 100% sure yet. Will play around with it a bit later, got a lot going on today.

---
I have no problem putting it out there. Any of my plugins I put out will be free for use in commercial and non-commercial anyways (credit appreciated but not necessary). So here's the current WIP plugin, ignore the "cmdSetItemVariable" method at the bottom, it - like many of the script - has been written and re-written a hundred times and I just kinda put a pause on it for the time being.
EDIT: Deleting all of this post( the old post. Everything below the line is new edits) >_> I'm a derp (didn't have the plugin turned ON) *facepalm*
----------

However still no progress. I did come up with an idea however, if anyone can enlighten me on how to accomplish it. Creating a new JSON file to store all of our objects
- Actors, Items, Party, Maps (events would be covered under this as well I believe, or Game_Interpreter, something like that), Troops, etc.

I've already begun working on a quick-note sheet with some pseudo-code to see how I can set this up. The JSON file itself would look ... eh... SOMETHING like this:
Code:
[
null,
{"id":1,"type":"item","switches":[],"variables":[]},
{"id":2,"type":"item","switches":[],"variables":[]},
{"id":3,"type":"actor","switches":[],"variables":[]}
]

Obviously would need more data within to obtain the right object. If we were to use my plugin command "SetItemVariable armor 4 1 5" again, I suppose "type":"armor" would be more effecient than "type":"item" then figuring out if it was armor...., 4 would have to reference the equip type Id (body), but then what if the item that was being worn was removed? (( could use our own "equips":[0,0,0,0,0,0] in the JSON.. ))

I just have so many questions lol Like I'm eager to learn, and I really want to complete this plugin. Not just for me, but for the community.

I already know how to create the JSON file as far as the initial stuff goes, just not how to format all the data we need to be put into it... and probably some help on the Manager stuff too (I need a new class, yes. But not sure how to format the functions).
So the plugin is nearing its close, but still hammering out a few more details, and I am even making a demo with screenshots this time since this system is a bit more complex at first. However, there is 1 more thing that is holding me up that I would like a few additional opinions on.

Item-based switches & variables. Currently the system initializes switch and variable arrays on the item objects. So like for example:
Code:
var item = $gameParty.items()[itemId - 1];
-or-
var item = $gameParty.leader().equips()[itemId - 1];

if (item._switches === undefined) item._switches = [];
if (item._variables === undefined) item._variables = [];

And it works in game sure, but doesn't carry over when Saved / Reloaded. How would I go about saving items equipped by each actor + party inventory, but... seperate data? like array0 = party's items, array1 = actor1's equipped items, array2 = actor2's equipped items, etc. and then I know to alias the DataManager but what do I put for contents?
Code:
LordV.DataManager_makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
    var contents = LordV.DataManager_makeSaveContents.call(this);
    contents.equips = ???
    return contents;
};

Alternatively, any other suggestions that I could do? One thought was to initialize an array based on item/weapon/armor IDs, but I want to only store switch/variable data on items the party/actors actually own, not just the whole item/weapon/armor lists.
Alright... plugin's done (stand alone at least). I've got some extra things to work on however, so it'll be compatible with Yanfly's scripts (Item Core mainly) .. so still a bit to do. I feel this can be closed now. Thank you all for your assistance <3
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/object-based-switches-and-variables-part-2.108948/