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.

Simple HUD ~~ One more question!

Hey people!  I have a small problem.  I'm trying to make a HUD that displays the amount of gold the player has.  For some reason (I'm new with scripting so it may be obvious), the window appears on screen, but the amount of gold will not.  If anyone could help me out (and maybe explain what I did wrong), I would be very grateful.

Here's the code:

Code:
class Window_GoldHUD < Window_Base
  def initialize
    super(500,0,100,50)
    self.contents = Bitmap.new(width-32, height-32)
    self.opacity = 150
  refresh
end
def refresh
  self.contents.clear
  self.contents.font.color = normal_color
    self.contents.draw_text(500, 0, 120, 32, $game_party.gold.to_s, 2)
end

def update
  super
  refresh
end
end
class Scene_Map
  alias goldhud_main main
  alias goldhud_update update
  def main
    @goldhud = Window_GoldHUD.new
    goldhud_main
    @goldhud.dispose
  end
  def update
    @goldhud.update
    goldhud_update
  end
end

Here's my new question:

I know how to make the HUD appear and disappear based on a switch, but how do I stop it from appearing for just a second whenever I return to the map screen (from the menu, starting the game, etc.)?  Thanks in advance!

Here's the code:
Code:
class Window_GoldHUD < Window_Base
  def initialize
    super(400,0,200,60)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 150
    
  refresh
end
def refresh
  self.contents.clear
    self.contents.draw_text(-60, 0, 150, 32, $game_party.gold.to_s, 2)
    self.contents.draw_text(100, 0, 150, 32, "Gold")
  end

def update
  super()
 @gold != $game_party.gold
 @gold = $game_party.gold
 refresh
end
end

class Scene_Map
  alias goldhud_main main
  alias goldhud_update update
  def main
    @goldhud = Window_GoldHUD.new
    goldhud_main
    @goldhud.dispose
  end
  def update
    @goldhud.visible = $game_switches[1]
    @goldhud.update    
    goldhud_update
  end
end

Thanks!
 

khmp

Sponsor

Code:
self.contents.draw_text(500, 0, 120, 32, $game_party.gold.to_s, 2)

The first parameter is the draw location on the x axis where you want to start. You have it so it starts drawing at the edge of the window. Just make that number 0 instead.

Code:
self.contents.draw_text(0, 0, 150, 32, $game_party.gold.to_s, 2)

You also might want to modify your update method that tells the window to redraw if the amount of gold changes. Like this:

Code:
def update
  super()
  unless @gold == $game_party.gold  # Same as saying if @gold != $game_party.gold
    @gold = $game_party.gold  # Save the variable for comparison on upcoming updates.
    refresh
  end
end

You only need to call "super()" from update if you are expecting the windowskin to change in game. But it doesn't hurt to have it there. Just a "for your information" thing. Last thing I see that might be a problem is the window height.

Code:
super(500,0,100,50)

You want to usually leave 32 pixels in height for text to draw. You have the height set to 50 and when the bitmap gets created you're left with 28 pixels. It probably won't be a problem because text usually fits in 24. Just another "now you know" :wink:

Good luck with your HUD AbyssalLord! :thumb:
 
Thanks for the help!

EDIT:Here's my new question:

I know how to make the HUD appear and disappear based on a switch, but how do I stop it from appearing for just a second whenever I return to the map screen (from the menu, starting the game, etc.)?  Thanks in advance!

Here's the code:
Code:
class Window_GoldHUD < Window_Base
  def initialize
    super(400,0,200,60)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 150
    
  refresh
end
def refresh
  self.contents.clear
    self.contents.draw_text(-60, 0, 150, 32, $game_party.gold.to_s, 2)
    self.contents.draw_text(100, 0, 150, 32, "Gold")
  end

def update
  super()
 @gold != $game_party.gold
 @gold = $game_party.gold
 refresh
end
end

class Scene_Map
  alias goldhud_main main
  alias goldhud_update update
  def main
    @goldhud = Window_GoldHUD.new
    goldhud_main
    @goldhud.dispose
  end
  def update
    @goldhud.visible = $game_switches[1]
    @goldhud.update    
    goldhud_update
  end
end

Thanks!
 

khmp

Sponsor

AbyssalLord":38r0jgp7 said:
Thanks for the help!

EDIT:Here's my new question:

I know how to make the HUD appear and disappear based on a switch, but how do I stop it from appearing for just a second whenever I return to the map screen (from the menu, starting the game, etc.)?  Thanks in advance!

Here's the code:
Code:
class Window_GoldHUD < Window_Base
  def initialize
    super(400,0,200,60)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 150
    
  refresh
end
def refresh
  self.contents.clear
    self.contents.draw_text(-60, 0, 150, 32, $game_party.gold.to_s, 2)
    self.contents.draw_text(100, 0, 150, 32, "Gold")
  end

def update
  super()
 @gold != $game_party.gold
 @gold = $game_party.gold
 refresh
end
end

class Scene_Map
  alias goldhud_main main
  alias goldhud_update update
  def main
    @goldhud = Window_GoldHUD.new
    goldhud_main
    @goldhud.dispose
  end
  def update
    @goldhud.visible = $game_switches[1]
    @goldhud.update    
    goldhud_update
  end
end

Thanks!

You kids and your spoilers. You need to fashion some sort of timer. My question for you is. Do you mind if the HUD comes up after a second or two whenever you enter Scene_Map or only coming from Scene_Map? Or is the Map something that is accessible from the map and you never leave the scene?

If you don't mind it not appearing for a couple seconds every time you go to Scene_Map:
Code:
class Scene_Map

  HUD_HIDE_FRAMES = 30 # The number of frames to hide the hud for.

  alias goldhud_main main
  alias goldhud_update update
  def main
    @goldhud = Window_GoldHUD.new
    @timer = HUD_HIDE_FRAMES
    goldhud_main
    @goldhud.dispose
  end
  def update
    # Update the timer.
    @timer -= 1 if @timer > 0

    # If the timer has reached zero use the switch.
    # If the timer has not reached zero don't check the switch just set it to false.
    @goldhud.visible = @timer == 0 ? $game_switches[1] : false

    @goldhud.update    
    goldhud_update
  end
end

Really though I'd need to know how you handle the map thing for both.
 

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