Main Menu
  • Welcome to The RPG Maker Resource Kit.

Variable to Count the Number of Items in Inventory

Started by Heretic86, June 18, 2011, 10:19:03 AM

0 Members and 1 Guest are viewing this topic.

Heretic86

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!

modern algebra

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.

exhydra

#2
Easy-peasy!


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.

UPDATED 05-29-14


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

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

ForeverZero

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

exhydra


UPDATED 05-29-14


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

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

ForeverZero

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.

exhydra

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

Heretic86

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

$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!

exhydra

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

pacdiggity

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

exhydra

#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.

UPDATED 05-29-14


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

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

pacdiggity

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

ForeverZero

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?

exhydra

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

modern algebra

#14
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!

exhydra

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