Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Using arrays...

I think this is what an array is, though I could be wrong.

Anyway. I'm trying to make a quest reward screen system, just something basic. But I've come to a snag. I want it to be like this:

def initialize(gold, exp, gp, [items])

Where [items] is something like... [(w, 1), (a, 54)]

So basically the items you get as a reward for that quest...

How would I do this?

Basically, w = weapon, a = armour, i = item, etc, since they are all classed as different things in the database.
 
Hash would probably work better for this.

Code:
module HashItemGain
  def self.gain_items(items = {})
    items.each do |item_type, ids|
      case item_type
      when 0
        if ids.is_a?(Array)
          ids.each {|id| $game_party.gain_item(id, 1)}
        elsif ids.is_a?(Hash)
          ids.each {|id, n| $game_party.gain_item(id, n)}
        end
      when 1
        if ids.is_a?(Array)
          ids.each {|id| $game_party.gain_weapon(id, 1)}
        elsif ids.is_a?(Hash)
          ids.each {|id, n| $game_party.gain_weapon(id, n)}
        end
      when 2
        if ids.is_a?(Array)
          ids.each {|id| $game_party.gain_armor(id, 1)}
        elsif ids.is_a?(Hash)
          ids.each {|id, n| $game_party.gain_armor(id, n)}
        end
      end
    end
  end
end

Now, this method will gain items for you automatically.

Call it with:
Code:
HashItemGain.gain_items(hash)

Now, your hash is started with this:

Code:
hash = {}
# Items
hash[0] = [item_id, item_id, ...]
# or Items
hash[0] = {item_id => n, item_id => n, ... }

# Weapons
hash[1] = [item_id, item_id, ...]
# or Weapons
hash[1] = {item_id => n, item_id => n, ... }

# Armors
hash[2] = [item_id, item_id, ...]
# or Armors
hash[2] = {item_id => n, item_id => n, ... }


I am so adding that to the MACL. ;)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top