HTHUD: Part 1 – Building The Base Class + Displaying Ammo   10 comments

Video Version

Subject: HTHUD: Part 1 – Building The Base Class + Displaying Ammo
Skill Level: Beginner
Run-Time: 1 Hour and 30 Minutes
Author: Michael Allar
Notes: Creating a new HUD class and having it display our current weapons ammo.

Streaming:     720×480 1920×1080

Download:     Low-Res (236MB) Hi-Res (337MB)

Video Update 3/25/2010

Run-Time: 2 Minutes
Notes: Fixes chat messages not displaying.

Streaming:     720×480

Download:     Low-Res (7MB)

Written Version

Subject: HTHUD: Part 1 – Building The Base Class + Displaying Ammo
Skill Level: Beginner
Author: Michael Allar
Notes: Creating a new HUD class and having it display our current weapons ammo.

See video for an in-depth explanation. Sorry about the indentation, it seems like my indentation will not survive copy paste… D: I will create a written tutorial soon.

HTHUD

/*******************************************************************************
 HTHUD

 Creation date: 09/03/2010 05:04
 Copyright (c) 2010, Allar

*******************************************************************************/

class HTHUD extends UDKHUD;

/** The Pawn that is currently owning this hud */
var Pawn PawnOwner;

/** Points to the UT Pawn.  Will be resolved if in a vehicle */
var HTPawn HTPawnOwner;

/** Cached reference to the another hud texture */
var const Texture2D HudTexture;

var linearcolor HudTint;

var bool bShowAmmo;
var vector2d AmmoPosition;
var TextureCoordinates AmmoBGCoords;

/** Resolution dependent HUD scaling factor */
var float HUDScaleX, HUDScaleY;
/** Holds the scaling factor given the current resolution.  This is calculated in PostRender() */
var float ResolutionScale, ResolutionScaleX;

/** The percentage of the view that should be considered safe */
var float SafeRegionPct;
/** Holds the full width and height of the viewport */
var float FullWidth, FullHeight;

/**
 * Perform any value precaching, and set up various safe regions
 *
 * NOTE: NO DRAWING should ever occur in PostRender.  Put all drawing code in DrawHud().
 */
event PostRender()
{
 PawnOwner = Pawn(PlayerOwner.ViewTarget);
 if ( PawnOwner == None )
 {
 PawnOwner = PlayerOwner.Pawn;
 }

 HTPawnOwner = HTPawn(PawnOwner);

 // draw any debug text in real-time
 PlayerOwner.DrawDebugTextList(Canvas,RenderDelta);

 HUDScaleX = Canvas.ClipX/1280;
 HUDScaleY = Canvas.ClipX/1280;

 ResolutionScaleX = Canvas.ClipX/1024;
 ResolutionScale = Canvas.ClipY/768;

 FullWidth = Canvas.ClipX;
 FullHeight = Canvas.ClipY;

 if ( bShowHud )
 {
 DrawHud();
 }

 // let iphone draw any always present overlays
 DrawInputOverlays();
}

/**
 * This is the main drawing pump.  It will determine which hud we need to draw (Game or PostGame).  Any drawing that should occur
 * regardless of the game state should go here.
 */
function DrawHUD()
{
 local float x,y,w,h;

 // Create the safe region
 w = FullWidth * SafeRegionPct;
 X = Canvas.OrgX + (Canvas.ClipX - w) * 0.5;

 // We have some extra logic for figuring out how things should be displayed
 // in split screen.

 h = FullHeight * SafeRegionPct;

 Y = Canvas.OrgY + (Canvas.ClipY - h) * 0.5;

 Canvas.OrgX = X;
 Canvas.OrgY = Y;
 Canvas.ClipX = w;
 Canvas.ClipY = h;
 Canvas.Reset(true);

 // Set up delta time
 RenderDelta = WorldInfo.TimeSeconds - LastHUDRenderTime;
 LastHUDRenderTime = WorldInfo.TimeSeconds;

 PlayerOwner.DrawHud( Self );
 if (bShowGameHUD)
 {
 DrawGameHud();
 }
}

function DrawGameHud()
{
 DisplayLocalMessages();
<div id="_mcePaste"> DisplayConsoleMessages();</div>
<div id="_mcePaste"></div>
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 ( bShowAmmo )
 {
 Weapon = HTWeapon(PawnOwner.Weapon);
 if ( Weapon != none )
 {
 DisplayAmmo(Weapon);
 }
 }
 }
}

function DisplayAmmo(HTWeapon Weapon)
{
 local vector2d POS;
 local string Amount;
 local int AmmoCount;

 // Resolve the position
 POS = ResolveHudPosition(AmmoPosition,AmmoBGCoords.UL,AmmoBGCoords.VL);

 // Figure out if we should be pulsing
 AmmoCount = Weapon.GetAmmoCount();

 // Draw the background
 Canvas.SetPos(POS.X,POS.Y);// - (AmmoBarOffsetY * ResolutionScale));
 Canvas.DrawColorizedTile(HudTexture, AmmoBGCoords.UL * ResolutionScale, AmmoBGCoords.VL * ResolutionScale, AmmoBGCoords.U, AmmoBGCoords.V, AmmoBGCoords.UL, AmmoBGCoords.VL, HudTint);

 // Draw the amount
 Amount = ""$AmmoCount;
 Canvas.DrawColor = WhiteColor;
 Canvas.DrawText(Amount,,3.0f,3.0f);
}

/**
 * Given a default screen position (at 1024x768) this will return the hud position at the current resolution.
 * NOTE: If the default position value is < 0.0f then it will attempt to place the right/bottom face of
 * the "widget" at that offset from the ClipX/Y.
 *
 * @Param Position        The default position (in 1024x768 space)
 * @Param Width            How wide is this "widget" at 1024x768
 * @Param Height        How tall is this "widget" at 1024x768
 *
 * @returns the hud position
 */
function Vector2D ResolveHUDPosition(vector2D Position, float Width, float Height)
{
 local vector2D FinalPos;
 FinalPos.X = (Position.X < 0) ? Canvas.ClipX - (Position.X * ResolutionScale) - (Width * ResolutionScale)  : Position.X * ResolutionScale;
 FinalPos.Y = (Position.Y < 0) ? Canvas.ClipY - (Position.Y * ResolutionScale) - (Height * ResolutionScale) : Position.Y * ResolutionScale;

 return FinalPos;
}

defaultproperties
{
 HudTexture=Texture2D'HTUI.Textures.T_UI_HUD_BaseA'

 HudTint=(R=1.0f,G=1.0f,B=1.0f,A=1.0f)

 AmmoPosition=(X=0,Y=-1)
 AmmoBGCoords=(U=0,UL=76,V=0,VL=126)

 SafeRegionPct = 1.0f

 bShowAmmo=true
}

UDKGame

Update your defaultproperties as shown:

defaultproperties
{
 DefaultPawnClass=class'UDKGame.HTPawn'
 PlayerControllerClass=class'UDKGame.HTPlayerController'
 HUDType=class'UDKGame.HTHUD'

 DefaultMapPrefixes(0)=(Prefix="HT",GameType="UDKGame.TheHuntGame")
}

T_UI_HUD_BaseA.psd

Compressed into a .zip file.

T_UI_HUD_BaseA.zip

Posted March 10, 2010 by Allar in Unreal

Tagged with , , , , , , , , , ,

10 responses to HTHUD: Part 1 – Building The Base Class + Displaying Ammo

Subscribe to comments with RSS.

  1. Pingback: HTHUD: Part 1 – Update: Fix Chat Messages | Allar's AWESOME Blog

  2. Pingback: pdcggpbr

  3. How would some one go about making different ammo display textures for each weapon? With this it seems that every weapon I will create will display the same Cannon ball texture which was obviously made for the Cannon. Will there be any thing on this is the future cause its seems to continue with the tutorials would make it harder later on to change this.

    • This would be fairly easy to implement. You would put these different textures onto the same texture sheet, but you would check for the weapon type and then draw coordinates based on weapon type instead of the default variable.

      Or a more object-oriented approach might be to create a DrawAmmo in the weapon class itself similar to how we draw our crosshairs, and then in the weapon code itself change how the ammo is drawn on a per-weapon basis, then call this function inside the HUD's DisplayAmmo function.

  4. The first one makes the most sense to me since i havn't gotten to drawcrosshair tutorial yet, but i'm completly stumped on what I would have code to check for different weapon types.

  5. Hi …

    Thanks for all your help. Currently i have, the next problem whe compilin in the HUD class

    I am using the UDK-2010-04 please helpme with this problem is very importarnt form me. Thanks

    *************

    Canvas.DrawColorizedTile

    Error, Unrecognized member 'DrawColorizedTile', in class Canvas.

    • This series uses the March build.

      In the April build, DrawColorizedTile was removed and now you have to use DrawTile, works exactly the same way though.

  6. When I compile the HTHUD in FrontEnd, it give this error.

    Error, Unexpected end of file at end of class

    The error is coming from line 180, which is where defaultproperties is located.

    Also I deleted the whole defaultproperties selection and I still got the same error on the same line. Any clues what this might be?

  7. So, how would one go about implementing a custom font? I've looked around at other forum posts, but it's not quite clear and my experiments haven't really come up with any results.

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>