RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Variable to Count the Number of Items in Inventory

0 Members and 1 Guest are viewing this topic.

***
Rep:
Level 70
RMRK Junior
Is there a way to Count the number of Items in the Players Inventory, and not just to check for their presence?
Heretic's Vehicles XP (Boat and Magic Carpet)

Heretic's Collection XP Ver 2.3 - Updated to include Dynamic Lighting, Moving Platforms, Vehicles, and much much more!

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Veteran2011 Favourite Staff Member2011 Most Mature Member2011 Best RPG Maker User (Scripting)2011 Best Use of Avatar and Signature Space2010 Most Mature Member2010 Favourite Staff Member
One of the operands in Control Variable is Item, but I don't think it will let you check the number of weapons or armors, just items.

*
Rep:
Level 82
GIAW 14: 1st Place (Easy Mode)2013 Project of the Year2013 Best RPG Maker User (Programming)2013 Most Promising ProjectParticipant - GIAW 11Bronze - GIAW 10
Easy-peasy!

Code: [Select]
class Ex_Inventory
 
  def self.count_items(szName = nil)
    nItems = 0
   
    if szName == nil
      for item in $game_party.items
        nItems += $game_party.item_number(item)
      end
    else
      while nItems < $game_party.items.size
        if szName.downcase == $game_party.items[nItems].name.downcase
          nItems = $game_party.item_number($game_party.items[nItems])
          break
        end
       
        nItems += 1
      end
    end
   
    return nItems
  end
 
end

Changed the code slightly ... now if you run Ex_Inventory.count_items normally, it returns the number of all items in the inventory. However, if you run Ex_Inventory.count_items(name_of_item), it will return the amount for just that item.
« Last Edit: June 19, 2011, 12:12:52 AM by Exhydra »

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

**
Rep:
Level 82
Code: [Select]
class Game_Party
 
  def weapon_count
    total = 0
    @weapons.each_value {|quantity| total += quantity }
    return total
  end
 
  def armor_count
    total = 0
    @armors.each_value {|quantity| total += quantity }
    return total
  end
 
  def item_count
    total = 0
    @items.each_value {|quantity| total += quantity }
    return total
  end
 
  def inventory_count
    return weapon_count + armor_count + item_count
  end
end

*
Rep:
Level 82
GIAW 14: 1st Place (Easy Mode)2013 Project of the Year2013 Best RPG Maker User (Programming)2013 Most Promising ProjectParticipant - GIAW 11Bronze - GIAW 10
Or that works too  :o

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

**
Rep:
Level 82
There's really no need to make a method for a specific item's count. They are there by default already

$game_party.item_number(ID) will return the quantity of that item, same with weapons and armors.

*
Rep:
Level 82
GIAW 14: 1st Place (Easy Mode)2013 Project of the Year2013 Best RPG Maker User (Programming)2013 Most Promising ProjectParticipant - GIAW 11Bronze - GIAW 10
Hmm, yeah, but they would need to know the RPG::Item, RPG::Weapon or RPG::Armor ID, right? Certainly, it could be found out easily, I just thought it would be simpler to just allow them to plop in a name and have it pop out automagically.

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

***
Rep:
Level 70
RMRK Junior
I should have mentioned that I was looking for a specific type of item.  Potions and Antidotes.  Im gonna go with this code:

Code: [Select]
$game_variables[4] =
$game_party.item_number(1)
$game_variables[5] =
$game_party.item_number(11)

Where 4 will be my Potions and 5 will be my antidotes.  Also excluding the stupid newline bug for event scripts, which already caught me.

Thanks much, guys!
Heretic's Vehicles XP (Boat and Magic Carpet)

Heretic's Collection XP Ver 2.3 - Updated to include Dynamic Lighting, Moving Platforms, Vehicles, and much much more!

