Team Icons

From Valve Developer Community
Revision as of 16:15, 1 September 2015 by Luqgreg (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Deutsch Polski Русский

This tutorial aims on how to accomplish having Hud Element display an image depending on what team you're on. For example, if the player is on one team, display 'this image', but if the player is on the other team, display 'this image'. Here's what you'll achieve: Hud icon example.png


For this tutorial to work, you'll need to have 2 vtf images with configured vmt settings. Example of vmt is below if you need it:

	"$basetexture" "vgui/hud/teamicons/iraqiran"
        "$translucent" "1"
        "$ignorez" "1" 

The HUD Element

Create two files in the client-side project named whatever you like but for this tutorial we'll be using Rteamicon.cpp & Rteamicon.h.

Now open up Rteamicon.cpp and start with putting the header in:

// ======= *********-|RECOIL|-********* =======//
// Programmer: John Stuart                                                    
// ============================================//

#include "hud.h"
#include "cbase.h"
#include "Rteamicon.h"
#include "iclientmode.h"
#include "hud_macros.h"
#include "c_team.h"
#include "vgui_controls/controls.h"
#include "vgui/ISurface.h"
#include "hl2mp_gamerules.h"
#include "tier0/memdbgon.h"

using namespace vgui;


Now that we have the header files we need to create the constructer. Just below the using namespace vgui; paste this:

// Purpose: 
CRTeamIcon::CRTeamIcon( const char *pElementName ) : CHudElement( pElementName ), BaseClass( NULL, "CRTeamIcon" )
   Panel *pParent = g_pClientMode->GetViewport();
   SetParent( pParent );   
   SetVisible( true );
   SetEnabled( true );

   // This defines m_nIraqIcon as "vgui/hud/teamicons/iraqiran" which calls the vtf.. duh.
   m_nIraqIcon = vgui::surface()->CreateNewTextureID();
   vgui::surface()->DrawSetTextureFile( m_nIraqIcon, "vgui/hud/teamicons/iraqiran" , true, false);

   // You get the deal now..
   m_nBritishIcon = vgui::surface()->CreateNewTextureID();
   vgui::surface()->DrawSetTextureFile( m_nBritishIcon, "vgui/hud/teamicons/ukusa" , true, false);


Now that the constructer is created, we can go ahead and create the main function for this to work, for this example we'll be using "Paint Background" because if you're going to use normal "Paint" you're going to have a black transparent background.

// Purpose: If pPlayer (me) is on team Combine, display Iraq icon
// Purpose+: else if i'm on team Rebels display British icon.
void CRTeamIcon::PaintBackground()
   CBasePlayer *pPlayer = UTIL_PlayerByIndex(engine->GetLocalPlayer());

      if(pPlayer->GetTeamNumber() == TEAM_COMBINE)
	   vgui::surface()->DrawSetTexture( m_nIraqIcon );
	   vgui::surface()->DrawTexturedRect(0, 0, GetWide(), GetTall());
	   //DevMsg("Team Combine Icon Displayed\n"); // Spams console hehe :D
	  else if(pPlayer->GetTeamNumber() == TEAM_REBELS)
	   vgui::surface()->DrawSetTexture( m_nBritishIcon );
	   vgui::surface()->DrawTexturedRect(0, 0, GetWide(), GetTall());
	   //DevMsg("Team Rebels Icon Displayed\n"); 



Now we've nearly complete on the actual hud element, to make this all work we've got to finish the header file. Open up Rteamicon.h and place this in there:

// ======= *********-|RECOIL|-********* =======//
// Programmer: John Stuart                                          
// ============================================//

#include "hudelement.h"
#include <vgui_controls/Panel.h>

using namespace vgui;

// Purpose: 
class CRTeamIcon : public CHudElement, public Panel


       CRTeamIcon( const char *pElementName );


       virtual void PaintBackground();

       int m_nIraqIcon;
       int m_nBritishIcon;


The materials

Now that the hud is working, we can move onto getting the actual image on the hud. it's already in there, but it's not defined. So go ahead and open up your "scripts/HudLayout.res" file and place this inside:

		"fieldName" "CRTeamIcon"
	        "xpos" "r62"
		"ypos" "0"
		"wide" "64"
		"tall" "32"
		"visible" "1"
		"enabled" "1"
		"PaintBackgroundType"   "2"

Of course you can place your image anywhere you like and what size you like by editing the xpos,ypos,wide,tall to your settings/needs.

The Result

You should now have a good hud element which displays an icon on your HUD of what team you're on.