Basic HUD Tutorial

Yeyinde's Basic HUD Tutorial


Introduction

This is a tutorial to help people learn how to script a basic HUD.

Getting Started

If you want to make a good HUD, you are going to need some basic knowledge of RGSS, but not much.
There are two parts to this tutorial: The Window, and the Scene. Absolutly no screenshots will be taken; I trust you to know if it looks right, or not.
First of all, Hit F11 on your keyboard. Scroll down and highlight Main, then press Insert. This creates a new script holder for us to use.

The Window

In order to have a HUD, you will need a Bitmap, which is in a Window, or a Sprite. I think people find Windows easier, so that's what we will use.

To create a window, we type in two lines:
class Window_YourHUD < Window_Base
end
Expand to see the code.
That creates the holder for the window(Window_Base)
Currently, our window isn't quite a window. To fix this, will will make the initialize method.
class Window_YourHUD < Window_Base
  def initialize
  end
end
Expand to see the code.
This alone won't do anything, so we will call Window_Base's initialize method with super, followed by a bitmap creation. While we are there, let's call the refresh method
class Window_YourHUD < Window_Base
  def initialize
    super(x, y, width, height)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
end
Expand to see the code.
Of course, the x, y, width, and height values are set by you. REMEMBER! width and height must be larger than 32, or you will get an error.
Now, we will create the refresh method. This is the method that draws all the text, and junk.
class Window_YourHUD < Window_Base
  def initialize
    super(x, y, width, height)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  def refresh
    self.contents.clear
  end
end
Expand to see the code.
As you can see, I threw in the line 'self.contents.clear'. This readies the bitmap for us to draw on.
Next, we will add a method I like to do to keep it all organized, the reset_variables method.
class Window_YourHUD < Window_Base
  def initialize
    super(x, y, width, height)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  def refresh
    self.contents.clear
    reset_variables
  end
  def reset_variables
  end
end
Expand to see the code.
In the reset_variables method, we will set four variables: HP, MAXHP, SP, asn MAXSP of the first character.
class Window_YourHUD < Window_Base
  def initialize
    super(x, y, width, height)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  def refresh
    self.contents.clear
    reset_variables
  end
  def reset_variables
    @actor = $game_party.actors[0]
    @old_hp = @actor ? @actor.hp : 0
    @old_maxhp = @actor ? @actor.maxhp : 0
    @old_sp = @actor ? @actor.sp : 0
    @old_maxsp = @actor ? @actor.maxsp : 0
  end
end
Expand to see the code.
Now, I bet you're asking "What is up with the @actor ? @actor.value : 0?" I do this as a safety precaution. If you try and call the hp method of a party member who is not there, an error will occur. Errors are bad.
Now that that's settled, we will finish the refresh method.
class Window_YourHUD < Window_Base
  def initialize
    super(x, y, width, height)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  def refresh
    self.contents.clear
    reset_variables
    return if !@actor
    draw_actor_hp(actor, x, y)
    draw_actor_sp(actor, x, y)
  end
  def reset_variables
   @actor = $game_party.actors[0]
   @old_hp = @actor ? @actor.hp : 0
   @old_maxhp = @actor ? @actor.maxhp : 0
   @old_sp = @actor ? @actor.sp : 0
   @old_maxsp = @actor ? @actor.maxsp : 0
  end
end
Expand to see the code.
It wasn't much. Just a return if the @actor was nothing(!), and draw hp and sp at coordinates that you decide.
Finally, we will make the window refresh if necessary. We will do this in the update method.
class Window_YourHUD < Window_Base
  def initialize
    super(x, y, width, height)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  def refresh
    self.contents.clear
    reset_variables
    return if !@actor
    draw_actor_hp(actor, x, y)
    draw_actor_sp(actor, x, y)
  end
  def reset_variables
   @actor = $game_party.actors[0]
   @old_hp = @actor ? @actor.hp : 0
   @old_maxhp = @actor ? @actor.maxhp : 0
   @old_sp = @actor ? @actor.sp : 0
   @old_maxsp = @actor ? @actor.maxsp : 0
  end
  def update
    super
    refresh if (@actor = $game_party.actors[0] or
                @old_hp = @actor ? @actor.hp : 0 or
                @old_maxhp = @actor ? @actor.maxhp : 0 or
                @old_sp = @actor ? @actor.sp : 0 or
                @old_maxsp = @actor ? @actor.maxsp : 0)
  end
