Sort Items by how they're found in-game

Hello,
I usually google extensively for the stuff I'm looking for, but I'm just really surprised that this topic seems to never have popped out anywhere. I'm kinda suprised it's not a default option in Rpg Maker MV at all. At this point, I just have to ask directly.

How hard would it be to create a plugin that sorts the Item database (though I can imagine other people would also care for the skills one) by the order those items are found in-game?

Melosx posted a plugin to sort them alphabetically:
https://forums.rpgmakerweb.com/index.php?threads/msx-alphabetical-sort.73991/

While Aloe Guvner posted one to sort them through any order you wish, by the use of notetags:
https://forums.rpgmakerweb.com/index.php?threads/item-and-skill-sort-priority.90931/

Unfortunately, I know next to zero about Java and the internals of Rpg Maker MV - so I can't even theorize how easy this would be. I assume it wouldn't be too hard, if the engine already keeps track somehow of the order items are acquired. But I don't know if that case.

Thanks a lot in advance!
it is not as easy as you think.

First, everything ingame works by the ID - which is why that is the default sort. The ID cannot be changed as that would cause a lot of problems.
Usually all other sort options are based on a fixed identification method - because that let's the plugin check everything and use a new value with the fixed order of the other method.

Having the order based on appearance in the game means that the order is not fixed but dynamic - two playthroughs will have a different order because the players might take or purchase items at different times.
But you can't simply have a plugin watch the inventory - that would create a lot of lag for no real cause.
What you need to do is have the engine tell the list "set this item as newest on the list if it is not yet set".

And that command needs to be given at each variant of how the player can gain something - event command change item, battle result item found, shop processing item purchased - and that might or might not include functions if someone uses javascript to add an item.

Yes, it can be done - but it is not quick as the plugin programmer needs to find every possible function that could be used to gain an item and change it.
it is not as easy as you think.

First, everything ingame works by the ID - which is why that is the default sort. The ID cannot be changed as that would cause a lot of problems.
Usually all other sort options are based on a fixed identification method - because that let's the plugin check everything and use a new value with the fixed order of the other method.

Having the order based on appearance in the game means that the order is not fixed but dynamic - two playthroughs will have a different order because the players might take or purchase items at different times.
But you can't simply have a plugin watch the inventory - that would create a lot of lag for no real cause.
What you need to do is have the engine tell the list "set this item as newest on the list if it is not yet set".

And that command needs to be given at each variant of how the player can gain something - event command change item, battle result item found, shop processing item purchased - and that might or might not include functions if someone uses javascript to add an item.

Yes, it can be done - but it is not quick as the plugin programmer needs to find every possible function that could be used to gain an item and change it.

I see, so the engine doesn't track this at all and it's a mess to find a solution.

Though, in my specific case I wouldn't mind setting up the command once every item is gained in-game, as I have a set limited of items given in very specific circumstances. I don't know if anyone is interested in making such a plugin, though.
If you only put the command once all items are found, then during any time before that happens, your inventory will still be on the default ID based sorting. Then suddenly it changes to which was obtained latest. Thats a bit confusing to the players.

Also, the fact is that the game doesnt track when you gain a specific item, so even if you only want it to change after getting all items, we will still need to track the items gained in real time to set the correct order in the end. So when you want to call the sort doesn't actually matter. The "list" of how they will be sorted should always be available, else you cant sort them. Creating this "list" or property is where the bulk of the work comes in and as Andar said, its quite a huge work to do.

Now if you have a controlled time when each item can be gained which will be followed always, you can just sort them on the database.
@Engr. Adiktuzmiko there is a way without such problems, I just didn't go into thedetails in my post.
a plugin needs to add a new property "order" to each item and that will be used for sorting. it would default to zero and be set automatically when an item is added to inventory.
the functions that add the items to inventory would then check if the order-property of the item is still zero or not, and if it is still zero replace it with the current highest number +1. That is what I meant with the plugin writer having to identify and change every function and place that can add items, because the check for the order property would have to be added there (the game would get a distorted inventory or crash if an item was added without setting the order property)
I know, that is why I said:

"Creating this "list" or property"

And that will still be where the bulk of the plugin will be focused on..

it would default to zero and be set automatically when an item is added to inventory.
the functions that add the items to inventory would then check if the order-property of the item is still zero or not, and if it is still zero replace it with the current highest number +1.

And that's exactly how you track them in real-time, set the property the moment you obtain the item.

As far as Im concerned, we are thinking of the same implementation.

Though I kind of think checking if the property is zero should be optional/changegable due to how multiples of each item get stacked by the system instead of instanced individually..

It should be optional in case the developer wants to either just consider the very first instance you get an item (Check if zero, doesnt update even if you get another piece of that item), or if they want to consider every instance of obtaining the item (everytime you get an item or copy of it, set it to the next higher order)
Yeah. I was concerned on how this would handle non-instance items.
If I started with a healing potion, got 20 other items then got another potion in the stack, how would that sort? Instance items would be fairly easy to implement by adding the counting element to gainItem() but multiples or stackables would be tricky.
This is a very old thread, and I don't know if for @_Soysauce_ this is still relevant, but while I was just searching to a solution of sorting items by myself, I landet here.

Reading all your answers and suggestions, I believe the answer was already here - or at least you were all close to it.

So I managed to do that task with very few lines of code and I would like to share this. I would like to hear if I have forgotten something at last?

My addition to this task is to just add one property to the "item" objects:
item._lastGainedDate which is set exactly in the function mentioned by @Maliki79 and set it to "Date()".

Hit me, but I believe this is the only function that covers all the possibilities that @Andar was concerned about. (I tried it and even if you stripp off a weapon that had been on the actor at game start, this function is called to put it to the menu!).

So the code is looking like that:
JavaScript:
former_Game_Party_gainItem = Game_Party.prototype.gainItem;
Game_Party.prototype.gainItem = function(item, amount, includeEquip) {
    // only when gaining item not by loosing
    if (amount > 0) {
        if (item) {
            if (!item._lastGainedDate) {
                item._firstGainedDate = Date();
            }
            item._lastGainedDate = Date();
        }   
    }


    former_Game_Party_gainItem.call(this, item, amount, includeEquip);
};


The function to sort by last acquired Item would look like this:
JavaScript:
//sort Items by last gained Date (last will be first)
Window_ItemList.prototype.makeItemList = function() {
    this._data = $gameParty.allItems().filter(item => this.includes(item));

    this._data.sort(function(a,b){
        if(a._lastGainedDate < b._lastGainedDate) return 1;
        if(a._lastGainedDate > b._lastGainedDate) return -1;
        return 0;
    });

    if (this.includes(null)) {
        this._data.push(null);
    }
};

I put everything in a plugin and it works quite decent!

(You may have noticed that I added another timestamp called item._firstGainedDate, so you could also sort by first acquisition of this item type)

So for me the answer is to note everything to the item id and to not try to think about single item "instances". That gives a simple solution that should also be fast in excecution.

Please comment, if you still see any issues.

My question would be how to make it possible to the user to choose a sorting option from the menu itself (how to add a kind of sorting option subwindow). If anybody has an idea on that, I'd be glad to hear about it 🙂

Original thread: https://forums.rpgmakerweb.com/threads/sort-items-by-how-theyre-found-in-game.108803/