Basic HUD/GUI

#===============================================================================
# HotFireLegend recently requested I make a small example of a HUD and explain 
# the workings behind my example. Here it is. 
# 
# NOTES: 
# - This example CAN (and should) be placed in your script editor. It 
# increases readability AND allows you to test the script and see it in action.
# - This 'tutorial' assumes you understand the basics of writing ruby code.
#===============================================================================

#===============================================================================
# 
# First, I need to make a class for my HUD. 
# I have opted to make a very simple sprite class for this example. 
# Of course, a class that makes multiple sprites would be avantageous depending
# on the type of hud you are trying to create. 
# 
# Sprites are the basic concept used to display Bitmap images on the game screen.
# 
#===============================================================================
class Time_HUD < Sprite
#===============================================================================
  #-----------------------------------------------------------------------------
  # creates sprite and bitmap.
  # super calls the initialize method from parent class 'Sprite'
  # this MUST be done before attempting to add a bitmap to the sprite.
  # Calls update method after creating bitmap to ensure data is printed 
  # immediately - it is somewhat noticeable if this is not done.
  #-----------------------------------------------------------------------------
  def initialize(viewport)
    super(viewport)
    self.bitmap = Bitmap.new(100,24) # Creates a new Bitmap object. 100x24
    update
  end
  #-----------------------------------------------------------------------------
  # updates sprite and bitmap. 
  # super calls update method of sprite class - not really necessary unless
  # you are creating a flash effect on the sprite.
  # self.bitmap.clear simply wipes the bitmap to ensure that it is 'blank'
  # prior to adding the new text.
  # self.bitmap.draw_text(x,y,width,height,text,alignment)
  # ^ this line draws the text onto the bitmap.
  # $game_system.playtime_s is defined within Game_System class and simply
  # returns a string (string of text) showing the time the game has been played
  #-----------------------------------------------------------------------------
  def update
    super
    self.bitmap.clear  # clears bitmap
    self.bitmap.draw_text(0,0,100,24, $game_system.playtime_s, 1)
  end
  #-----------------------------------------------------------------------------
  # calls dispose method on bitmap and then (due to super) on the sprite. 
  #-----------------------------------------------------------------------------
  def dispose
    self.bitmap.dispose if self.bitmap
    super
  end
  #-----------------------------------------------------------------------------
  # 
  #-----------------------------------------------------------------------------
end
#===============================================================================
# Now, we are adding our new class into 'Spriteset_Map' 
# This is a class that is included in Vx Ace default scripts and shows many
# various sprites (such as tileset and characters) on the map.
#===============================================================================
class Spriteset_Map
#===============================================================================
  #-----------------------------------------------------------------------------
  # Aliased Methods
  #-----------------------------------------------------------------------------
  alias :init_hud :initialize
  alias :disp_hud :dispose
  alias :updt_hud :update
  #-----------------------------------------------------------------------------
  # Creates new instance of Time_HUD class and passes the argument of class
  # variable '@viewport2' (defined within default spriteset map)
  #-----------------------------------------------------------------------------
  def initialize
    init_hud # Calls alias data
    @time_hud = Time_HUD.new(@viewport2)
  end
  #-----------------------------------------------------------------------------
  # Disposes time hud BEFORE other dispose methods are carried out. 
  # This simply ensures that the sprite will be disposed correctly and does
  # not create any memory leaks. 
  #-----------------------------------------------------------------------------
  def dispose
    @time_hud.dispose if @time_hud
    disp_hud # Calls alias data
  end
  #-----------------------------------------------------------------------------
  # Updates time hud. This can be done before or after alias data is called, 
  # Personally, i prefer before :)
  #-----------------------------------------------------------------------------
  def update
    @time_hud.update if @time_hud
    updt_hud # Calls alias data
  end
  #-----------------------------------------------------------------------------
  # 
  #-----------------------------------------------------------------------------
end

4 thoughts on “Basic HUD/GUI

  1. G.V says:

    Great tutorial/script on The basics on a Hud/Gui. Have’nt visited your site for a while due to the long hours of developing my project
    in which your scripts are exclusively used. As a solo developer, the long hours i mentioned are spent actually reading and understanding
    the scripts i actually use, not just copy and pasting something in without at least having some knowledge of what it actually does so
    that i can decide whether i actually want to use it or not ( I’ve experimented with hundeds over the years and 99.9 of them ended up
    getting deleted.) My point is (sorry for the the rambling) is that i find your scripts and tutorials very educational ( i mentioned in the past
    that i have previous knowledge of C++, old dos ect. ect. , so ruby is still kind of new to me but i shares a lot in common with the others).
    Just a thank you for your hard work and keep it up.You are amonst a very small few who can write scripts that i can actually comprehend.

    -ghostknight4@hotmail.com

    1. Dekita-RPG says:

      Thanks 🙂

      I sometimes wonder if anyone is actually benefiting from all the shit I write in my script documentation, but before I started scripting, the lack of information in scripts really annoyed me. Like, there would be scripts that boasted features I wanted, but no information on how to use said features. Fomar’s scripts where quite bad for this.

      Obviously once I knew more code I found it much easier using other peoples scripts, but I ended up just scripting all my own features so I would learn more 😀

      So yea, I am pleased you took the time to message. And also, that you have benefited from both my scripts and tutorials 🙂

      I do plan to keep at it and hopefully, bring some new unexpected things to RPG Maker Games 😀
      My current project, Mahō no kādo, is a clear example of the kind of things I want to be doing in the future. In case you are interested and have not seen, here is a link 🙂
      https://dekitarpg.wordpress.com/tcg-mnk/

    2. Ira Coey says:

      Hey Dude I know this is 2 years old or maybe longer, I also feel the lack of information given to create things. I don’t know much about scripting but the basics. I just wanted to know where did you start to code, I really wanna follow in your footsteps into creating.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s