HollowMonty

Villager
Member
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.
 
Last edited by a moderator:

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
662
Reaction score
377
First Language
German
Primarily Uses
Hi,


I think such questions would belong to Learning Ruby and RGSSX, even though it is maker specific you should have a better chance to get your RGSS related questions solved there.


Apart from that, first: @items  is actually initialized with an empty Hash, not an Array (there's a pair of braces, not square brackets). :)


Items are stored by using the items database ID as key and the amount the party possesses of it as value. Items the party no longer posesses are removed from the hash (see: gain_item method). For example, if your party has 2 Potions, 11 Antidotes and 5 Elixirs, the process could look like this:

@items...


=> { 1 => 2, 8 => 5, 6 => 11 } , Hash from (ID => Amount) entries of the parties items


@items.keys... 


=> [1, 8, 6] , Array of item IDs the party has


@items.keys.sort... 


=> [1, 6, 8] , Array of item IDs, sorted in ascending order


@items.keys.sort.collect { ... } 


=> [ Item001 , Item006 , Item008 ] , Array of database item objects.


$data_items is an array that contains literally what you see in the item tab of the database. It is initialized when you start the game before you enter the title screen and contains RPG::Item objects that hold the settings of each particular database entry. $data_items[3].price would return the price of the third item in the database for example.
 
Last edited by a moderator:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
38,438
Reaction score
10,869
First Language
German
Primarily Uses
RMMV
I've moved this thread to Learning Ruby & RGSSx. Please be sure to post your threads in the correct forum next time. Thank you.
 

HollowMonty

Villager
Member
Joined
Jun 6, 2015
Messages
28
Reaction score
5
First Language
English
Primarily Uses
Thanks. I'll direct anymore RGSS  related questions towards that area of the forum. This explanation pretty much answers everything. Thanks for your quick reply. My main confusion was that I wasn't sure what the keys and values were. Lists and menus work a lot with index's, which start at 0 and go on +=1 for each item in the list so that the window knows what item your on. I was thinking that the index range from like say, 4 items index = [0...3] then the values would be the item id's.But this doesn't seem to be the case, so I'll have to dissect that a bit more. Thanks again.
 

HollowMonty

Villager
Member
Joined
Jun 6, 2015
Messages
28
Reaction score
5
First Language
English
Primarily Uses
One more minor question. From what I can tell, the hash is empty before this little line of code. At what point is it populated by the keys and values? I couldn't really find a place where I saw @items getting any value besides nil (empty). Does that line call $date_items to the hash before anything else is done?


# Edit: Thanks for moving the thread. Sorry for the improper placement.
 
Last edited by a moderator:

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
662
Reaction score
377
First Language
German
Primarily Uses
The hash is populated within the gain_item method, in line 251 to be exact.


The order the methods are defined in does not really matter as they only define the possible behavior of your Game_Party objects. The entire script is executed before you get to the title screen. Once the class is defined, you could go on and create your own little party:

party = Game_Party.new


# => Create a new Party object and call its "initialize" method.


# Each party object has its own set of items, members, gold etc.


# The only instance of that class is usually held by $game_party


print party.items


# => At this point, your new party has indeed no items => Empty Array


party.gain_item($data_items[1], 2)


party.gain_item($data_items[8], 11)


party.gain_item($data_items[6], 5)


# => Let's use the gain_item method to change that!


print party.items


# => Now the result should contain 3 different items



Another thing to note is that each object you create can be shared by multiple variables. For example, if you write


a = [2, 3, 4]


b = a


then the variables a and b refer to the same array. You can think of it as an alias name for the same object. It does not matter whether you write


a.delete(3)


or


b.delete(3)


to remove the number 3 from that array.


In this case, container is set depending on whether you want to add an item, weapon or armor to the inventory in line 247.


(Might edit this when I wake up)
 
Last edited by a moderator:

HollowMonty

Villager
Member
Joined
Jun 6, 2015
Messages
28
Reaction score
5
First Language
English
Primarily Uses
Hu.. That's quite interesting. So, if I'm understanding it correctly, if you start a new game and have no items, when you gain an item it runs that method and add's it to the @items hash. If you continue from a save file, then it will run that method again for every item you have (Which I would think would be in cache.) therefore repopulating the @items hash. Or perhaps the hash itself is stored. Though I guess it doesn't really matter. I understand a lot better, thank you very much for this.


I'm sorry to keep bothering you with these really minor questions but, I've tried looking it up and can't find a reference. what does the (.id) in the gain_item method do exactly? If I had to guess, I could say (.id) is another way of calling out the key from a hash.
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
662
Reaction score
377
First Language
German
Primarily Uses
Continuing from a savefile is kind of an exception here. When loading a savefile your game objects are directly recreated from the savefile data. So your new party should have the same contents as the party you saved, without calling any extra methods.


In practically all other cases however, including changing items via event commands, battle rewards, shop processing or changing equipment, gain_item is used.


As for line 251:


container[item.id] = [[new_number, 0].max, max_item_number(item)].min


.id  is called on whatever "item" resolves to. Here, "item" holds the item that should be added to the inventory -> a RPG::Item, RPG::Weapon or RPG::Armor object. You can look up the properties of those database types in the help file (Help -> Contents  in the editor).


In this case, id refers to the items database ID. For example, if you call


$game_party.add_item($data_items[1], 15)


in a script command to add 15 potions to the inventory, item.id would resolve into 1.


As general advice for orientation, you can take a look at the pinned thread in this subforum if you haven't already done that. Also, looking how the various event commands are implemented might help. To find the implementation of an event command you can search for its name within the Game_Interpreter script.


The actual game is controlled by the main script. If you are interested in how the parts are working together, you can also try to follow the method chain invoked in that script.


I'm sure there are also good tutorials out there, but I'm not familiar with them unfortunately.
 

HollowMonty

Villager
Member
Joined
Jun 6, 2015
Messages
28
Reaction score
5
First Language
English
Primarily Uses
Thank you very much. You seem to be quite experienced and have answered my question clearly and concisely, I appreciate it. Thank you for your advice. I'll certainly put it to good use.


I suppose the moderator can close this thread now.
 

Latest Threads

Latest Profile Posts

People seem to be on the down of late. Must be the changing of the seasons or something.

WITH THAT SAID, I WOULD LIKE TO PROMOTIONICE THAT MY GAME, CROWNING LEGACY TRADEMARK, WILL ALSO INCLUDE CHANGING SEASONS, NOW WITH 100% LESS DEPRESSION!
:kaoback::biggrin::eek::rock-left:D:<
seasoning.png
Rave Heart Overhaul Edition is now available! :elwink:

It was fun while it lasted, but guess it's time to say goodbye to my antidepresants. My body is immune to their good effects and only bad effects would remain.

Forum statistics

Threads
131,588
Messages
1,221,293
Members
173,287
Latest member
kito
Top