Script giving item of most quantity another of same item

Hi, i'm hoping someone might be able to help me with because I don't think my problem can be solved using just the MVs engine commands.

So what I'm looking for is some script I can insert that checks the amount of each of the player's items between items 101 - 110, and after which it adds one more to the item that has the most. In the instance 2 or more items have the same amount it should randomly chose which one to add to.

There's another condition, if the player has x8 of an item it should not give anything and instead add to the item with the next highest amount. If the player has 8 of every item in this range, it should instead give 100 gold.

Finally a message should come up and say "You have been awarded [Item name here] / [100 gold]!"

If anyone can help with this, I'd be grateful!
Hi, i'm hoping someone might be able to help me with because I don't think my problem can be solved using just the MVs engine commands.

So what I'm looking for is some script I can insert that checks the amount of each of the player's items between items 101 - 110, and after which it adds one more to the item that has the most. In the instance 2 or more items have the same amount it should randomly chose which one to add to.

There's another condition, if the player has x8 of an item it should not give anything and instead add to the item with the next highest amount. If the player has 8 of every item in this range, it should instead give 100 gold.

Finally a message should come up and say "You have been awarded [Item name here] / [100 gold]!"

If anyone can help with this, I'd be grateful!
I'd do something like

JavaScript:
const items = $dataItems.filter(item => item && item.id >= 101 && item.id <= 110 && $gameParty.numItems(item) < 8);
if (items.length > 0) {
    const possibleItems = items.filter(item => $gameParty.numItems(item) === Math.max(...items.map(item => $gameParty.numItems(item))));
    const item = possibleItems[Math.randomInt(possibleItems.length)];
    $gameParty.gainItem(item, 1);
    $gameMessage.add(`You have been awarded ${item.name}!`);
} else {
    $gameParty.gainGold(100);
    $gameMessage.add("You have been awarded 100 gold!");
}
I'd do something like
Gave it a quick whirl and it seems to work exactly as I described. Thanks so much! You are awesome!
Gave it a quick whirl and it seems to work exactly as I described. Thanks so much! You are awesome!
Do you want me to explain to you what it does and why it works or are you happy with it just working? 😀
I would like to read an explanation of your code, not that it's wrong or something, just my hunger for knowledge.
Do you want me to explain to you what it does and why it works or are you happy with it just working? 😀

I'm always happy to learn more. I'm no JavaScript whizz (evidently!) but I'm not a complete newbie either, so looking at your script closely I can at least understand some of what is going on there. And I recognise some familiar elements. Just lacked the knowledge to put this all together as nicely like you have!
I'd do something like

JavaScript:
const items = $dataItems.filter(item => item && item.id >= 101 && item.id <= 110 && $gameParty.numItems(item) < 8);
if (items.length > 0) {
    const possibleItems = items.filter(item => $gameParty.numItems(item) === Math.max(...items.map(item => $gameParty.numItems(item))));
    const item = possibleItems[Math.randomInt(possibleItems.length)];
    $gameParty.gainItem(item, 1);
    $gameMessage.add(`You have been awarded ${item.name}!`);
} else {
    $gameParty.gainGold(100);
    $gameMessage.add("You have been awarded 100 gold!");
}
A slight modification I'd make is to cache the Math.max call so that you only run it once instead of 10 times.
Code:
const items = $dataItems.filter(item => item && item.id >= 101 && item.id <= 110 && $gameParty.numItems(item) < 8);
if (items.length > 0) {
    const highestCount = Math.max(...items.map(item => $gameParty.numItems(item)));
    const possibleItems = items.filter(item => $gameParty.numItems(item) === highestCount);
    const item = possibleItems[Math.randomInt(possibleItems.length)];
    $gameParty.gainItem(item, 1);
    $gameMessage.add(`You have been awarded ${item.name}!`);
} else {
    $gameParty.gainGold(100);
    $gameMessage.add("You have been awarded 100 gold!");
}

