Wayback Machine
Jul AUG SEP
Previous capture 8 Next capture
2006 2007 2008
2 captures
8 Aug 07 - 15 Sep 08
sparklines
Close Help

Mazor's VGUI HUD Tutorial

Part III

This tutorial will explain how to add the current name of the weapon the player is using to the HUD we created earlier. This SHOULD be a pretty simple tutorial. But, its quite tedious for doing it for every single weapon. So, I'll do it for the glock, and that will be it. You should be able to figure out how to do it for the other weapons. I hope to go God you have previous C++ knowledge. If so, continue, if not, LEARN IT!

Comments are in blue

Mazor's code is in green

Mazor's old code is in yellow

Valve's code is in red

Files are in orange

Wow, this is gonna be quite the colorful tutorial =)

Open hud.h and scroll to the CHudAmmo class definition.

In that class definition, find WEAPON *m_pWeapon; that should be in the private section in that class.

Move that line into the PUBLIC section so that we can access it from our HUD.

Now, here is where we make our label in our new HUD. First we have to make all the new casts and declarations and junk, so in vgui_newhud.h find this set of code:

// health

CTransparentPanel *m_pStatsPanel;

Label *m_pPlayerHealth;

char m_szPlayerHealth[512];

// health

below that, lets add some more new stuff:

// weapon name

CTransparentPanel *m_pWeaponPanel;

Label *m_pWeaponName;

char m_szWeaponName[512];

//weapon name

Ok, now you have your new variables. Now open up vgui_ScorePanel.cpp and find

// zero out the values

strcpy(m_szPlayerHealth, "");

right after that, put in:

// weapon name

strcpy(m_szWeaponName,"");

// weapon name

now, in that same area, locate:

m_pPlayerHealth->setBgColor( 0,0,0, 255);
m_pPlayerHealth->setContentAlignment( vgui::Label::a_west );
m_pPlayerHealth->setParent(m_pStatsPanel);

// health

then add

// create the weapon panel

m_pWeaponPanel = new CTransparentPanel(100, ScreenWidth * 0.8, ScreenHeight * 0.7, ScreenWidth * 0.2, ScreenHeight * 0.1);
m_pWeaponPanel->setParent(this);
m_pWeaponPanel->setBorder( new LineBorder( Color(0,0,255,0) ) );

// create the weapon panel

I kinda guestimated those values, so you may have to do a little messin around with the height and width.

Now, we create the label:

so, find:

m_pPlayerHealth->setBgColor( 0,0,0, 255);
m_pPlayerHealth->setContentAlignment( vgui::Label::a_west );
m_pPlayerHealth->setParent(m_pStatsPanel);

// health

reminder, we are sill in vgui_ScorePanel.cpp

below that block of code, add this:

// weapon name

m_pWeaponName = new Label( "", XRES(5), YRES(5) , XRES(125), YRES(18) );
m_pWeaponName->setFont( pSchemes->getFont(hTitleScheme) );
m_pWeaponName->setBgColor( 0,0,0, 255);
m_pWeaponName->setContentAlignment( vgui::Label::a_east );
m_pWeaponName->setParent(m_pWeaponPanel);

// weapon name

Ok, now we are done with the main part, now all we have to do is keep updating the values, so, find the NewHUD::Update() function.

In that function, locate:

// health

// copy over the health into the variable

sprintf(m_szPlayerHealth, "Health: %d", gHUD.m_Health.m_iHealth);

// update the value to the label

m_pPlayerHealth->setText(m_szPlayerHealth);

// health

After all that, put in this:

// weapon name

// copy over the weapon name into the weapon name variable

sprintf(m_szWeaponName. "%s", gHUD.m_Ammo.m_pWeapon->szName);

// update the label

if (!strcmp(m_szGunName, "weapon_9mmhandgun" ) )
{
m_pWeaponName->setText("Glock");
}

// weapon name

Now, when the server sends the message to the client about what weapon they are carrying, it isn't a pretty looking name. It looks like "weapon_9mmhandgun" for the glock. So, I chose to change this before the weapon label gets displayed. Hence the strcmp. You should be able to add a whole bunch more strcmp's for each weapon in your mod.

Ok, that SHOULD do it. I'm not too sure though, if you find problems, let me know PLEASE. Contact me on ICQ: 114771351 or through e-mail: mailto:mazor@spudgunmod.net?SUBJECT=VGUI Tutorials

Thank you!