Help needed with tracking the total amount of items

VesperCire

Veteran
Veteran
Joined
Feb 9, 2016
Messages
36
Reaction score
1
First Language
English
Primarily Uses
RMMV
Basically what the title says. I'm wondering if there's a way to track the total number of items, not just the amount of a single item. If the total is over a certain number or higher, chests won't open. If possible, I'd prefer it to be tracked with a variable or in a conditional branch, but any way possible will be fine.

Example: The player has 3 potions, 10 max potions and 1 elixir. The game will register the total as 14, since that is the amount of all the items added up.

Thanks in advance!

(This is for MV if this somehow gets placed in the wrong category)
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
28,996
Reaction score
6,676
First Language
German
Primarily Uses
RMMV
nothing automatic.

the simplest version would probably be a common event that uses two vaariables to add the total number of items together, with one control variable command for each item in the list.

A scripted loop would also be a possibility, but that would really add everything including hidden items that are often used for triggers without the player seeing them in the inventory.
 

Oddball

Veteran
Veteran
Joined
Sep 4, 2014
Messages
1,683
Reaction score
451
First Language
English
Primarily Uses
N/A
You would also have to event a shop (if your game uses shops) not use the shop processing. May i suggest a mechanic to go with what you want to do? Bartering with items. You can trade multipules of an item to get a slightly better item for free. You wouldn't be able to do something like 100 potions for the legendary-sword-of-bad-assery. But you could trade three potions for two refined potions. Or four spears-of-fae-might for one legendary-sword-of-bad-assery

It would be annoying for the player if they had to throw away items or sell a bunch of items and not buy anything every time they were about to go into a dungeon
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,299
Reaction score
11,897
First Language
English
Primarily Uses
RMMV
I'm on my work pc so don't have MV at the moment, but isn't the $gamePlayer._items just an array, where the index is the item number and the value is the count? Is there an array method that adds up all the elements in the array?

Also, when you say 'items' do you mean 'items' and 'weapons' and 'armors', and does that include what's equipped?
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
1,892
Reaction score
893
First Language
Spanish
Primarily Uses
RMVXA
nothing automatic.
unless you count the JS equivalent of Ruby's object.collect{} and object.select{}, with proper item IDs and filtering.
with "object" being the items database.

I don't know how to write them, but I know they exist.
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,627
Reaction score
1,060
First Language
English
Primarily Uses
RMMV
$gameParty._items is an object (probably called a map in other programming languages) with the key being the ID# of the item and the value being the count of the item.
So if you have 2 potions and 3 magic waters it would be:
Code:
console.log($gameParty._items)
{
   1: 2,
   2: 3
}
Therefore if you are only interested in the number of total items, you just want the values of the object (you don't care about the ID# i.e. the keys)

Code:
Object.values($gameParty._items)
From there you have an array with the counts of all items in the inventory. There's no built-in way to sum an array in JS so you'd probably do this:

Code:
Object.values($gameParty._items).reduce((a,b) => a + b, 0)
You can do a Control Variables event command and set it to the script option with that code, and it'll pop out the total number of items the party has. Then have your event do something (or not do something) with a Conditional Branch based on the value of that variable.
 

VesperCire

Veteran
Veteran
Joined
Feb 9, 2016
Messages
36
Reaction score
1
First Language
English
Primarily Uses
RMMV
nothing automatic.

the simplest version would probably be a common event that uses two vaariables to add the total number of items together, with one control variable command for each item in the list.

A scripted loop would also be a possibility, but that would really add everything including hidden items that are often used for triggers without the player seeing them in the inventory.
I think this will work. If it doesn't I'll reply back sometime later.

Yep, it works like a charm. Thanks a ton!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Stream will be going live early today as I will be working on some music before I transition to art! When I switch to art, I will be focusing on making a new stream avatar~ Feel free to drop by!
For anyone who has uploaded a game to Steam and wonders if they actually check your game's build when you first upload it, I can personally vouch for Steam.
6 more towns to make in my game. SIX. not done with interiors yet but SIX EXTERIORS.
what to do when you come across a person that has stated "What if I say, f*** their EULA? I could probably get away with it." concerning asset packs sold here... on this site ...
So a guy enters in a bar and walks up to the counter. He looks the bartender with a mysterious look and asks him...

Forum statistics

Threads
93,496
Messages
912,998
Members
123,031
Latest member
zojin
Top