Back again for loops

I always get my self mixed up in for loops - Especially in ruby. So lets start with this:

Spoiler
module DROPS DROP_HASH = { "Drop One" => { :item_type => 0, :item_id => 1, :amount => 6, :currency_type => "GC", :currency_amount => 25 }, "Drop Two" => [ { :item_type => 2, :item_id => 1, :amount => 6, :currency_type => "GC", :currency_amount => 0 }, { :item_type => 1, :item_id => 1, :amount => 6, :currency_type => "GC", :currency_amount => 20 }, { :item_type => 2, :item_id => 20, :amount => 6, :currency_type => "RT", :currency_amount => 20 }, ] }endclass << DataManager alias kalacious_troop_drop_object create_game_objects def create_game_objects kalacious_troop_drop_object $kc_drop = Troop_Drop.new endendclass Troop_Drop def initialize @troop_drop_object = [] end def drop_object @troop_drop_object end def create_new_drop(item_type, item_id, amount, currency_type, currency_gain) if @troop_drop_object.any? @troop_drop_object.each do |drop| if drop.item_type == item_type and drop.item_id == item_id return false end end end drop_object = Troop_Drop_Object.new(item_type, item_id, amount, currency_type, currency_gain) @troop_drop_object.push(drop_object) end def create_new_static_drop(name) drops = DROPS:😀ROP_HASH[name] || [] drops = [drops] if !drops.is_a? Array drops.each do |drop| create_new_drop(drop[:item_type], drop[:item_id], drop[:amount], drop[:currency_type], drop[:currency_gain]) end end def get_drop_name create_master_item_list(@troop_drop_object) end def test_menthod $game_message.texts.push("example"); end private def create_master_item_list(inventory_object) drop_inventory = [] inventory_object.each do |inv| case inv.item_type when 0; item = $data_items[inv.item_id] when 1; item = $data_weapons[inv.item_id] when 2; item = $data_armors[inv.item_id] end if drop_inventory drop_inventory.push(item) end end create_inventory_hash(drop_inventory, drop_object) end def create_inventory_hash(drop_inventory, troop_object) inv_hash = {} drop_inventory.each do |inv| troop_object.each do |tr_obj| inv_hash[tr_obj.item_id] = inv.name end end puts inv_hash.inspect endendclass Troop_Drop_Object attr_reader :item_type attr_reader :item_id attr_reader :amount attr_reader :currency_type attr_reader :curency_gain def initialize(item_type, item_id, amount, currency_type = '', currency_gain = 0) @item_type = item_type @item_id = item_id @amount = amount @currency_type = currency_type @currency_gain = currency_gain endend
There are three things to focus on, the hash - in the module, then the private methods: create_master_item_list and create_inventory_hash.

create_master_item_list will create an array of RPG objects based on their type. That's fine, we then need to take that array of items, the hash it came from and create something like:
 
Code:
{ 2 => what ever armor, 1 => what ever armor, 2 => what ever armor}
You might be wondering, where is this information coming from?

When you call create_new_static_drop("Drop Two") you get are processing the Drop Two key, with its nested hashes, try it and inspect it.

So based on that, in the method: create_inventory_hash what do I have to do so that its tr_obj.item_id => inv.name - that is the id should match the name.

An example:

In Drop Two hash is item_type 2, armor with an ID 1 - in my test database that's leather armor. So the hash should be: 1 => leather armor.

the method that sets up or is responsible for setting up this hash is:
 

def create_inventory_hash(drop_inventory, troop_object) inv_hash = {} drop_inventory.each do |inv| troop_object.each do |tr_obj| inv_hash[tr_obj.item_id] = inv.name end end puts inv_hash.inspect endI hope my post was clear enough - I tend to fail at explaining things through text.

If you run the script and view the results in the terminal you'll see things are messed up.
w8, do you have a problem with the script or what?
I did not try it but maybe you can tell us what you expect and what is actually happening?
Please close figured it out
What was the solution?
Hi Kalacious,

I would suggest you read my post about how to structure the customization section: http://forums.rpgmakerweb.com/index.php?/topic/7887-handholding-in-scripting/?p=86738

The Struct class is also quite useful. You can quickly create structs that you can fill out that way:

Troop_Drop_Object = Struct.new🙂item_type,                               :item_id,                               :amount,                               :currency_type,                               :currency_amount)drop = Troop_Drop_Object.newdrop.item_type = 0drop.item_id = 1drop.amount = 6drop.currency_type = "GC"drop.currency_amount = 25
You can require specific initialization parameters with your new class. You can basically treat it as any other class and instead do this:

Troop_Drop_Object = Struct.new🙂item_type,                               :item_id,                               :amount,                               :currency_type,                               :currency_amount)class Troop_Drop_Object  def initialize(item_type, item_id, amount, currency_type = '', currency_amount = 0)    super  endenddrop = Troop_Drop_Object.new(0, 1, 6)drop.currency_type = "GC"drop.currency_amount = 25

*hugs*

 - Zeriab
Thanks, It was a good read but I figiured it out. I was going about it the wrong way. Didnt have anything to do with customization 🙂
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/back-again-for-loops.16824/