Steal plugin help

Hello all.

I'm working on a steal skill plugin that reads the enemy drops instead of requiring any notes added in the enemy or troop fields.

After a few weeks of working on it, I finally got an item to show up in the player's party. Only problem is, it gives the player all the drops the troop has, as opposed to giving them one item. I've played around with the code a bit as you can see in the comments 

Code:
Game_Enemy.prototype.makeStealDrops = function () {		 return this.enemy().dropItems.reduce(function(r, di) {        if (di.kind > 0) {            return r.concat(this.itemObject(di.kind, di.dataId));        } else {            return r;        }    }.bind(this), []);	};Game_Troop.prototype.steal = function(){		var items = this.makeStealDrops();	$gameMessage.add("Testing 1 & 2 ");  //debug line		items.forEach(function(item) {        $gameParty.gainItem(item, 1);    });};Game_Troop.prototype.makeStealDrops = function() {		return this.aliveMembers().reduce(function(r, enemy) {			return r.concat(0, 0, enemy.makeStealDrops());		}, []);	};//////        This one doesn't work but doesn't crash///*Game_Troop.prototype.makeStealDrops = function() {		return this.aliveMembers().reduce(function(r, enemy) {			return r.splice(0, 0, enemy.makeStealDrops());		}, []);	};*/	///////       This one crashes the game with undefined function error////*Game_Troop.prototype.makeStealDrops = function() {		return this.aliveMembers().reduce(function(r, enemy) {			return r.inject(enemy.makeStealDrops());		}, []);	};*/
It's giving one of each item, because that's what you're telling it to do.

items.forEach(function(item) {$gameParty.gainItem(item, 1);});If you only want to give one of the items randomly, then you need to make a random number between 1 and the number of elements in the array (inclusive), and give just the item with that element.
Hrm. That actually opens up a new problem. I was talking about the number of items (1 potion for each member of the troop, for example), but since you mentioned that it's apparent that it will also give all the drops as well.

I really hate arrays and array like objects 😀

Back to coding. I'll check back in once I get some more headway done should I have any more problems or to share a solution.
If your items array is a combination of all the drop items for all members of the enemy troop, it should work.

That's what I'd expect the default scripts to do, except it would choose randomly between them, I think.  Have you had a close look at the default scripts?
I've been over everything that mentions drops, item, items, gainItem, etc. Been over the manager (the current version that gives all items is based on the BattleManager code for rewarding ) There's probably something really obvious that I'm missing that will have me smdh at the end of next week.

I find the scripts I have the most problems with are the ones I had working perfectly in Ace. I know javaScript a lot better - which actually makes it harder to translate from RGSS3 where I had no idea what I was doing but that it worked 😀
Okay! 

I've got it down to where if the enemy has multiple drops it only gives one of the items. Now to just cut down on the number of that one item. 

Game_Troop.prototype.makeStealDrops = function() { return this.aliveMembers().reduce(function(r, enemy) { return r.concat(enemy.makeDropItems()[0]); }, []);};I'm pretty sure the solution is going to be something along the lines of accessing just one of the aliveMembers in the troop but the standard methods (this.aliveMembers()[x], or even this.aliveMembers(1) and such) keep throwing up an undefined error. I've also tried just accessing the this.members array to no avail.
Gwalxavad, please avoid double posting, as it is against the forum rules. You can review our forum rules here. Thank you.
I still don't understand how many of what are you trying to give to the player while stealing.

How many times can player steal in one battle?

If drops are 1xPotion, 2xHi Potion and 5xRandom Leaf, what will it do?

Will it take random number of random drop, but less than max of that drop, like, it can't steal 2xPotion, or 1 of random, like 1xRandom Leaf, or it'll take all of random drop, like 2xHi Potion?
Sorry the reply took a few days. I was out of town for the weekend and internet connection was spotty at best.

Basically, the player can attempt to steal as much as they want in a battle. We've got an algorithm running elsewhere that controls the formula that prevents steal from being a 1:1 success. The code I'm having problems with is something that runs after the chances of success have been determined.

If drops are 1xPotion, 2x Hi Potion, and 5x Random Leaf we'd like it to select one of the drops and give the player that drop and that drop only. Preferably it will give the drop of just the enemy that it's stealing from - so if the enemy itself (say a slime) drops 1xPotion but is in a troop with two other slimes and a spider that drops the 2xHi Potion we'd want it to just give the 1x Potion. If the player then attempts to steal from the spider and is successful, they'd get the 2x Hi Potion etc.

No random drops and no random numbers. 1xPotion if stealing from a slime, 2xHi Potion if stealing from a spider, 5x Random Leaf if stealing from an Epileptic Tree. 
What if slime has more than one drop?

If, like, one slime could drop both potions and leaves?
Then we want slime to just drop one. I've already got that figured out, it's easily done by entering in the array number for the already existing GameEnemy.prototype.makeDropItems() function in brackets following the call.

Edit: Update on what I'm discovering so far:

Okay, so the code as it works right now basically gives the first drop for every enemy in the troop. So, if a troop is only composed of one enemy of the same type with multiple drops types, it give 3 potions (just for example). If there are different types of enemies with different types of drops then it gives the first drop for each type of enemy.

Example - If a slime drops potions and a bat drops Ear Drops then steal will drop 1 (one) potion and ear drop each.

Game_Troop.prototype.makeStealDrops = function() { return this.aliveMembers().reduce(function(r, enemy) { return r.concat(enemy.makeDropItems()[0]);//the array accesses the first drops of all enemies //in the troop with the current call. }, []);};Edit: Update 2

Okay, got it working so that it only gives one. As I stated earlier in this thread, it was a smh solution. In the function where I assign the item, it was the forEach function that was assigning items for each member of the troop.

Game_Troop.prototype.steal = function(){ var items = this.makeStealDrops(); var setup = this.makeStealDrops().length; var amount = setup.toString(); $gameMessage.add(amount); //debug line items.forEach(function(item) { $gameParty.gainItem(item, 1); //$gameParty.remove //$gameMessage.add(items); }, 0);}; Replaced with:

Game_Troop.prototype.steal = function(){ var items = this.makeStealDrops(); var setup = this.makeStealDrops().length; var amount = setup.toString(); $gameMessage.add(amount); //debug line var item = items[1]; $gameParty.gainItem(item, 1); }; Now, I just need a way to determine which member of the troop is being stolen from and I can assign the correct drop with a little bit more coding.

Original thread: https://forums.rpgmakerweb.com/threads/steal-plugin-help.50337/