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.

How to make a plug and play HUD?

Jason

Awesome Bro

Okay, so I can made HUD's fine, however, in order for them to show I've been having to use a script call, which isn't reall the most efficient way to do it. How would I go about making a HUD display on the game screen automatically without the user having to tinkle about with anything?
 
The alternative to Scene_Map is Spriteset_Map, it doesn't really matter.

I wrote a small example:
Ruby:
# The HUD is displayed as an instance variable of Spriteset_Map

# so here are some aliases

class Spriteset_Map

  alias_method :how_to_create_a_hud_initialize, :initialize

  def initialize

    @example_hud = Jbrist_HUD.new

    how_to_create_a_hud_initialize

  end

  

  alias_method :how_to_create_a_hud_update, :update

  def update

    @example_hud.update

    how_to_create_a_hud_update

  end

  

  alias_method :how_to_create_a_hud_dispose, :dispose

  def dispose

    @example_hud.dispose

    how_to_create_a_hud_dispose

  end

end

 

class Jbrist_HUD < Window_Base

  def initialize

    # Create the Window

    super(512, 0, 128, 96)

    self.contents = Bitmap.new(96, 64)

    # Update

    update

  end

  

  def update

    super()

    # Check if one of the values has changed

    new_actor_hp = $game_party.actors[0].maxhp

    new_variable = $game_variables[1]

    if @old_actor_hp != new_actor_hp or @old_variable != new_variable

      # Update values and refresh

      @old_actor_hp = new_actor_hp

      @old_variable = new_variable

      refresh

    end

  end

  

  def refresh

    self.contents.clear

    self.contents.draw_text(0, 0, 96, 32, "#{@old_actor_hp}")

    self.contents.draw_text(0, 32, 96, 32, "Var 1: #{@old_variable}")

  end

end
 
@ DeM0nFiRe: I think that is a matter of taste ;)
Since the Spriteset should include everything which has to do with graphics in my opinion Enterbrain should have put the Windows there as well :P
 

Jason

Awesome Bro

Neo-Bahamut":3tcd2qe6 said:
The alternative to Scene_Map is Spriteset_Map, it doesn't really matter.

I wrote a small example:
Ruby:
# The HUD is displayed as an instance variable of Spriteset_Map

# so here are some aliases

class Spriteset_Map

  alias_method :how_to_create_a_hud_initialize, :initialize

  def initialize

    @example_hud = Jbrist_HUD.new

    how_to_create_a_hud_initialize

  end

  

  alias_method :how_to_create_a_hud_update, :update

  def update

    @example_hud.update

    how_to_create_a_hud_update

  end

  

  alias_method :how_to_create_a_hud_dispose, :dispose

  def dispose

    @example_hud.dispose

    how_to_create_a_hud_dispose

  end

end

 

class Jbrist_HUD < Window_Base

  def initialize

    # Create the Window

    super(512, 0, 128, 96)

    self.contents = Bitmap.new(96, 64)

    # Update

    update

  end

  

  def update

    super()

    # Check if one of the values has changed

    new_actor_hp = $game_party.actors[0].maxhp

    new_variable = $game_variables[1]

    if @old_actor_hp != new_actor_hp or @old_variable != new_variable

      # Update values and refresh

      @old_actor_hp = new_actor_hp

      @old_variable = new_variable

      refresh

    end

  end

  

  def refresh

    self.contents.clear

    self.contents.draw_text(0, 0, 96, 32, "#{@old_actor_hp}")

    self.contents.draw_text(0, 32, 96, 32, "Var 1: #{@old_variable}")

  end

end

This confuses me, however, it works so I'm gonna' keep going over it until I understand all these aliasing thingies and stuff.

By the way, what is aliasing used for? All I see is giving it a random name of your choice and calling it whenever you feel like it? I seriously don't get it, lol. Care to explain what they are and why they're used? :thumb:
 
@ Dem0nFiRe: Matter of taste :p

@ Jbrist: You don't have to understand the aliasing part if you don't want to. It's enough if you change the variable and class name there :)

alias and alias_method does nothing more than just copying a method to a new name.
Code:
# This code:

def test(string)

 p string

end

 

alias test_2 test

 

# Is the same as this code:

def test(string)

 p string

end

 

def test_2(string)

 p string

end

Usually alias is used to add content to a method.
Code:
def test(string)

 p string

end

 

test('Hello') # gives out "Hello"

 

alias test_alias test

 

def test(string) # the original method is rewritten while the copy stays

 new_string = string + " World" # the string is modified

 test_alias(new_string) # the original method is called with a modified string

end

 

# now, whenever some script wants to call the "test" method

# it still can do this but the printed string has always "World" at its end

I hope this helped a bit :)
 
Let me also mention there's a few aliasing tutorials out there, for example Me(tm)s Aliasing Tutorial, which sadly isn't very elaborative. You might want to look for better ones, but really, all you need to understand is the basic syntax:

[rgss]class Foo
  alias sevendwarfs foobar # syntax: 'alias' (the word), placeholder method name, original method name
  def foobar
    # content to be added before executing original code goes here
    sevendwarfs
    # content to be added after executing original code goes here
  end
