RMMV Count dropped items in battle

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
838
Reaction score
467
First Language
Poland
Primarily Uses
RMMV
Let's clarify things first, I'm very well aware of Yanfly's Extra Enemy Drops plugin, that would solve my problem, but I don't need that plugin in overall, I modified drop script like that:
JavaScript:
Soulrender.prototype.displayRewards = function(){
    $gameMessage.clear();   
    var items = '';
    var gold = $gameTroop.goldTotal();
    var exp = $gameTroop.expTotal();   
    var spoils = BattleManager._rewards.items;
    var i = 0;
    var count = 1;
    if (spoils.length > 0) {
        spoils.forEach(function(item) {
            i += 1;
            items += "<br>#" + i + " \\i[" + item.iconIndex + "] " + item.name + " (" + count + "x)";
        });
    } else { items = "<br> \\hc[ff7366]► No Items\\hc[ffffff]"; }
        
    $gameMessage.setPositionType(1);
    $gameMessage.setBackground(0);       
    $gameMessage.add("\\msgrows[auto]<center>\\fs[28]\\hc[32cd32]You are Victorious!\\hc[ffffff]</center><WordWrap>\\fs[20]<br>Experience: \\hc[ffa81c]+" + exp + " XP\\hc[ffffff]<br>\\fs[20]Gold: \\hc[ffa81c]" + gold + "\\hc[ffffff]<br>Items:" + items);   
}
It show dropped items and generaly I could go with it, but I think it will better look in message window when the result will look like that:

- Some Item A (4x)
- Some Item B (3x)

instead like currently
#1 Some Item A (1x)
#2 Some Item A (1x)
#3 Some Item A (1x)
#4 Some Item A (1x)
#5 Some Item B (1x)
#6 Some Item B (1x)
#7 Some Item B (1x)
 
Last edited:

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,071
Reaction score
1,493
First Language
EN
Primarily Uses
RMMZ
So:
  • You have this: [item, item, item, item, item, item, ...]
  • You want this: [{item, number}, {item, number}, ...]
From here we can see that two things need to happen:
  1. Store how many times each item appears, and
  2. Remove duplicates from the list.
You can see a loop is going to be necessary here, since we need to check each item. One possible approach:
JavaScript:
var mem = [], num = [];
BattleManager._rewards.items.forEach(function(item) {
    var n = mem.indexOf(item);
    if (n < 0) mem.push(item);
    num[item.id] = (num[item.id] || 0) + 1;
});
console.log(mem.map(function(item) {
    return { i: item, n: num[item.id] };
}));
This outputs an array of objects; your example would become:
JavaScript:
[{ i: ItemA, n: 4 }, { i: ItemB, n: 3 }]
This gives you the necessary information for your "ItemName (xN)" type messages. :kaothx:

(I'm sending output to the console here but you can easily redirect that to a function output or whatever~)
 

RinTheWanderer

Villager
Member
Joined
Aug 23, 2020
Messages
15
Reaction score
14
First Language
English
Primarily Uses
RMMZ
The guy above me has a much cleaner solution but I already went through this so I'm posting it. If you use regex you can stack some of the work up and end up with a formatted string. Then you can just add it the same way you did. You just replace the +items with +stackedSpoils and that should be it.

var stackedSpoils = "";
var ind = 0;
spoils.map(function(spoil){
if (stackedSpoils.includes(spoil.name + ' (')) {
stackedSpoils = stackedSpoils.replace(new RegExp(spoil.name + "\\s\\(x([0-9]+)"), function(match, p1) { return spoil.name + ' (x' + (parseInt(p1)+1)})
}else {
ind++
stackedSpoils += "<br>#" + ind + \\i[" + spoil.iconIndex + "] " + spoil.name + ' (x1)'
}}
)

This will cause the stackedSpoils to look like
'#1 [item icon] Some Item A (x4)
#2 [item icon] Some Item B (x3)'.

EDIT: Just realized this was for MV so the arrow notation doesn't work. Changing it to function notation.
 
Last edited:

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,071
Reaction score
1,493
First Language
EN
Primarily Uses
RMMZ
EDIT: Just realized this was for MV so the arrow notation doesn't work. Changing it to function notation.
RMMV upgraded to ES6 with v1.6 of the editor, so arrow functions should be OK in the latest versions. Safer to stick with ES5, though! ^-^
 

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
838
Reaction score
467
First Language
Poland
Primarily Uses
RMMV
@RinTheWanderer oh crap... I was using filter() instead of map() and I had very strange results in shown dropped items.

@caethyril - As always irreplaceable man, I already spoke what I have on my mind in recent issue, so to avoid repeating my self, you have my personal guarantee you will have a major role in my project I'm working on :)
 

RinTheWanderer

Villager
Member
Joined
Aug 23, 2020
Messages
15
Reaction score
14
First Language
English
Primarily Uses
RMMZ
Oh, well thats nice that I can use arrow notation in MV. Might as well get used to it sooner than later.
oh crap... I was using filter() instead of map() and I had very strange results in shown dropped items.
I honestly tried to figure out how to make it work with filter but it wasn't worth it cause I couldn't modify the resulting array in the middle of the filter. I think you could technically do it with reduce by reducing it into a string but there isn't really any benefit to doing that over just iterating through. Map is just a lazier/cleaner way to do forEach
 

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
838
Reaction score
467
First Language
Poland
Primarily Uses
RMMV
Anyway my problem with drop now is solved. Thank you both gentelmen.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Thank you all angels so much! I've received fanarts, fanfics, song cover, and Ko-fis and such wonderful messages already! T uT Look at this message I'm crying (Ko-fi was being mean saying it was anonymous but she was like nooo it's me and showed me).
Working on using DreamX's prefix suffix plugin to create unidentified weapons, armor and sellable loot. The weapons and armor will always draw from the highest tier you have the ability to create, keeping them relevant throughout most of the game. The loot is going to be the fun part! Let's just say I doubt I'll have any unused item entries
... I updated an enemy I drew last year.
My dynamic weather system grows... now with cloud cover influenced by wind speed! (4x playback) (Not final cloud image/map)
Wrote a plugin today that allows you to assign BGM, Battlebacks, and even Battle Entry SE's on a per-troop basis. Introducing a random encounter with a high-pitched Chicken Squeal is worth it all by itself.

Forum statistics

Threads
105,765
Messages
1,016,266
Members
137,452
Latest member
12dl
Top