Help needed with tracking the total amount of items

VesperCire

Veteran
Veteran
Joined
Feb 9, 2016
Messages
50
Reaction score
2
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
30,711
Reaction score
7,365
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,885
Reaction score
521
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
39,935
Reaction score
13,574
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
2,217
Reaction score
1,190
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,628
Reaction score
1,105
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
50
Reaction score
2
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

sooo I'm making the music for my game and yeah... prepare for a very non typical rpg ost... I don't even know how to make typical sounding stuff. I can do D&B, hiphop, glitch, and whatnot all day tho lol.
It's interesting that the logic involved in skill range obstructions is also used for Tetris...
Saw a resource and was like "Oh, there was a request for that, I can help that person now!"... turns out, request was nearly two years ago. Welp.
My garbage collection service has the BEST hold music I've ever heard. Sometimes, I legitimately hope to be on hold for more than a few minutes just to hear the song to completion.
Streaming now, maybe it will work this time!

Forum statistics

Threads
103,069
Messages
997,037
Members
134,533
Latest member
Mothraezilla43
Top