end
[/rgss]

So, as you can see, 'sevendrarfs' makes little to no sense (relatively, as foobar isn't really too sense-making anyway :p ), so what you mainly want to do is yourscript_methodname in a short fashion, like pixelmove_update.

Aliased code is merged with the original method, not replacing it as if you'd just make another update definition. My efforts to pack all of the above in a look-at-it-and-get-it-image you can print out, have your girlfriend make a lipstick-kiss-signature on it and write my name below (I intentionally kept the original method's contents short, making room for that in the upper-right corner :D ) have resulted in this:

aliasexplanation.png
 
if you would like an example of a really simple hud i made for a friend like a year ago. it is 100% plug and play, user just needs graphics. before someone gets all pissy let me say it: my update should not call refresh unless something changed, instead it is called every frame which could slow things down, but since its so simple it really doesn't give any issues, so i never fixed it.

[rgss] 
#==============================================================================
# ** Plague180_Hud
#------------------------------------------------------------------------------
# This class draws a simple hud.
#==============================================================================  
class MainHud < Window_Base
  def initialize
    super(-10,-10,175,480)
    self.contents = Bitmap.new(width, height)
    self.contents.font.size = 16
    self.opacity = 0
    self.windowskin = nil
    self.z = 1000
    refresh
  end  
  def refresh
    self.contents.clear
    draw_hud
    draw_text
  end  
  def update
    refresh
  end  
  def draw_hp_bar(x, y,current,to_next,width = 176)
    bar_y = y + (Font.default_size * 2 /3)
    @skin = RPG::Cache.windowskin("HPMeter")
    @width  = @skin.width
    @height = @skin.height / 3
    src_rect = Rect.new(0, 0, @width, @height)
    self.contents.blt(x , bar_y, @skin, src_rect)  
    @line   = (current == 100 ? 2 : 1)
    @amount = (100 == 0 ? 0 : 100 * current / to_next)
    src_rect2 = Rect.new(0, @line * @height, @width * @amount / 100, @height)
    self.contents.blt(x, bar_y, @skin, src_rect2)
  end
  def draw_exp_bar(x, y,current,to_next,width = 176)
    bar_y = y + (Font.default_size * 2 /3)
    @skin = RPG::Cache.windowskin("EXPMETER")
    @width  = @skin.width
    @height = @skin.height / 3
    src_rect = Rect.new(0, 0, @width, @height)
    self.contents.blt(x , bar_y, @skin, src_rect)  
    @line   = (current == 100 ? 2 : 1)
    @amount = (100 == 0 ? 0 : 100 * current / to_next)
    src_rect2 = Rect.new(0, @line * @height, @width * @amount / 100, @height)
    self.contents.blt(x, bar_y, @skin, src_rect2)
  end
  def draw_hud
    for i in 0...$game_party.actors.size
      bitmap = RPG::Cache.picture("Size2")
      @actor = $game_party.actors
      charcurrent = @actor.now_exp
      chartonext = charcurrent + @actor.next_rest_exp_s.to_i
      cx = (i*55)
      self.contents.blt(0,  20+cx, bitmap, Rect.new(0, 0, 175,100))
      self.contents.font.color = Plague180_Color.get_color(1)
      self.contents.font.bold = true
      self.draw_hp_bar(7,16+cx, @actor.hp, @actor.maxhp)
      self.draw_exp_bar(7,27+cx, charcurrent, chartonext)
    end  
    def draw_text
      for i in 0...$game_party.actors.size
        @actor = $game_party.actors
        cx = (i*55)
        charcurrent = @actor.now_exp
        chartonext = charcurrent + @actor.next_rest_exp_s.to_i
        charhp = "Hp: " + @actor.hp.to_s + "/" + @actor.maxhp.to_s
        charexp = charcurrent.to_s + "/" +chartonext.to_s
        hpcenter = 110 - (charhp.length * 6)
        expcenter = 70 - (charexp.length * 6)
        charname = @actor.name
        charclass = @actor.class_name
        charnamespace = charname + "(" + charclass + ")"
        charcenter = 120 - (charnamespace.length * 6)
        self.contents.draw_text(charcenter,12+cx, 175, 32, charnamespace)
        self.contents.font.size = 12
        self.contents.draw_text(hpcenter,22+cx,175,32, charhp)
        self.contents.draw_text(expcenter,33+cx,175,32, "Exp: " + charexp)
      end
    end  
  end
end
#===============================================================================
# Game_Actor
#===============================================================================
class Game_Actor < Game_Battler
  def now_exp
    return @exp - @exp_list[@level]
  end
end
#===============================================================================
# Scene_Map
#===============================================================================
class Scene_Map
  alias plague180_hud_main main
  alias plague180_hud_update update
  def main
    @hud = MainHud.new
    plague180_hud_main
    @hud.dispose
  end
  def update
    @hud.update
    plague180_hud_update
  end  
end
[/rgss]
if you want the graphics to play with this let me know(or you could make your own). i couldn't find the size2 graphic but here is what it looks like more or less.
r2rfpe.jpg
 

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