*
Rep:
Level 82
GIAW 14: 1st Place (Easy Mode)2013 Project of the Year2013 Best RPG Maker User (Programming)2013 Most Promising ProjectParticipant - GIAW 11Bronze - GIAW 10
Hrm, Potions and Antidotes aren't always '1' and '11'. It also depends on what order you receive the items in the game. Also, I don't think you should be getting back the proper data with that code that you're using. I could be wrong, but I've been poking around at those snippets of code and they aren't returning to proper numbers ...

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

*****
my name is Timothy what's yours
Rep:
Level 79
Hello
2014 Most Missed Member2014 Zero to Hero2014 Best IRC Quote2012 Zero To HeroSecret Santa 2012 ParticipantContestant - GIAW 9For frequently finding and reporting spam and spam bots2011 Zero to Hero
That call returns the quantity of the item with that ID, not the position in the inventory. Thus, if potions are 1 and antidotes are 11 in the database, that call will always return the quantity of potions or antidotes.
it's like a metaphor or something i don't know

*
Rep:
Level 82
GIAW 14: 1st Place (Easy Mode)2013 Project of the Year2013 Best RPG Maker User (Programming)2013 Most Promising ProjectParticipant - GIAW 11Bronze - GIAW 10
I must be doing something horrifically wrong then, which is not too much of a surprise. But when I give myself 99 Potions (which is its default position 1) and do : 'print $game_party.item_number(1)' I keep coming up with '0'. I gave myself random numbers of the position 1 weapon and armor, but it still comes up as '0'.

The Game_Party > item_number code really looks like it's asking for an RPG::Item, RPG::Weapon or RGP::Armor ID and not just a normal integer.
« Last Edit: June 19, 2011, 01:25:07 AM by Exhydra »

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

*****
my name is Timothy what's yours
Rep:
Level 79
Hello
2014 Most Missed Member2014 Zero to Hero2014 Best IRC Quote2012 Zero To HeroSecret Santa 2012 ParticipantContestant - GIAW 9For frequently finding and reporting spam and spam bots2011 Zero to Hero
That's because you don't have any of the item in your inventory.
It's a call to determine the number of that item in the inventory, not just the item.
it's like a metaphor or something i don't know

**
Rep:
Level 82
It's a number. It wants the ID, hence the requested argument being "item_id".

The @items, @weapons, and @armors variables in Game_Party are hashes. The key is the ID of the respective item, and the value stored at that key is the quantity.

Are you sure you are adding the potions into your inventory?

*
Rep:
Level 82
GIAW 14: 1st Place (Easy Mode)2013 Project of the Year2013 Best RPG Maker User (Programming)2013 Most Promising ProjectParticipant - GIAW 11Bronze - GIAW 10
Alright, this uploaded project shows how I'm attempting it. The person on the left adds 50 Potions to my Inventory and the one on the right prints $game_party.item_number(1). And it always returns zero. I feel like I'm going a little crazy here ... it's probably something stupid simple that I'm missing.

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Veteran2011 Favourite Staff Member2011 Most Mature Member2011 Best RPG Maker User (Scripting)2011 Best Use of Avatar and Signature Space2010 Most Mature Member2010 Favourite Staff Member
In VX, it is asking for an RPG::Item, RPG::Weapon, or RPG::Armor object.

In XP, it only asks for an ID since there are separate methods when checking armors or weapons.

So you're both right!
« Last Edit: June 19, 2011, 02:28:59 AM by modern algebra »

*
Rep:
Level 82
GIAW 14: 1st Place (Easy Mode)2013 Project of the Year2013 Best RPG Maker User (Programming)2013 Most Promising ProjectParticipant - GIAW 11Bronze - GIAW 10
Oh good, I'm not insane! I was thinking that we were talking in VX terms. I've never used XP.

UPDATED 05-29-14


IS YOUR PROJECT OPTIMIZED?
UPDATED 07/04/15 - v2.5

RPG MAKER TOOLBOX
UPDATED 07/04/15 - v1.5