Example where question":3gwdmloy said:
Where is all the number of items, weapons, armors held in the party, and how is it used?
First off, we will start in Game_Party. Under the def initialize method, we have this:
# Create amount in possession hash for items, weapons, and armor
@items = {}
@weapons = {}
@armors = {}
Now, with only this much information, we can't really say what they are planning on doing with this, so let's look a little further shall we...
scrolls through Game_Party code...
WAIT A MINUTE!!!
#--------------------------------------------------------------------------
# * Get Number of Items Possessed
# item_id : item ID
#--------------------------------------------------------------------------
def item_number(item_id)
# If quantity data is in the hash, use it. If not, return 0
return @items.include?(item_id) ? @items[item_id] : 0
end
Ok. So this is the method that reads the number of items held. Lets see, we pass the item_id to this method, and...
@items.include?(item_id) ? @items[item_id] : 0
The .include? method checks if an item_id has been added to our Hash (@items, as initialized in the initialize method). If it has the item_id, it returns the value. That means, that our key in the @items hash, is the item_id, and the value is the number owned. If no keys is found, it returns 0 (See explanation about Ternary Operators above if you don't understand the syntax behind this line).
As you can see
#--------------------------------------------------------------------------
# * Gain Items (or lose)
# item_id : item ID
# n : quantity
#--------------------------------------------------------------------------
def gain_item(item_id, n)
# Update quantity data in the hash.
if item_id > 0
@items[item_id] = [[item_number(item_id) + n, 0].max, 99].min
end
end
Here, we send the item_id, and number of the object to gain. We are setting the item_id in our @items Hash, and we are setting it with that funky looking expression on the right. The expression on the right side, is just two arrays, the major array returns the min value with the array. The minor array is within that array, and it returns the max number within itself. Which means, our item number will be
the biggest number in our minor array, but the smallest number in our major array.
The two numbers in our minor array are the number of items already owned (calling our item_number method from above), plus how every many items you are gaining (the second number passed in this method call). Our second number is just 0, which means we will never have less than this, because our minor array picks the biggest number.
The two numbers in our major array are the number that is returned in the minor array, and 99. Now, the major array returns the min number here, so if we have more than 99 of an object, it will cap it back to 99.
Summary:Items, Weapons and Armors all work the same way. Keys are the objects reference id (id to the $data_object array, such as item_id to $data_items) and values are the number owned.
(If you were wondering why I changed my narrative/tone throughout this explanation, it is to show you what should be going through your head when you don't know something. I took the position as the unknowing student, and the teacher)