Counter number of items obtained from database

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
180
Reaction score
38
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?
 

ShadowDragon

Realist
Veteran
Joined
Oct 8, 2018
Messages
4,168
Reaction score
1,595
First Language
Dutch
Primarily Uses
RMMV
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.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,630
Reaction score
2,004
First Language
EN
Primarily Uses
RMMZ
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.
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
180
Reaction score
38
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?
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
180
Reaction score
38
Great, happy RPG Making! :kaojoy:
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;
    };
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,630
Reaction score
2,004
First Language
EN
Primarily Uses
RMMZ
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. :kaohi:

Here's some links in case you're interested in more details:
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
1,307
Reaction score
703
First Language
English
Primarily Uses
RMMV
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.
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
180
Reaction score
38
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);
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,630
Reaction score
2,004
First Language
EN
Primarily Uses
RMMZ
Yes, sorry, looks like I misinterpreted the question. :kaoslp: Good to see you found a solution, though!
 

Latest Threads

Latest Posts

Latest Profile Posts

Did you know that 90% of people in the world use the number in their sentence as they see fit?
It's been a long time since I was here but I have a lot of updates... btw I am teaching again about game programming and game design in some schools here in Brazil hahahaha It's good to see kids and teens learning this kind of thing!
welp, I literally can't continue working on ALEX now that my MZ trial is up (unless I somehow place in the jam which I don't remotely deserve to for my unfinished crap) so I am back on my bullshit
some test stuff. Empty for now. My other project (this project) needs more attention. The lagging really kicks up when I try to capture gameplay. ✨

Forum statistics

Threads
113,887
Messages
1,078,212
Members
147,980
Latest member
gpc_826
Top