The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: Heretic86 on June 18, 2011, 10:19:03 AM

Title: Variable to Count the Number of Items in Inventory
Post by: Heretic86 on June 18, 2011, 10:19:03 AM
Is there a way to Count the number of Items in the Players Inventory, and not just to check for their presence?
Title: Re: Variable to Count the Number of Items in Inventory
Post by: modern algebra on June 18, 2011, 06:35:22 PM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: exhydra on June 18, 2011, 08:42:59 PM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: ForeverZero on June 18, 2011, 09:01:03 PM
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
Title: Re: Variable to Count the Number of Items in Inventory
Post by: exhydra on June 18, 2011, 09:05:19 PM
Or that works too  :o
Title: Re: Variable to Count the Number of Items in Inventory
Post by: ForeverZero on June 18, 2011, 09:17:59 PM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: exhydra on June 18, 2011, 09:35:33 PM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: Heretic86 on June 18, 2011, 09:57:13 PM
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!
Title: Re: Variable to Count the Number of Items in Inventory
Post by: exhydra on June 18, 2011, 10:16:06 PM
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 ...
Title: Re: Variable to Count the Number of Items in Inventory
Post by: pacdiggity on June 19, 2011, 12:54:50 AM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: exhydra on June 19, 2011, 01:14:44 AM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: pacdiggity on June 19, 2011, 01:55:13 AM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: ForeverZero on June 19, 2011, 01:58:12 AM
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?
Title: Re: Variable to Count the Number of Items in Inventory
Post by: exhydra on June 19, 2011, 02:20:48 AM
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.
Title: Re: Variable to Count the Number of Items in Inventory
Post by: modern algebra on June 19, 2011, 02:24:41 AM
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!
Title: Re: Variable to Count the Number of Items in Inventory
Post by: exhydra on June 19, 2011, 02:29:35 AM
Oh good, I'm not insane! I was thinking that we were talking in VX terms. I've never used XP.