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 Hashes?

Yes it's me again. I'm trying to use hashes to apply certain numbers to certain enemies. For example, let's say I have a variable called points. I would like this variable to increase by 3 for every ghost defeated, but 10 for every goblin, and 15 for every daemon. And if an enemy isn't defined with a certain value, how would I go about setting a default?

Would it be like this?

Code:
POINTS = {enemy.id => amount, enemy.id => amount}
 

poccil

Sponsor

The hash syntax is correct.  To ensure a default value in case a value doesn't exist in the hash, you would use code like:

Code:
point = POINTS[enemyID] || 0
where the value would be 0 if "enemyID" isn't defined in the hash.
 
Ok. So how would I go about implementing this? I have my code below:

Code:
#==============================================================================
# ** Scene_Battle (part 2)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------  
  alias mvment_start_phase5 start_phase5
  #--------------------------------------------------------------------------
  # * Start After Battle Phase
  #--------------------------------------------------------------------------  
  def start_phase5
    $movement_points += 1
    mvment_start_phase5
  end
end

EDIT: Crap, hit the post button instead of preview. Anyways, I want to change it so it checks to see if the enemy.id is defined, and if not, then the default is used.
 
You can do like this:
Code:
class Scene_Battle
  alias mvment_start_phase5 start_phase5
  def start_phase5
    $movement_points += 1
    for enemy in $game_troop.enemies
      if !enemy.hidden
        if POINTS.has_key?(enemy.id)
          your_variable += POINTS[enemy.id]
        end
      end
    end
    mvment_start_phase5
  end
end
I don't know which variable you want to increase, so replace "your_variable" with yours.

In case it is the "$movement_points" variable you showed there, just do:
Code:
class Scene_Battle
  alias mvment_start_phase5 start_phase5
  def start_phase5
    for enemy in $game_troop.enemies
      if !enemy.hidden
        if POINTS.has_key?(enemy.id)
          $movement_points += POINTS[enemy.id]
        end
      end
    end
    mvment_start_phase5
  end
end

The code just includes a loop for every enemy in the battle, it excludes the hidden ones, and if the enemy.id is defined in your POINTS hash, it is added to your variable.

PD: I haven't tested it, but it should work.
 

khmp

Sponsor

Alternatively you can specify a default value if you don't want to include a check.
Code:
POINTS.default = 0
In that case any key that isn't found in the Hash will retrieve the value 0.
 

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