HTHUD: Part 3 – DisplayHealth()   3 comments

Video Version

Subject: HTHUD: Part 3 – DisplayHealth()
Skill Level: Beginner
Run-Time: 22 Minutes
Author: Michael Allar
Notes: Displaying our current health to the screen.

Streaming:     720×480 1920×1080

Download:     Low-Res (73MB) Hi-Res (100MB)

Written Version

Subject: HTHUD: Part 3 – DisplayHealth()
Skill Level: Beginner
Author: Michael Allar
Notes: Displaying our current health to the screen.

HTHUD – Variables

For this tutorial, we will be introducing four new variables for our HTHUD class.

var bool bShowHealth;
var vector2d HealthPosition, HealthTextOffset;
var TextureCoordinates HealthBGCoords;

bShowHealth will toggle the showing of our Health. HealthPosition, HealthTextOffset, and HealthBGCoords function just like their ammo counter-parts what we have made in the previous tutorials.

HTHUD – DisplayHealth()

You will find that our DisplayHealth method is almost identical to our DisplayAmmo function.

The first thing we need to do is declare some local variables for us to use during the calculation stages of our ammo drawing.

local vector2d POS, HealthTextOffsetPOS;
local string Amount;
local int HealthCount;

These work just as their Ammo counter-parts in DisplayAmmo do. However notice we do not have TX or TY, as I removed as I do not plan to ever right-align my health display text.

// Resolve the position
 POS = ResolveHudPosition(HealthPosition,HealthBGCoords.UL,HealthBGCoords.VL);

 HealthCount = PawnOwner.Health;

 // Draw the Health Image Widget
 Canvas.SetPos(POS.X,POS.Y);// - (HealthBarOffsetY * ResolutionScale));
 Canvas.DrawColorizedTile(HudTexture, HealthBGCoords.UL * ResolutionScale, HealthBGCoords.VL * ResolutionScale, HealthBGCoords.U, HealthBGCoords.V, HealthBGCoords.UL, HealthBGCoords.VL, HudTint);

 // Draw the amount
 Amount = ""$HealthCount;
 Canvas.DrawColor = WhiteColor;

 HealthTextOffsetPOS = ResolveHUDOffset(POS, HealthTextOffset);
 Canvas.SetPos(HealthTextOffsetPOS.X,HealthTextOffsetPOS.Y);
 Canvas.DrawText(Amount,,HUDFontScale.X * ResolutionScale,HUDFontScale.Y * ResolutionScale);

Once again, you will see that its exactly the same code as DisplayAmmo, but with a few less lines. The lines removed are ones that calculate TX and TY, as we will not be right-aligning the health text and therefore it is a waste of time to calculate them. If you require right aligned health text, go ahead and copy the DisplayAmmo function exactly, using our Health variables instead of our Ammo variables.

We do however do something new, and that is assign HealthCount (previously AmmoCount) to PawnOwner.Health. PawnOwner is the pawn that owns this instance of the HUD (our player), and Health is a variable that is declared and managed in the base Pawn class. We can grab our current health by just pulling from the data in the Health variable. We then draw it just like we’ve drawn the ammo text.

HTHUD: DrawLivingHUD()

/**
 * Anything drawn in this function will be displayed ONLY when the player is living.
 */
function DrawLivingHud()
{
local HTWeapon Weapon;

// Manage the weapon.  NOTE: Vehicle weapons are managed by the vehicle
// since they are integrated in to the vehicle health bar

if( PawnOwner != none )
      {

          if ( bShowHealth )
          {
                DisplayHealth();
          }

          Weapon = HTWeapon(PawnOwner.Weapon);
          if ( Weapon != none )
          {
               if ( bShowAmmo )
               {
                    DisplayAmmo(Weapon);
               }
               if (bShowCrosshair)
               {
                    Weapon.DrawWeaponCrosshair(self);
               }
          }
     }
}

What we have done here is add a check to see if we should show our health, and then display it if we do. Notice how this is inside the PawnOwner check but not in the Weapon check. This is so we always display our Pawn’s health if we have a Pawn and it doesn’t rely on us having a weapon to show our health.

HTHUD: DefaultProperties

Now we get to define our four new variables and put them to use!

defaultproperties

{

//...

 HealthPosition=(X=0,Y=-1) //Position of our Health sprite. Set here to our bottom left corner.
 HealthTextOffset=(X=80,Y=0) //Offsets text 80 pixels right from the Health sprite.
 HealthBGCoords=(U=131,UL=80,V=0,VL=80) //Pixel coordinates of our Health Sprite in our HUDTexture

 bShowHealth=true //Let us display Health on our HUD.

// ...

}

Result:

Our Health Hud Display In Action

Download Project Source Code Here

Posted March 27, 2010 by Allar in Unreal

Tagged with , ,

3 responses to HTHUD: Part 3 – DisplayHealth()

Subscribe to comments with RSS.

  1. Hi ,
    i just could not display a text message on the hud canvas..

    what i didi was this:
    1> changed gameinfo to my gameinfo class in the config-> defaultgame.ini

    2> extended my game info class from "utgame"

    3> in my game info default properties..i added Hudtype to my Hud class

    4> i extend my hud class from UThud (not UDKhud)

    5> and i created a drawhud() in my hud class and added:
    canvas.drawtext("bumba")
    but when i compile.. i get no error , but my message is not shown either..
    can you help ?

    • There is lots of differences from using the UT classes instead of the lower defined ones which this tutorial works with, however, do you ever set the Canvas position?

      Use Canvas.SetPos(X,Y) to set the position of where you want to draw.
      Then use Canvas.DrawColor = WhiteColor to start drawing in white.
      Then use Canvas.DrawText("bumba",,1.0f,1.0f) to draw your text with a scale of 1.

  2. Allar..you are awesome as ususal . your `log () helped me a lot.
    the mistake i was doing repeatedly that even if i declared which map to be used and which game info class to be used ( in config-> Editor/Game ini), i really did not establish a connection between the two..(means when it loads my map, it should load my game info clas too…)
    in allars tutorial..i found the solution as i had to include

    DefaultMapPrefixes(0)=(Prefix="HT",GameType="bumba .bumbagameinfo")

    My map was previously named as "bumbamap.udk"
    but i gave a prefix "HT-bumbamap.udk"

    and added to my game info class default properties:

    DefaultMapPrefixes(0)=(Prefix="HT",GameType="bumba .bumbagameinfo")

    thats all..
    and my game info started reading my HUd class properly..

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>