Counter number of items obtained from database

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
178
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,011
Reaction score
1,523
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,473
Reaction score
1,865
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
178
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
178
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,473
Reaction score
1,865
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,159
Reaction score
634
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
178
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,473
Reaction score
1,865
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

Not about my game project but i feels like to talk about it a bit. :kaohi:
moka.jpg

It's my コテキャ and maybe closest thing in english is a "avatar personality" i guess?
Her design also looks very similar to protagonist from my game because i'm so uninspiring :kaoslp:
Oh and my hobby is sleeping and drawing. :kaojoy:
Okay, so it seems I've found a major bug in VNM. Check my thread about variable buttons in the support forum for more info.
Man, this quick One Map game is causing unprecedented amounts of trouble.
I always end up having issues with things i'd never expect.

Capture.PNG

Those friggin mountains man xD

Forum statistics

Threads
112,402
Messages
1,068,068
Members
146,053
Latest member
komradiv
Top