Counter number of items obtained from database

Is there any script call to count items in the inventory? example if the player has obtained item ID 1 to 4 in the data base, you can store it in variable, and the value should be 4, and is it possible to count the total number of possessions, if there are 4 pcs of each, then the value would be 16?
the shortest way to accomplish that in a scriptcall or rather in a
conditional branch is to use ConditionalBranch+ which can check
the items + amount of it.

read the helpfile good, so you know how to check them correctly.
Example with event commands:
Code:
◆Control Variables:#0007 Count = The number of Potion
◆Control Variables:#0007 Count += The number of Magic Water
◆Control Variables:#0007 Count += The number of Dispel Herb
◆Control Variables:#0007 Count += The number of Stimulant
◆Text:None, Window, Bottom
:    :I counted all your Potions, Magic Waters
:    :Dispel Herbs and Stimulants.
:    :
:    :\|You have \v[7] of these in total!
To count all items (Items and unequipped Armors/Weapons) you could try Control Variables > Script:
JavaScript:
$gameParty.allItems().reduce(function(a, c) { return a + $gameParty.numItems(c); }, 0)
To count a different category, replace allItems() with:
  • items() for Items only (no Armors or Weapons)
  • weapons() for Weapons only
  • armors() for Armors only
  • equips() for Armors and Weapons only
I think it would become much more complicated if you need to count equipped items as well.
To count all items (Items and unequipped Armors/Weapons) you could try Control Variables > Script:
JavaScript:
$gameParty.allItems().reduce(function(a, c) { return a + $gameParty.numItems(c); }, 0)
yes, this one works!

Example with event commands:
Code:
◆Control Variables:#0007 Count = The number of Potion[/INDENT]
[INDENT]◆Control Variables:#0007 Count += The number of Magic Water[/INDENT]
[INDENT]◆Control Variables:#0007 Count += The number of Dispel Herb[/INDENT]
[INDENT]◆Control Variables:#0007 Count += The number of Stimulant[/INDENT]
[INDENT]◆Text:None, Window, Bottom[/INDENT]
[INDENT]:    :I counted all your Potions, Magic Waters[/INDENT]
[INDENT]:    :Dispel Herbs and Stimulants.[/INDENT]
[INDENT]:    :[/INDENT]
[INDENT]:    :\|You have \v[7] of these in total!
is there any way shorter? its for the item unlocked almanac/glossary.. like percent unlocked?
the shortest way to accomplish that in a scriptcall or rather in a
conditional branch is to use ConditionalBranch+ which can check
the items + amount of it.

read the helpfile good, so you know how to check them correctly.
i need to count, not the conditional branch. i would have to make many conditional branch to count every time. is there no for loop script to just count each if have?
yes, this one works!
Great, happy RPG Making! 😂
Great, happy RPG Making! 😂
how do you check if the player has possession of each item ID 1-4 using a for loop? a counter variable increases everytime its true player has?

this code is from triacontance SceneGlossary.js
JavaScript:
 Game_Party.prototype.getHasGlossaryPercent = function(categoryName) {
        var hasCount = 0, allCount = 0;
        this.getAllGlossaryList(true, false, categoryName).forEach(function(item) {
            if (this.isNoCollect(item)) {
                return;
            }
            if (this.hasGlossary(item)) {
                hasCount++;
            }
            allCount++;
        }.bind(this));
        return allCount > 0 ? Math.floor(hasCount / allCount * 100) : 0;
    };
how do you check if the player has possession of each item ID 1-4 using a for loop?
If you want to use a for-loop, you could try something like this in a Script command:
JavaScript:
var count = 0;
for (var n = 1; n <= 4; n++) {
  count += $gameParty.numItems($dataItems[n]);
}
$gameVariables.setValue(123, count);
This should store the result in game variable 123.

Alternatively, use Control Variables > Script:
JavaScript:
[1,2,3,4].reduce(function(a, c) { return a + $gameParty.numItems($dataItems[c]); }, 0)
This should give the same result. reduce loops through an array and outputs a single value; it is ideal for sums like this. 👋

Here's some links in case you're interested in more details:
how do you check if the player has possession of each item ID 1-4 using a for loop?

If you want to use a for-loop, you could try something like this in a Script command:
Just to mention it...this will give how many total items the party has encompassing IDs 1-4, but it does not ensure they have at least one of each.

Granted, it could be an error in translation, but this will check if the player has possession of each item 1-4:
Code:
var owns=true;
for (var i = 1; i <= 4; i++)
{
    if ($gameParty.numItems($dataItems[i]<1)
        owns=false;
}
$gameVariables.setValue(123, (owns==true ? 1 : 0));

This will set variable 123 (change to whatever number you want to use) to 1 if the party owns at least one of each item ID 1 through 4. If they are missing one of those items, the variable will be 0. So you can then check that using a Conditional Branch in your event commands.
i combined both your scripts and came up with this! thank you very much
JavaScript:
var count = 0;
for (var n = 1; n <= 140; n++) {
  if ($gameParty.numItems($dataItems[n])) count++;
}
var percent = count / 140 * 100;
$gameVariables.setValue(47, percent);
Yes, sorry, looks like I misinterpreted the question. 😴 Good to see you found a solution, though!

Original thread: https://forums.rpgmakerweb.com/threads/counter-number-of-items-obtained-from-database.136482/