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?
Counter number of items obtained from database
RPG Maker MV Supportthe 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.
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:
To count all items (Items and unequipped Armors/Weapons) you could try Control Variables > Script:
To count a different category, replace
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!
JavaScript:
$gameParty.allItems().reduce(function(a, c) { return a + $gameParty.numItems(c); }, 0)
allItems() with:items()for Items only (no Armors or Weapons)weapons()for Weapons onlyarmors()for Armors onlyequips()for Armors and Weapons only
yes, this one works!
is there any way shorter? its for the item unlocked almanac/glossary.. like percent unlocked?
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?
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;
};
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);
Alternatively, use Control Variables > Script:
JavaScript:
[1,2,3,4].reduce(function(a, c) { return a + $gameParty.numItems($dataItems[c]); }, 0)
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:
for - JavaScript | MDN
The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.
Array.prototype.forEach() - JavaScript | MDN
The forEach() method of Array instances executes a provided function once for each array element.
Array.prototype.reduce() - JavaScript | MDN
The reduce() method of Array instances executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
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/