MV Add "event listener" for when $dataArmors or $dataWeapons length changes? (SOLVED)

Just like on the tin: Add "event listener" for when $dataArmors or $dataWeapons length changes?
Is there anything that does this and calls a function?

And it can't be the Game_Interpreter.prototype.command127-128. Because if you are using say YEP_ItemCore with independent Items it doesn't get the new item, just the base item. So the best thing would be if I could just know when and/or if the length of either database changes?

The point of this is to store the last created item.

Best regards,
all data structures are read-only, the only way to change them is the editor.
You CANNOT add to the $dataArmor etc dynamically during gameplay.

There are some plugins that allow for dynamic database extensions, but even they should not modify the data files directly but create their own structures, because due to the facts above the $data structures are never saved - any modification would be lost on saving the game.


So I suggest you take three steps back and explain how and with which plugin you are changing the weapon and armor lists, then someone can check where that new data is really stored and you can explain what you want to do with that info.
all data structures are read-only, the only way to change them is the editor.
You CANNOT add to the $dataArmor etc dynamically during gameplay.

There are some plugins that allow for dynamic database extensions, but even they should not modify the data files directly but create their own structures, because due to the facts above the $data structures are never saved - any modification would be lost on saving the game.


So I suggest you take three steps back and explain how and with which plugin you are changing the weapon and armor lists, then someone can check where that new data is really stored and you can explain what you want to do with that info.
As I wrote, it's for storing last item that's been created. And I did write it is for Yanfly_ItemCore. So I don't really enjoy being told to take steps back :/ But I will explain more properly, since that is the main point of your posted question.

What I am currently doing is:
JavaScript:
//Gain Weapon -- Measuring the length of the database before independent item is added
    (function(alias) {
        Game_Interpreter.prototype.command127 = function() {
            BISH._preWeaponLength = $dataWeapons.length;
            console.log("$dataWeapons.length pre: " + $dataWeapons.length)
            return alias.apply(this, arguments);
        }
    }) (Game_Interpreter.prototype.command127);

    //Gain Armor -- Measuring the length of the database before independent item is added
    (function(alias) {
        Game_Interpreter.prototype.command128 = function() {
            BISH._preArmorLength = $dataArmors.length;
            console.log("$dataArmors.length pre: " + $dataArmors.length)
            return alias.apply(this, arguments);
        }
    }) (Game_Interpreter.prototype.command128);


//The compatibility update for Yanfly_ItemCore:
if (Yanfly)
    if (Yanfly.Item)
        (function(alias) { //Add last item parameters to independent items. -- For Yanfly_ItemCore.
            Game_Party.prototype.registerNewItem = function() {
                alias.apply(this, arguments);
                try {
                    BISH.lastItem();
                    BISH.setItem(newItem);
                } catch(e) {};
            }
        }) (Game_Party.prototype.registerNewItem);

//The function I am using to set lastItem:
    BISH.lastItem = function() {
        try {
            if (BISH._preWeaponLength < $dataWeapons.length) {
                lastCreatedItem = $dataWeapons[$dataWeapons.length - 1];
                BISH._preWeaponLength++;
            }
            if (BISH._preArmorLength < $dataArmors.length) {
                lastCreatedItem = $dataArmors[$dataArmors.length - 1];
                BISH._preArmorLength++;
            }
            BISH.setItem(lastCreatedItem);
        } catch(e) {console.error(e)};
    };

This works very well with Yanfly_Itemcore. The issue that I can see is exactly what you wrote, that some plugins allow for the changing of the databases length dynamically, not just Yanfly. And I'd prefer to have a function that simply triggers if either the weapon or armor databases lengths have been extended. This would improve potential compatibility.

If there would be a better way of doing this, when preparing for unknown plugins that add new items and such. That would be some greatly appreciated information 🙂
The issue that I can see is exactly what you wrote, that some plugins allow for the changing of the databases length dynamically, not just Yanfly.
unfortunately what is needed here is usually called a compatibility patch for specific plugins.

In my original post I didn't understand that the main reason for your question was to keep compatibility with other plugins affecting the same structure - mostly because in my opinion mixing several plugins for dynamic databases is a very bad idea.

Because there is a possibility that you completely ignored in your planning: what if the new entries are added outside the original data structure?
A plugin could theoretically work in a way that it checks the index and then branches like (index below original_index, go $data[index] elseif index above original index, go $adddata[index-original_index])


It is relatively easy to write a patch for a dynamic database plugin that you know of - like you did with the yanfly patch. All you would need is add your own function call inside the plugin function that adds to the database.

but preparing compatibility for an unknown plugin that changes the database in an unknown way?
I don't think that you'll ever get that done, you would need at least some info about what that other plugin does before programming a patch.

And don't forget that it will be the game developer who decides on plugins. Yes, I know that there are a lot of users who add plugins without care - but to have a chance of really completing a game you'll need to show more planning, and in those cases they won't need such protections from you because they'll know that adding multiple plugins with dynamic database functions can cause problems.
Please read what Andar wrote, extending 3rd Party Plugins usually is a bit tricky. However, if you want to listen on changes on $dataXY, you can try to use proxies: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy I ever dealt with that, but so far as I read the doc, you can listen on e.g. $dataItems.push
unfortunately what is needed here is usually called a compatibility patch for specific plugins.

In my original post I didn't understand that the main reason for your question was to keep compatibility with other plugins affecting the same structure - mostly because in my opinion mixing several plugins for dynamic databases is a very bad idea.

Because there is a possibility that you completely ignored in your planning: what if the new entries are added outside the original data structure?
A plugin could theoretically work in a way that it checks the index and then branches like (index below original_index, go $data[index] elseif index above original index, go $adddata[index-original_index])


It is relatively easy to write a patch for a dynamic database plugin that you know of - like you did with the yanfly patch. All you would need is add your own function call inside the plugin function that adds to the database.

but preparing compatibility for an unknown plugin that changes the database in an unknown way?
I don't think that you'll ever get that done, you would need at least some info about what that other plugin does before programming a patch.

And don't forget that it will be the game developer who decides on plugins. Yes, I know that there are a lot of users who add plugins without care - but to have a chance of really completing a game you'll need to show more planning, and in those cases they won't need such protections from you because they'll know that adding multiple plugins with dynamic database functions can cause problems.
I suppose that you are correct in some ways... But I do think there is a reason to at least try to make the plugin compatible with as many situations as possible without making patches for specific plugins. And I also want to have a lastCreatedItem that is as foolproof as possible.

And as long as the plugin in being used only adds another item to a database, the length should theorethically be enough to prevent any issues. But if there are two plugins creating two copies and extending the length by two, I am still comparing the pre and post length of the database, so I could just loop through the difference. But in the end, such an error would be on the user to correct tbh.

And when the length changes I'll just parse the latest entry into my function to store the last created item.

I think what I will do is read up on the thread from @Aerosys, then I will see if that would be a possible solution.

I suppose fixing patches for the main plugins would be the way to go otherwise. Perhaps there isn't too many out there, personally only ever used Yanfly for these types of things. Do you know a few ones that a user is likely to be using?

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/add-event-listener-for-when-dataarmors-or-dataweapons-length-changes-solved.141707/