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.

Meter script help

Kav

Sponsor

Hi all. I'm new at RGSS scripting and am trying to make a meter that constantly loops and will go "up" for every second the player does not move. This meter is created by drawing part or all of a meter image file loaded into a dummy variable (@@bar).

Code:
class RLS_Meter < Sprite
  def initialize
    super
    @@bar = Sprite.new
    @@bar.bitmap = RPG::Cache.picture("meter.png")
    self.bitmap = Bitmap.new(22, 125)
    self.x = 600
    self.y = 25
    self.z = 9999
    @@bar.visible = false
    self.visible = false
    @upper_bound = @@bar.bitmap.height
    update
  end
  
  def main
    update
  end
  
  def dispose
    self.bitmap.dispose
    @@bar.bitmap.dispose
    super
  end
  
  def update
    super
    self.visible = true
    @upper_bound = $game_variables[3]
    @pointer = @@bar.bitmap.height
    if $game_variables[3] != 0
      # Loops through bitmap vertically until cutoff
      while @pointer >= @upper_bound
      # Loops through bitmap horizontally setting pixels
      @py = @pointer
        for @px in 0...21
          @color = @@bar.bitmap.get_pixel(@px, @py)
          self.bitmap.set_pixel(@px, @py, @color)
        end
        @pointer -= 1
      end
    else
      self.bitmap.clear
    end
  end
end

The value in $game_variables[3] determines how high to draw the meter. It is called through Script: RLS_Meter.new. I want it so that it will increase ($game_variables[3] increases) while the player does nothing, and reset to 1 if the player moves. As you'll notice, I haven't yet set up what happens when it reaches the top, so you can leave that blank for now.

Thanks for any help in advance.
 

khmp

Sponsor

How many of these will there be? @@ variables denote that you will have multiple instances of this meter. Also because you are deriving from sprite you don't need to create a sprite within the class. Actually there's a lot of things you are going about oddly. You don't need to play with set_pixel/get_pixel. You can just change the src_rect of what is being drawn. This eliminates the need for a second sprite. Instead we can just manipulate src_rect. Let's say we want to show 80% of the image.

Code:
self.src_rect = Rect.new(left, top, width, height)
self.src_rect = Rect.new(0, self.bitmap.height - (self.bitmap.height * .8), self.bitmap.width, self.bitmap.height * .8)

Code:
class RLS_Meter < Sprite
  # Meter Constants
  METER_X = 600
  METER_Y = 25
  METER_WIDTH = 22
  METER_HEIGHT = 125
  METER_IMAGE = 'meter.png'
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super
    self.bitmap = RPG::Cache.picture( METER_IMAGE )
    self.x = METER_X
    self.y = METER_Y
    self.z = 9999
    self.visible = true
  end
  #--------------------------------------------------------------------------
  # * Object Cleanup
  #--------------------------------------------------------------------------
  def dispose
    # If the bitmap exists delete it.
    if self.bitmap
      self.bitmap.dispose
    end
    super
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    #super # Only need to call super if I plan on making the sprite flash.
    # Capped at 1.0
    percentage = [$game_variables[3] / 100.0, 1.0].min
    if percentage
      self.src_rect = Rect.new(0,                                           # x
                    self.bitmap.height - (self.bitmap.height * percentage), # y
                    METER_WIDTH,                                       # width
                    self.bitmap.height * percentage)                   # height
      self.y = self.bitmap.height - (self.bitmap.height * percentage) + METER_Y
    end
  end
end

#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs map screen processing.
#==============================================================================

class Scene_Map
  # Increment/Decrement
  ADJUST_VALUE = 2
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Create the RLS_Meter object to be displayed.
    meter = RLS_Meter.new
    # Make sprite set
    @spriteset = Spriteset_Map.new
    # Make message window
    @message_window = Window_Message.new
    # Transition run
    Graphics.transition
    # Main loop
    loop do
      if $game_player.moving?
        $game_variables[3] > 0 ? 
          $game_variables[3] -= ADJUST_VALUE : $game_variables[3] = 0
      else
        $game_variables[3] < 100 ? 
          $game_variables[3] += ADJUST_VALUE : $game_variables[3] = 100
      end
      #$game_variables[3] = 100
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Update the meter
      meter.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of sprite set
    @spriteset.dispose
    # Dispose of message window
    @message_window.dispose
    # Dispose of the meter
    meter.dispose
    # If switching to title screen
    if $scene.is_a?(Scene_Title)
      # Fade out screen
      Graphics.transition
      Graphics.freeze
    end
  end
end

Good luck with your game Kav! :thumb:
 

Kav

Sponsor

Thanks for the help, but it always displays the meter full, no matter what the value of $game_variables[3] is. Also, I only want this to be displayed at certain times, so would it be better to turn a switch on than do RLS_Meter.new (in Scene_Map it creates it automatically) and then use if statements to check if it's on in Scene_Map?
 

khmp

Sponsor

Well the second part is good as done. Do you want a $game_switch to determine whether or not to display the meter? The first part though. I've tested it on my game, mainly to see the src_rect thing worked correctly, but it does work on my machine. You wouldn't happen to be altering the variable anywhere else would you? Or uncommented that line that line of code right below? I'll look at it again and I think I can alias this code instead of overriding :) Alter the constant SWITCH_METER_ENABLE to whatever switch you want assigned to enabling/disabling the meter.

Code:
class RLS_Meter < Sprite
  # Meter Constants
  METER_X = 600
  METER_Y = 25
  METER_WIDTH = 22
  METER_HEIGHT = 125
  METER_IMAGE = 'meter.png'
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super
    self.bitmap = RPG::Cache.picture( METER_IMAGE )
    self.x = METER_X
    self.y = METER_Y
    self.z = 9999
    self.visible = false
  end
  #--------------------------------------------------------------------------
  # * Object Cleanup
  #--------------------------------------------------------------------------
  def dispose
    # If the bitmap exists delete it.
    if self.bitmap
      self.bitmap.dispose
    end
    super
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    #super # Only need to call super if I plan on making the sprite flash.
    # Capped at 1.0
    percentage = [$game_variables[3] / 100.0, 1.0].min
    self.src_rect = Rect.new(0,                                           # x
                  self.bitmap.height - (self.bitmap.height * percentage), # y
                  METER_WIDTH,                                       # width
                  self.bitmap.height * percentage)                   # height
    self.y = self.bitmap.height - (self.bitmap.height * percentage) + METER_Y
  end
end

#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs map screen processing.
#==============================================================================

class Scene_Map
  # Alias some methods.
  alias_method :kav_rlsmeter_main, :main
  alias_method :kav_rlsmeter_update, :update
  
  # Increment/Decrement
  ADJUST_VALUE = 2
  SWITCH_METER_ENABLE = 3
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Create the RLS_Meter object to be displayed.
    @rlsmeter = RLS_Meter.new
    kav_rlsmeter_main
    @rlsmeter.dispose
  end
  
  def update
    if $game_switches[SWITCH_METER_ENABLE]
      @rlsmeter.visible = true
      # Meter's update
      @rlsmeter.update
      if $game_player.moving?
        # If the player is moving decrement the value if possible.
        $game_variables[3] > 0 ? 
          $game_variables[3] -= ADJUST_VALUE : $game_variables[3] = 0
      else
        # The player is not moving increment the value if possible.
        $game_variables[3] < 100 ? 
          $game_variables[3] += ADJUST_VALUE : $game_variables[3] = 100
      end
    else
      @rlsmeter.visible = false
    end
    kav_rlsmeter_update
  end
end
 

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