end
Expand to see the code.
That's our window! Next comes the edit of Scene_Map. For this, we will be using the very useful alias command.
class Scene_Map
  alias yourhud_main main
  alias yourhud_update update
end
Expand to see the code.
We just set up the aliases. Now you will see why aliases are very useful.
class Scene_Map
  alias yourhud_main main
  alias yourhud_update update
  def main
    @yourhud = Window_YourHUD.new
    yourhud_main
    @yourhud.dispose
  end
  def update
    @yourhud.update
    yourhud_update
  end
end
Expand to see the code.
Wow! You just shortened up a huge amount of code into a mere 11 lines! Aliases allow one to insert, and do things to a variable while keeping compatibility to a maximum.

End Product
[LEFT]That's the end of my tutorial, so save and test play your window. If you got an error, go back and try this tutorial again. I don't know why you would get an error; it's so easy to make HUDs.

Later, until next time. Yeyinde, the Local Yautja
[/LEFT]
I'm really interested in making a simple HUD for my game but I don't seem to get one from following your tutorial.

Could you break it down to exactly which bits of your code go where?
I've got the creation of a new script and typed up the window creation, but I dont know where the aliases and such go :/
You granted my wish!

I WILL understand this! :D ^_^
Meisme :( How could you? You didn't comment anything :(
Mr.Mo wrote:
Meisme :( How could you? You didn't comment anything :(
Mr. Mo, I just don't like to comment much. It's my scripting style.
Where do we put the last piece of code, surely we don't replace all of Scene_Map with just that?
You put it right below your HUD.
I'm getting an error:
ArgumentError

wrong number for arguments (0 for 4)

on line 34

which is: @yourhud = Window_YourHUD.new
I see. This was the version of the tutorial before I fixed the initialize bug
Change def initalize to def initialize
thanks! done that, now I'm getting:

Script 'HUD' line 3: RGSSError occoured.

disposed window
If you're using the SDK in the part of the script:
class Scene_Map
  alias yourhud_main main
  alias yourhud_update update
  def main
    @yourhud = Window_YourHUD.new
    yourhud_main
    @yourhud.dispose
  end
  def update
    @yourhud.update
    yourhud_update
  end
end
Expand to see the code.


delete @yourhud.dispose as the windows on Scene_Map are automatically disposed if you use the SDK.
thanks man =]

looks great.

I'm gunna try and work around RGSS, my freind does it for me but I guess learning it myself will make life easier lol.
one of the tutorials i was really able to follow!

Code of my hud Spoiler
class Window_YourHUD < Window_Base
  def initialize
    super(0, 416, 640, 64)
    self.contents = Bitmap.new(640 - 32, 64 - 32)
    refresh
  end
  def refresh
    self.contents.clear
    reset_variables
    return if !@actor
    draw_actor_hp(@actor, 0, 0)
    draw_actor_sp(@actor, 320, 0)
  end
  def reset_variables
    @actor = $game_party.actors[0]
    @old_hp = @actor ? @actor.hp : 0
    @old_maxhp = @actor ? @actor.maxhp : 0
    @old_sp = @actor ? @actor.sp : 0
    @old_maxsp = @actor ? @actor.maxsp : 0
  end
  def update
    super
    refresh if (@actor = $game_party.actors[0] or
                @old_hp = @actor ? @actor.hp : 0 or
                @old_maxhp = @actor ? @actor.maxhp : 0 or
                @old_sp = @actor ? @actor.sp : 0 or
                @old_maxsp = @actor ? @actor.maxsp : 0)
  end
end
class Scene_Map
  alias yourhud_main main
  alias yourhud_update update
  def main
    @yourhud = Window_YourHUD.new
    yourhud_main
    @yourhud.dispose
  end
  def update
    @yourhud.update
    yourhud_update
  end
end
Expand to see the code.
This tutorial is great! However, this may sound like a noob question but how do you make it so that it shows a number which is stored in a variable? And how to make the HUD show when a switch is on and disappear once it's off?

Thanks
You can show it with the draw_text method. Juat use after self.contents.clear:
self.contents.draw_text(x, y, width, height, text[, align])
Expand to see the code.

Just use the variable you want in the "text" field. To make your HUD visible depending on a switch, just add below @yourhud.update
@yourhud.visible = $game_switches[[i]id[/i]]
Expand to see the code.


Where id is the id of the switch you want to use.
Ok.. I've managed to get the switch part working... But, I'm still having troubles with displaying the variables on screen.

This is what I did just after self.contents.clear:

self.contents.draw_text(20, 20, 200, 200, $game_variables[1])

However, in game, I get this msg "cannot covert Fixnum into String"

Thanks
Use $game_variables[1].to_s
how would i use script calls to turn this script on and off?
Hmm, this isn't working for me
I'm wandering what is the x y positions?
I have my hud to appear in the upper right corner.
and I also have the window (480, 0, 160, 64)
and want the HP and SP to be something like this
Hp ????/????==
==SP ????/????

-=NOTE=-
the == are spaces
What does make the variable super, is it a variable which is already defined or can i make in every Scene everytime a new variable caled the same?
How do I use a sprite instead of a window?
The easiest way I know to do this is, first do you have HP/Max HP and SP/MaxSP defined.

So...
#Show HP in Numericals
self.contents.draw_text(x, y, 640, 32, "HP")
self.contents.draw_text(x+10, y, 640, 32, $actor_hp.to_s)
self.contents.draw_text(x+13, y, 640, 32, "/")
self.contents.draw_text(x+16, y, 640, 32, $actor_maxhp.to_s)
#Show SP in Numericals
self.contents.draw_text(x, y, 640, 32, "SP")
self.contents.draw_text(x+10, y+10, 640, 32, $actor_sp.to_s)
self.contents.draw_text(x+13, y+10, 640, 32, "/")
self.contents.draw_text(x+16, y+10, 640, 32, $actor_maxsp.to_s)
Expand to see the code.


Replace the X and Y values with something of your choice, then addition part should be simple enough, just add that value onto your x or y value.

I'll try to give a better representation when I get home later, hard to do trial and error at school.
Hello, I've tried this various ways. I've tried the exact one in the original post. I've tried the 'initialize' update, and I've tried removing @yourhud.dispose

I still get

Script 'HUD' line 3: RGSSError occoured.

disposed window


What else should I be lookin out for? I'm not great with scripting, what can be up with line 3?
Let's say I wanted to make the appearance of the HUD conditional... I've tried adding an IF statement in the Scene_Map class like this:

class Scene_Map
  alias yourhud_main main
  alias yourhud_update update
  def main
    if $battle
      @yourhud = Window_YourHUD.new
      yourhud_main
      @yourhud.dispose
    else
      yourhud_main
    end
  end
  def update
    if $battle
      @yourhud.update
      yourhud_update
    else
      yourhud_update
    end
  end
end
Expand to see the code.


where $battle is a created global variable that I control with event scripting (Boolean). The code works fine if I comment out the IF statement itself (and the else code) but as soon as I throw the code into this conditional, It tries to tell me that @yourhud.update doesn't exist. (the code for Window_YourHUD is unmodified)

What's the deal with that?
Ok I need help with this, I've got it exactly the same as the Tutorial, but with my own x+y co-ordinates.

But whenever I come to test it, I get this error:

????? 'Window_YourHUD' ? 4 ??? NameError ????????
uninitialized constant Window_YourHUD::Bitamp

Can someone tell me what it means and how to fix it please, Because everytime I've tried a tutorial, I get the very same error.
foftime, just put @your_hud.visible = $battle right after @your_hud.update and @your_hud = Window_YourHUD.new. No need for conditionals.

jbrist, It's Bitmap, not Bitamp.
[QUOTE=Yeyinde;144828]foftime, just put @your_hud.visible = $battle right after @your_hud.update and @your_hud = Window_YourHUD.new. No need for conditionals.
[/QUOTE]

Shexy, thanks!
-A
Thats what I meant to type, I even Copied&Pasted it to see if it would work, and it still doesn't.
Followed every direction and it turned out perfectly, although I'm considering changing the draw_actor_hp to two lines:

self.contents.draw_text(x, y, width, height, "HP: ")
self.contents.draw_Text(x, y, width, height, $game_party.actors[0].hp.to_s + "/" + $game_party.actors[0].maxhp.to_s)
...
etc.

the x, y, width, height, you just plug it wherever you wish.

I added Sephiroth's "Bars". This way you can adjust where you display "HP", and the actual HP further apart if you wish -- since the default method only lets you have so much space inbetween the two. The only type of bars I can do is the beginner ones for now.

To Illustrate the differences:

HUD Window with Draw_actor
HUD Window with self.contents.draw_text...

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=9404