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.

HUD Problems

Ok, I have made a HUD that is supposed to display certain variables/gold based on which switches are active.  I've got it working all except for one thing that I can't figure out.  Whenever I activate the HUD (using switch 1), a blank window appears.  So far so good (since the other switches are not on yet).  Now, the issue is what happens with all of the other switches.  When I turn on switch 2 (for example), the text does not appear.  Only once I open the menu (or otherwise navigate away from that map) and then return to the map is the text there.  I would appreciate any help you could give. 

Here's the code:

Code:
class Window_GoldHUD < Window_Base
  def initialize
    super(440,0,200,60)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.opacity = 255
 
        
  refresh
end
def refresh
  self.contents.clear
  reset_gold
    if $game_switches[2] == true
      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")
elsif $game_switches[3] == true
  self.contents.draw_text(-60, 0, 150, 32, @bank_gold.to_s, 2)
  self.contents.draw_text(100, 0, 150, 32, "Gold")
elsif $game_switches[4] == true
  self.contents.draw_text(-60, 0, 150, 32, @loan_gold.to_s, 2)
  self.contents.draw_text(100, 0, 150, 32, "Owed")
end
end
def reset_gold
 @gold = $game_party.gold
 @bank_gold = $game_variables[1] #Change the [1] to whatever variable ID you want
                                 # to use to display the gold in the bank.
 @loan_gold = $game_variables[6]#Change the [6] to whatever variable ID you want
                               #to use to display the gold you owe the bank(loans).
end                              #You must do the same below for this to work.

def update
  super()
 refresh if ( @gold != $game_party.gold or
 @bank_gold != $game_variables[1] or #If you changed the ID's above, you must 
 @loan_gold != $game_variables[6] )  #change them to the same ID's here.
 end
end

class Scene_Map
  alias goldhud_main main
  alias goldhud_update update
  def main
    @goldhud = Window_GoldHUD.new
    @goldhud.visible = $game_switches[1] #Change this ID to change the switch that
    goldhud_main                         #makes the HUD appear.
    @goldhud.dispose
   
  end
  def update
    @goldhud.visible = $game_switches[1] #Same as above.
    @goldhud.update    
    goldhud_update
  end
end
 

khmp

Sponsor

Update is your problem. There's nothing there that says if the switch has been changed to refresh. The only way it currently refreshes, based on that update, is when the values of any gold have been changed. So there's a couple remedies to this. Save the switches for comparison within your window class.

Like:
Code:
class Window_GoldHUD < Window_Base
  def initialize
    # ... old code 
    @switch1, @switch2, @switch3 = $game_switches[2], $game_switches[3], 
      $game_switches[4]
  end
end

And then in the update include the checks:
Code:
  refresh if @switch1 != $game_switches[2] or... etc. etc.

Or might I suggest a new approach to your problem. Include one additional instance variable in your window class. This variable will replace your three switches. That can be messy anyway. If switch [2] and [6] were on it would draw [2] but [6] might have been what you wanted. Plus in order to see the proper data you have to mess with those three switches. That's kind of a bad idea. So in this approach we use one variable to dictate what gets drawn. Sounds good right? All right so lets make an additional variable called:

Code:
@gold_draw

We can use this variable to decide what should be drawn. For example. When this variable is set to zero nothing will be drawn. When this variable is set to one then it will draw the party gold. When it's two draw the gold in the bank. When it's three draw the amount of gold owed. This forces an additional line to be added to the refresh check, initialize and also the reset method right off the bat. But what we are gaining is flexibility and less reliance on switches. Its replacement being a $game_variable.

In initialize of Window_GoldHUD what changes?
* We add the line that initializes @gold_draw.
* Remove opacity = 255. (Windows default to full opacity)

In refresh of Window_GoldHUD what changes?
* We add in a return after grabbing the new values that only returns if the window isn't meant to draw anything.
* We add in a case statement that draws rather than an "if elsif end" based on @gold_draw.

In update of Window_GoldHUD what changes?
* Add the check for a change in @gold_draw.

In reset of Window_GoldHUD what changes?
* Add the saving of the @gold_draw variable.

Apart from the changes above whats new. Introduction of commenting standard. You need to start commenting especially lines in which you are unsure on what they do. Tabbing. Please tab your code properly. It looks terrible and is sometimes hard to follow when lines don't match up. This is not nitpicking. It's easier to read:
Code:
class Donkey
  def initialize
    print('I am a small horsey')
    a = b = 40
    print('Nice horsey')
    c = 'rawradfa'
  end

  def update
    return false if self.dead?
  end
