- Joined
- Jun 6, 2015
- Messages
- 28
- Reaction score
- 5
- First Language
- English
- Primarily Uses
I've teaching myself ruby scripting since I happen to have RPG maker VX Ace. Learning how Ruby works is one thing, but learning how to manipulate in while in RPG maker is completely different.
So many modulus, classes, inheriting classes, and methods that are all interconnected across it's entirety. It's a bit hard to wrap your head around. At the moment I'm trying to understand how a specific method works, more specifically:
def items
@items.keys.sort.collect {|id| $data_items[id] }
end
It's in the "Game_Party" Class. The call for this is $game_party.items
That call will make a list of items in my inventory. My long winded question is,
How does the above method do what it does?
From what I can gather, @items is initiated at the top of the class as an empty array. There are no other calls that I can find within the class, or it's inherent class, Game_Unit.
So first from the definitions I found (.keys) apparently makes a new array of only keys and not values. (I thought key's were only in hashes, but apparently arrays can hold hashes to?) The (.sort) makes "key, value" pairs and then sorts them like an array. And finally (.collect) works like the (.each) command? Calling the entire thing for each element within the array.
So @item.keys.sort.collect makes are array of keys, makes an array of keys and values and sorts them in some order (What kind of order?) and then repeats itself for everything inside itself? When this is called, I'm not quite understanding how exactly it's getting it's value. Since the @items is empty, does (.keys) generate keys based on how many items there are in $data_items ? Arrays start at 0, which help when calling values to a list in a window, but it's just confusing. Can anyone help me understand this better? I'm sorry if this question doesn't belong here. I wasn't sure where to put it.
So many modulus, classes, inheriting classes, and methods that are all interconnected across it's entirety. It's a bit hard to wrap your head around. At the moment I'm trying to understand how a specific method works, more specifically:
def items
@items.keys.sort.collect {|id| $data_items[id] }
end
It's in the "Game_Party" Class. The call for this is $game_party.items
That call will make a list of items in my inventory. My long winded question is,
How does the above method do what it does?
From what I can gather, @items is initiated at the top of the class as an empty array. There are no other calls that I can find within the class, or it's inherent class, Game_Unit.
So first from the definitions I found (.keys) apparently makes a new array of only keys and not values. (I thought key's were only in hashes, but apparently arrays can hold hashes to?) The (.sort) makes "key, value" pairs and then sorts them like an array. And finally (.collect) works like the (.each) command? Calling the entire thing for each element within the array.
So @item.keys.sort.collect makes are array of keys, makes an array of keys and values and sorts them in some order (What kind of order?) and then repeats itself for everything inside itself? When this is called, I'm not quite understanding how exactly it's getting it's value. Since the @items is empty, does (.keys) generate keys based on how many items there are in $data_items ? Arrays start at 0, which help when calling values to a list in a window, but it's just confusing. Can anyone help me understand this better? I'm sorry if this question doesn't belong here. I wasn't sure where to put it.
Last edited by a moderator: