UnrealScript Game Programming Cookbook

(Chris Devlin) #1
Chapter 7

197

How it works...


We start off by defining the size of our screen based on resolution. Our PostRender()
function dynamically scales our HUD based on the default screen size of 1024 x 768. Our
DrawHUD() function gets called during each frame of the main game loop, so it's constantly
updating the screen every time the game itself becomes updated, based on changed
information, such as the pawn's health.


We then added a function to draw our health bar. Really though, UDK calls this a tile with its
DrawTile() function, but we prefer to call it a bar. This same function will be used for our
ammo bar.


Our health bar needs a specific value to be drawn, however, to represent both the maximum
and current health. We define and then use those in the DrawHealthText() function, and
use those parameters in the DrawHealthBar() function.


The bar itself is drawn in the top-left corner of the screen, as defined by CorrectedHudPos().
It's a lot thrown at you at once, so bear with me. In DrawHealthText(), we define our
position with the Vector2D variable POS. POS is actually set to the CorrectedHudPos(),
which takes our HPPosition variable as a Vector2D parameter, then applies the
math within CorrectedHudPos() to draw our text at the given position.


You'll see in our DefaultParameters block that we define what our HPPosition variable
is. Setting the Y value to 1 will place the bar in the top of the screen, while setting it to -1 will
place it in the bottom. Setting the X value to 1 will place our bar against the top of the screen,
and a negative value will do just the opposite! How convenient is that?


Drawing text for a player's health


Sure, it's great to have a bar to represent our health, but what if we want something more
accurate? It's like reading the gas meter on your car, are you really teetering on empty, or
can you push it just a tiny bit longer?


Because I don't condone living so dangerously, I suggest we draw an actual number on screen
so that we know exactly how much health we are left with. We're going to take our existing
functions and make only a few changes to allow this.


Getting ready


Open your IDE and have your TutorialHUD class available. We're going to make
some additions.

Free download pdf