end
Then:
Code:
class Donkey
def initialize
print('I am a small horsey')
 a = b = 40
  print('Nice horsey')
c = 'rawradfa'
end
def update
return false if self.dead?
end
end

Constants have also been added. When you often use $game_switches and $game_variables in code you reference there indexes to a constant. This way you don't have to go through all the code to find one line dealing with a switch number or multiple lines that use the same switch index. Instead save them in one Constant so whatever change you make to the constant is reflected in your code.

Code:
#==============================================================================
# ** Window_GoldHUD
#------------------------------------------------------------------------------
#  This class is used to display amounts of gold.
#==============================================================================

class Window_GoldHUD < Window_Base
  #--------------------------------------------------------------------------
  # * @gold_draw
  #   0 : Display nothing
  #   1 : Display party gold
  #   2 : Display gold savings
  #   3 : Display gold owed
  #--------------------------------------------------------------------------
  #--------------------------------------------------------------------------
  # * Constant Variables
  #--------------------------------------------------------------------------
  BANK_GOLD_VARIABLE = 1 # Variable index where bank gold is kept.
  OWED_GOLD_VARIABLE = 6 # Variable index where owed gold in kept.
  GOLD_DRAW_VARIABLE = 9 # Variable index where the decision of what gold to
                         # draw is kept.
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(440, 0, 200, 60)
    self.contents = Bitmap.new(width - 32, height - 32)
    @gold_draw = 0
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    # Clear the bitmap.
    self.contents.clear
    
    # Gather the new values.
    reset
    
    # Return if nothing is meant to be drawn.
    return if @gold_draw == 0
    
    # Default the strings to empty so that even if it does draw it will be
    # blank.
    gold, amount = '', ''
    
    # Decide what the proper amount and titles should be.
    case @gold_draw
    when 1
      gold = 'Gold'
      amount = @gold.to_s
    when 2
      gold = 'Gold'
      amount = @bank_gold.to_s
    when 3
      gold = 'Owed'
      amount = @loan_gold.to_s
    end
    
    # Draw the stuff finally.
    self.contents.draw_text(-60, 0, 150, 32, amount, 2)
    self.contents.draw_text(100, 0, 150, 32, gold)
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Refresh if the switch
      refresh if (@gold_draw != $game_variables[GOLD_DRAW_VARIABLE] ||
        @gold != $game_party.gold ||
        @bank_gold != $game_variables[BANK_GOLD_VARIABLE] ||
        @loan_gold != $game_variables[OWED_GOLD_VARIABLE])
  end
  #--------------------------------------------------------------------------
  # * Reset Values
  #--------------------------------------------------------------------------
  def reset
    @gold = $game_party.gold
    @bank_gold = $game_variables[BANK_GOLD_VARIABLE]
    @loan_gold = $game_variables[OWED_GOLD_VARIABLE]
    @gold_draw = $game_variables[GOLD_DRAW_VARIABLE]
  end
end

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

class Scene_Map
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias goldhud_main main
  alias goldhud_update update
  #--------------------------------------------------------------------------
  # * Constant Variables
  #--------------------------------------------------------------------------
  HUD_VISIBLE_SWITCH = 1 # The switch ID that dictates whether the HUD is 
                         # visible or not.
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    @goldhud = Window_GoldHUD.new
    @goldhud.visible = $game_switches[HUD_VISIBLE_SWITCH] 
    goldhud_main
    @goldhud.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    @goldhud.visible = $game_switches[HUD_VISIBLE_SWITCH]
    @goldhud.update    
    goldhud_update
  end
end
 

khmp

Sponsor

AbyssalLord":23d8a7c3 said:
Wow, that was extremely confusing, but as soon as I understand it (probably when it's not almost 1 in the morning), this will prove to be extremely helpful!  Thanks khmp!

If you have any questions on why or what I did I don't mind this is your thread after all :wink:
 
Ok, I just looked at it again now that I'm more awake, and it makes sense now.  Thanks for the help! (Now my scripts will be a lot less messy too!)

EDIT:  In this line, what's that last 2 do?

self.contents.draw_text(-60, 0, 150, 32, amount, 2)
 

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