Since people are asking:
The first line looks at all items the player currently has and filters them for:
  • Item exists (handles for if an item is defined in the database, but the player doesn't have any of it)
  • The Id of the item is between 101 and 110
  • The number of that item in the inventory is less than 8
The items that meet those criteria are cached in a variable called items.

Then we have a conditional saying if there's at least one item that met that criteria, additionally filter them by figuring out what's the highest count of the various items, using a few tools:
  • Math.max takes in any number of number arguments and returns the highest value. You can't directly pass an Array of numbers, but you can use spread syntax to convert an Array into a number of arguments equal to the contents of the Array
  • Array.prototype.map can be called on an Array to basically convert the Array into another Array. So the items Array is an Array of objects that have a number of properties and methods, but we're using .map to instead convert it from an Array of objects into an Array of numbers representing the count of each object
So the original code posted says, for each item that met the original criteria, filter the list again for:
  • Find the highest count of items using .map to convert all the items in the list into the number of items the player has, then using Math.max to return the highest number
  • If the count of that item is equal to the highest count, keep it
That resulting Array of additionally-filtered objects is then randomly chosen from - if there's only one item, then it would be the only one chosen. The chosen item selected at random is called item.

The rest is pretty self-explanatory.

The only change I'm suggesting is that the original code performs the "find the highest count of items using .map..." step once for each item in the items Array - but that number will never change and only ever needs to actually be checked once, so my suggested refactor is to find it once instead of once per item.
A slight modification I'd make is to cache the Math.max call so that you only run it once instead of 10 times.
Code:
const items = $dataItems.filter(item => item && item.id >= 101 && item.id <= 110 && $gameParty.numItems(item) < 8);
if (items.length > 0) {
    const highestCount = Math.max(...items.map(item => $gameParty.numItems(item)));
    const possibleItems = items.filter(item => $gameParty.numItems(item) === highestCount);
    const item = possibleItems[Math.randomInt(possibleItems.length)];
    $gameParty.gainItem(item, 1);
    $gameMessage.add(`You have been awarded ${item.name}!`);
} else {
    $gameParty.gainGold(100);
    $gameMessage.add("You have been awarded 100 gold!");
}

Thanks for detailed explanation and the suggestion too! I've added your change.

Also, if I wanted to add a variable amount to the gold message what is the correct way. I tried:

Code:
$gameMessage.add(`You found $gameVariables.value(84) gold!`);

But it displays as is.
$gameMessage.add("You found" + $gameVariables.value(84) + " gold!");

should do the trick as it you give a string name, than gain the value of 84, than at text again.
$gameMessage.add("You found" + $gameVariables.value(84) + " gold!");

should do the trick as it you give a string name, than gain the value of 84, than at text again.
Thanks! I Also added it to the first line with the item and although a bit clunky to write out with all the spaces and quotation marks and make sure it's all spaced correctly, got it working in the end.
Thanks for detailed explanation and the suggestion too! I've added your change.

Also, if I wanted to add a variable amount to the gold message what is the correct way. I tried:

Code:
$gameMessage.add(`You found $gameVariables.value(84) gold!`);

But it displays as is.
You'd use the same string interpolation method as the first one did.

$gameMessage.add(`You found ${$gameVariables.value(84)} gold!`);

A slight modification I'd make is to cache the Math.max call so that you only run it once instead of 10 times.
Code:
const items = $dataItems.filter(item => item && item.id >= 101 && item.id <= 110 && $gameParty.numItems(item) < 8);
if (items.length > 0) {
    const highestCount = Math.max(...items.map(item => $gameParty.numItems(item)));
    const possibleItems = items.filter(item => $gameParty.numItems(item) === highestCount);
    const item = possibleItems[Math.randomInt(possibleItems.length)];
    $gameParty.gainItem(item, 1);
    $gameMessage.add(`You have been awarded ${item.name}!`);
} else {
    $gameParty.gainGold(100);
    $gameMessage.add("You have been awarded 100 gold!");
}

Since people are asking:
The first line looks at all items the player currently has and filters them for:
  • Item exists (handles for if an item is defined in the database, but the player doesn't have any of it)
  • The Id of the item is between 101 and 110
  • The number of that item in the inventory is less than 8
The items that meet those criteria are cached in a variable called items.

Then we have a conditional saying if there's at least one item that met that criteria, additionally filter them by figuring out what's the highest count of the various items, using a few tools:
  • Math.max takes in any number of number arguments and returns the highest value. You can't directly pass an Array of numbers, but you can use spread syntax to convert an Array into a number of arguments equal to the contents of the Array
  • Array.prototype.map can be called on an Array to basically convert the Array into another Array. So the items Array is an Array of objects that have a number of properties and methods, but we're using .map to instead convert it from an Array of objects into an Array of numbers representing the count of each object
So the original code posted says, for each item that met the original criteria, filter the list again for:
  • Find the highest count of items using .map to convert all the items in the list into the number of items the player has, then using Math.max to return the highest number
  • If the count of that item is equal to the highest count, keep it
That resulting Array of additionally-filtered objects is then randomly chosen from - if there's only one item, then it would be the only one chosen. The chosen item selected at random is called item.

The rest is pretty self-explanatory.

The only change I'm suggesting is that the original code performs the "find the highest count of items using .map..." step once for each item in the items Array - but that number will never change and only ever needs to actually be checked once, so my suggested refactor is to find it once instead of once per item.
Yeah, absolutely! The only reason I didn't do that was because I was whipping up a quick implementation without thinking too much about efficiency. XD With only 10 checks it's largely negligible anyway, but it's never a bad thing to cut down on unnecessary operations. Very thorough explanation of my code too, thank you for that.
You'd use the same string interpolation method as the first one did.

$gameMessage.add(`You found ${$gameVariables.value(84)} gold!`);
Ah thanks again. I actually tried that first time, but I missed the double $ there in - ${$ -, so it didn't work. That's definitely a less clunky looking solution.
Ah thanks again. I actually tried that first time, but I missed the double $ there in - ${$ -, so it didn't work. That's definitely a less clunky looking solution.
Yeah, it looks tricky like that but all you need to remember is that with ${stuff} "stuff" is what gets evaluated and inserted into the string, and $gameVariables was the object you were accessing a value from.
Yeah, it looks tricky like that but all you need to remember is that with ${stuff} "stuff" is what gets evaluated and inserted into the string, and $gameVariables was the object you were accessing a value from.

I have another thing I'd like to add to your script if possible. I'd like it so the item also adds +1 to a variable. All my variables in this case are assigned the exact same number as the item. So let's say your script picks item 111, what I want it to do is add 1 to variable 111.

I've tried adding this before the else condition:

Code:
$gameVariables.setValue($gameVariables.value(item.id), $gameVariables.value($gameVariables.value(item.id)) +1);

But that doesn't seem to work.
I have another thing I'd like to add to your script if possible. I'd like it so the item also adds +1 to a variable. All my variables in this case are assigned the exact same number as the item. So let's say your script picks item 111, what I want it to do is add 1 to variable 111.

I've tried adding this before the else condition:

Code:
$gameVariables.setValue($gameVariables.value(item.id), $gameVariables.value($gameVariables.value(item.id)) +1);

But that doesn't seem to work.
The first argument to setValue is just the ID number of the variable, not the value of it.

$gamrVariables.setValue(item.id, $gameVariables.value(item.id) + 1);
The first argument to setValue is just the ID number of the variable, not the value of it.

$gamrVariables.setValue(item.id, $gameVariables.value(item.id) + 1);

Oh right. Yeah that was because I am using that part elsewhere and it required the value. I was close. Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/script-giving-item-of-most-quantity-another-of-same-item.149649/