HTWeapon: Part 2 – DrawWeaponCrosshair()   12 comments

Important: I didn’t notice this until after I posted this but my stuttering is pretty bad in this video. Just a heads up. I will re-do this in the future.

Video Version

Subject: HTWeapon: Part 2 – DrawWeaponCrosshair()
Skill Level: Beginner
Run-Time: 30 minutes
Author: Michael Allar
Notes: Going over how to have our weapons handle the drawing of our crosshair.

Streaming:     720×480 1920×1080

Download:     Low-Res (111MB) Hi-Res (160MB)

Written Version

Subject: HTWeapon: Part 2 – DrawWeaponCrosshair()
Skill Level: Beginner
Author: Michael Allar
Notes: Going over how to have our weapons handle the drawing of our crosshair.

Sorry about the indentation, it seems like my indentation will not survive copy paste… I will go back and fix the indentation later.

HTWeapon

The variables we added to HTWeapon

/** HUD Stuff */
var Texture CrosshairImage;
var UIRoot.TextureCoordinates CrosshairCoordinates;
The DrawWeaponCrosshair() function.
/**
* Draw the Crosshairs
*/
simulated function DrawWeaponCrosshair( Hud HUD )
{
local vector2d CrosshairSize;
local float x,y, ScreenX, ScreenY;
local HTHUD    H;

H = HTHUD(HUD);
if ( H == None )
return;

CrosshairSize.Y = CrosshairCoordinates.VL * H.Canvas.ClipY/720;
CrosshairSize.X = CrosshairSize.Y * ( CrosshairCoordinates.UL / CrosshairCoordinates.VL );

X = H.Canvas.ClipX * 0.5;
Y = H.Canvas.ClipY * 0.5;
ScreenX = X - (CrosshairSize.X * 0.5);
ScreenY = Y - (CrosshairSize.Y * 0.5);
if ( CrosshairImage != none )
{
// crosshair drop shadow
H.Canvas.DrawColor = H.CrosshairShadowColor;
H.Canvas.SetPos( ScreenX+1, ScreenY+1 );
H.Canvas.DrawTile(CrosshairImage,CrosshairSize.X, CrosshairSize.Y, CrossHairCoordinates.U, CrossHairCoordinates.V, CrossHairCoordinates.UL,CrossHairCoordinates.VL);

H.Canvas.DrawColor = H.CrosshairColor;
H.Canvas.SetPos(ScreenX, ScreenY);
H.Canvas.DrawTile(CrosshairImage,CrosshairSize.X, CrosshairSize.Y, CrossHairCoordinates.U, CrossHairCoordinates.V, CrossHairCoordinates.UL,CrossHairCoordinates.VL);
}
}

HTHUD

We’ve added a boolean bShowCrosshair to our declared variables.

var bool bShowAmmo, bShowCrosshair;

Also reworked our DrawLivingHUD so that its a bit more streamlined and logic flows better. Also implements bShowCrosshair along with calling our DrawWeaponCrosshair() function.

/**
* 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 )
{
Weapon = HTWeapon(PawnOwner.Weapon);
if ( Weapon != none )
{
if ( bShowAmmo )
{
DisplayAmmo(Weapon);
}
if (bShowCrosshair)
{
Weapon.DrawWeaponCrosshair(self);
}
}
}
}

UDKGame Classes At This Point

Click here to download the source files I’ve had after completing this tutorial.

Posted March 19, 2010 by Allar in Unreal

Tagged with , , , , , , , ,

12 responses to HTWeapon: Part 2 – DrawWeaponCrosshair()

Subscribe to comments with RSS.

  1. hi ,
    i just want youto thank for your great work!
    excellent tutorials
    nilotpal

  2. Hey what would be the pros and cons of also putting our draw ammo code in each of our weapons saying that each weapon has a different way of showing there ammo. For instance I plan on having a picture of the gun and then its bullets on my hud. Would I be better off changing my Ammo draw code to be in each individual weapon class? Or better yet just putit in my base weapon class and then change each texture for my different guns?

    • In the projects I'm working on I've just moved my draw ammo code to our weapons, as it was easier to do custom ammo drawing that way.

      Pros: Easily make a different ammo style per weapon without doing weapon type checks in HUD
      Cons: You have to define a draw ammo function in all your weapons.

      For my approach, I created a boolean in my hud called bWeaponDrawsAmmo and will call the weapon's DrawAmmo function instead of the HUD's DrawAmmo function if set to true. Its like a hybrid solution I suppose.

  3. im getting a problem with
    H.Canvas.DrawColor = H.CrosshairShadowColor;

    “error, Unrecognised member ‘crosshairshadowcolor’ in class MyHUD”

    • It means that the compiler cannot find this member in that specific place you specified… Can be missspell or you forgot to define it.

  4. Hi, I had a strange problem with the lightning that seemed to ignore the gun at all.

    I went through the code for the weapon and the pawn and the problem appeared to be in that the weapon first person skeletal mesh component used the pawns LE that had none reference.

    The solution was easy — there were two lines of code in the HTPawn default properties missing (that were in the UTPawn where the code was originally taken from):
    Defaultproperties
    {
    Components.Add(MyLightEnvironment)
    LightEnvironment=MyLightEnvironment
    }

    I was just wondering why anyone else didn’t come across the same problem as I did and why this code worked in the Allar’s video well… This problem appears for example in the code enclosed to this article.

    Please write if the code worked on your end or if you had similar problems.

  5. Is there any reason why my crosshair is stuck up at the uppermost left / top corner of the screen?

    These tutorials are great by the way! Leading me into the UDK step by step, Thanks! :)

  6. Hello,

    I encountered a problem, for my HTHUD the engine was able to read my HudTexture=Texture2D'Rubicon_test_map.textureFolder.T_UI_HUD_BaseA4cs5'

    but for the PawnGun the engine does no projects the crosshair and wasn't able to read the CrosshairImage=Texture2D'Rubicon_test_map.textureFolder.T_UI_HUD_BaseA4cs5'

    your help will be a big help for us. thank you

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>