Creating Teams: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
 
m (clean up, added orphan tag)
 
(21 intermediate revisions by 15 users not shown)
Line 1: Line 1:
[[Category:Programming]]
{{Orphan|date=January 2024}}
==Info:==
This Tutorial was originally from [http://www.gneu.org/wiki/index.php?title=HL2:_Creating_Teams gneu.org] and moved here to combine all the tutorials into one location.


This tutorial will lay out how to force teamplay in a '''Deathmatch Source SDK'''. If you want to set up teams for a from-scratch SDK, refer to [http://articles.thewavelength.net/667/ this] tutorial.
This tutorial will lay out how to force teamplay in a '''Deathmatch Source SDK'''. If you want to set up teams for a from-scratch SDK, refer to [http://articles.thewavelength.net/667/ this] tutorial.
Line 7: Line 5:
First we will create a team menu, similar to the one seen in Counter-Strike: Source, then we will force HL2MP to be teamplay only.
First we will create a team menu, similar to the one seen in Counter-Strike: Source, then we will force HL2MP to be teamplay only.


{{warning|When using this method, maps that only have info_player_combine and info_player_rebel on them '''must''' be run with mp_teamplay 1, set on the server. This can be done by entering it into your Autoexec.cfg file for the game Steamapps\SourceMods\<modname>\cfg\autoexec.cfg}}


==VGUI Overview==
== VGUI overview==
Before you start working on the VGUI team menu, it is recommended that you look at the VGUI2 Overview. (http://www.valve-erc.com/srcsdk/VGUI2/vgui2.html). You will learn that the base for all VGUI panels (not class-based, but where VGUI-objects are attached) is a viewport. This viewport is created in '''cl_dll/sdk/clientmode_sdk.cpp''' in the method '''ClientModeSDKNormal::InitViewport''':  
Before you start working on the VGUI team menu, it is recommended that you look at the [[VGUI Documentation]]. You will learn that the base for all VGUI panels (not class-based, but where VGUI-objects are attached) is a viewport. This viewport is created in '''cl_dll/sdk/clientmode_sdk.cpp''' in the method '''ClientModeSDKNormal::InitViewport''':


<pre>void ClientModeSDKNormal::InitViewport()
This is just for reference. You do not need to edit anything here.
<source lang=cpp>void ClientModeSDKNormal::InitViewport()
{
{
   m_pViewport = new SDKViewport();
   m_pViewport = new SDKViewport();
   m_pViewport->Start( gameuifuncs, gameeventmanager );
   m_pViewport->Start( gameuifuncs, gameeventmanager );
}</pre>
}</source>
 
This is just for reference. You do not need to edit anything here.
 


==Team Panel and Menu==
== Team panel and menu ==
Let's take a look at the CBaseViewport, which handles all the different Panels used by the mod. It is defined in '''cl_dll/baseviewport.cpp'''.  
Let's take a look at the CBaseViewport, which handles all the different Panels used by the mod. It is defined in '''cl_dll/baseviewport.cpp'''.


This function creates all panels known to this viewport:  
This function creates all panels known to this viewport:  
<pre>void CBaseViewport::CreateDefaultPanels( void )
<source lang=cpp>void CBaseViewport::CreateDefaultPanels( void )
{
{
   AddNewPanel( CreatePanelByName( PANEL_SCOREBOARD ) );
   AddNewPanel( CreatePanelByName( PANEL_SCOREBOARD ) );
Line 33: Line 30:
   // AddNewPanel( CreatePanelByName( PANEL_CLASS ) );
   // AddNewPanel( CreatePanelByName( PANEL_CLASS ) );
   // AddNewPanel( CreatePanelByName( PANEL_BUY ) );
   // AddNewPanel( CreatePanelByName( PANEL_BUY ) );
}</pre>
}</source>
Since you want to enable the team menu, '''uncomment'''  
Since you want to enable the team menu, '''uncomment'''  
  AddNewPanel( CreatePanelByName( PANEL_TEAM ) );  
  AddNewPanel( CreatePanelByName( PANEL_TEAM ) );


The "real" creation is done in the function '''CBaseViewport::CreatePanelByName''', which takes the panelname as string and creates a corresponding class. The panel names are referenced in game_shared/viewport_panel_names.h, e.g.  
The "real" creation is done in the function '''CBaseViewport::CreatePanelByName''', which takes the panelname as string and creates a corresponding class. The panel names are referenced in game_shared/viewport_panel_names.h, e.g.


  #define PANEL_TEAM "team"  
  #define PANEL_TEAM "team"  
'''PANEL_TEAM''' creates the class '''CTeamMenu''', which contains all the functionality of that panel and is implemented in '''game_controls/teammenu.cpp'''. So uncomment that, too.  
'''PANEL_TEAM''' creates the class '''CTeamMenu''', which contains all the functionality of that panel and is implemented in '''game_controls/teammenu.cpp'''. So uncomment that, too (in '''cl_dll/baseviewport.cpp''').


<pre>  else if ( Q_strcmp(PANEL_TEAM, szPanelName) == 0 )
<source lang=cpp>  else if ( Q_strcmp(PANEL_TEAM, szPanelName) == 0 )
   {
   {
     newpanel = new CTeamMenu( this );
     newpanel = new CTeamMenu( this );
   }</pre>
   }</source>
 


The class CTeamMenu is nearly functional; the only thing missing is the function which handles the messages created by the panel. Open '''game_controls/teammenu.h''' to add the declaration of the new function OnCommand to the class. Find  
The class CTeamMenu is nearly functional; the only thing missing is the function which handles the messages created by the panel. Open '''game_controls/teammenu.h''' to add the declaration of the new function OnCommand to the class. Find  
  // command callbacks  
  // command callbacks  
and below it add  
and below it add
 
  virtual void OnCommand( const char *command );
  virtual void OnCommand( const char *command );


Then add the implementation in '''game_controls/teammenu.cpp''':  
Then add the implementation in '''game_controls/teammenu.cpp''':  
<pre>void CTeamMenu::OnCommand( const char *command )
<source lang=cpp>void CTeamMenu::OnCommand( const char *command )
{
{
if ( Q_stricmp( command, "vguicancel" ) )
if ( Q_stricmp( command, "vguicancel" ) )
Line 63: Line 60:
gViewPortInterface->ShowBackGround( false );
gViewPortInterface->ShowBackGround( false );
BaseClass::OnCommand(command);
BaseClass::OnCommand(command);
}</pre>
}</source>


You don't see anything done here with the commands. They are passed to the baseclass where hopefully they will be employed in a useful manner, but that is beyond the scope of this tutorial.  
You don't see anything done here with the commands. They are passed to the baseclass where hopefully they will be employed in a useful manner, but that is beyond the scope of this tutorial.


 
== Menu layout ==
==Menu Layout==
The layout and contents of your panel can be defined solely by a resource file. If you don't need to do customized things (e.g. displaying gamemode-dependent buttons or map information), you won't require any more to complete your menu.
The layout and contents of your panel can be defined solely by a resource file. If you don't need to do customized things (e.g. displaying gamemode-dependent buttons or map information), you won't require any more to complete your menu.  


Most resources are missing in the Source SDK; the team menu resource is no exception. You need to create one yourself (or copy one from a cache file); the standard path would be '''<modname>/resource/ui/Teammenu.res'''. I think the format is self-explanatory, and you'll find an example below.
Most resources are missing in the Source SDK; the team menu resource is no exception. You need to create one yourself (or copy one from a cache file); the standard path would be '''<modname>/resource/ui/Teammenu.res'''. I think the format is self-explanatory, and you'll find an example below.


It defines one panel (team), one label (joinTeam) and five buttons (jointeam1, jointeam2, jointeam3, autojoin, CancelButton). The interesting part in the button-definition is "Command", because this defines, which command the client executes, if he presses that button.  
It defines one panel (team), one label (joinTeam) and five buttons (jointeam1, jointeam2, jointeam3, autojoin, CancelButton). The interesting part in the button-definition is "Command", because this defines, which command the client executes, if he presses that button.


You don't need to edit the file by hand. Valve included a nice tool with which you may change it ingame, as mentioned in VGUI2 Overview (http://www.valve-erc.com/srcsdk/VGUI2/vgui2.html). Just press [SHIFT]+[CTRL]+[ALT]+[B] in the game to open the VGUI Build-Mode editor:
You don't need to edit the file by hand. Valve included a nice tool with which you may change it in-game, as mentioned in [[VGUI Documentation]]. Just press [SHIFT]+[CTRL]+[ALT]+[B] in the game to open the VGUI Build-Mode editor:


Added: replaced example with modified Counter-Strike: Source team menu, which includes HTML map info panel. You can save this example into <modname>/resource/ui/TeamMenu.res:
Added: replaced example with modified Counter-Strike: Source team menu, which includes HTML map info panel. You can save this example into <modname>/resource/ui/TeamMenu.res:
<pre>"Resource/UI/TeamMenu.res"
<source lang=cpp>"Resource/UI/TeamMenu.res"
{
{
"team"
"team"
Line 253: Line 249:
"Command" "vguicancel"
"Command" "vguicancel"
}
}
}</pre>
}</source>


==Menu Command==
== Menu command ==
The only thing missing to show the menu is a command to bring it up on screen. '''cl_dll/baseviewport.cpp''' already implements a command that you can enter from the console to show any panel:
The only thing missing to show the menu is a command to bring it up on screen. '''cl_dll/baseviewport.cpp''' already implements a command that you can enter from the console to show any panel:
  CON_COMMAND( showpanel, "Shows a viewport panel <name>" )
  CON_COMMAND( showpanel, "Shows a viewport panel <name>" )


but making a team menu command is no big deal:  
but making a team menu command is no big deal:  
<pre>CON_COMMAND( chooseteam, "Opens a menu for teamchoose" )
<source lang=cpp>CON_COMMAND( chooseteam, "Opens a menu for teamchoose" )
{
{
  if ( !gViewPortInterface )
  if ( !gViewPortInterface )
   return;
   return;
  gViewPortInterface->ShowPanel( "team", true );
  gViewPortInterface->ShowPanel( "team", true );
}</pre>
}</source>


With this code, if you enter the command "chooseteam" in the console it will show the menu.  
With this code, if you enter the command "chooseteam" in the console it will show the menu.


==Localization==
== Localization ==
If you do not already have a <modname>_<language>.txt in your mod's resource folder, you will need to create one.
If you do not already have a <modname>_<language>.txt in your mod's resource folder, you will need to create one.


Line 275: Line 271:


If you already have a <modname>_english.txt file, you can copy just the team parts from the example below and paste them into your existing file.
If you already have a <modname>_english.txt file, you can copy just the team parts from the example below and paste them into your existing file.
<pre>"lang"  
<source lang=cpp>"lang"  
{  
{  
"Language" "English"  
"Language" "English"  
"Tokens"  
"Tokens"  
{  
{  
//spectate HUD labels
"Cstrike_Spec_Ter_Score" "Team 1:"
"Cstrike_Spec_CT_Score" "Team 2:"


//team menu labels 
"TM_Join_Team"    "Join a Team"
"TM_Join_Team"    "Join a Team"
"TM_Join_Team_1"  "Spectate"
"TM_Join_Team_1"  "Spectate"
Line 331: Line 331:


}  
}  
}</pre>
}</source>


If you take a look back at '''TeamMenu.res''', you'll find labeltexts beginning with a '#'. These labels are replaced by their counterparts in the translation file, only without the '#'. For example '''#TM_Join_Team''' will be replaced with "Join a Team" if your Steam language is set to English. For other languages you need to create additional files, like '''<modname>_german''', '''<modname>_french.txt''', etc.
If you take a look back at '''TeamMenu.res''', you'll find labeltexts beginning with a '#'. These labels are replaced by their counterparts in the translation file, only without the '#'. For example '''#TM_Join_Team''' will be replaced with "Join a Team" if your Steam language is set to English. For other languages you need to create additional files, like '''<modname>_german''', '''<modname>_french.txt''', etc.
Line 337: Line 337:
Using the example TeamMenu.res provided above, you can also display an HTML mapinfo file next to the team menu. Just create a simple HTML file and fill it with map information. It should be named <mapname>_<language>.html and be located in <modname>/resource/maphtml/
Using the example TeamMenu.res provided above, you can also display an HTML mapinfo file next to the team menu. Just create a simple HTML file and fill it with map information. It should be named <mapname>_<language>.html and be located in <modname>/resource/maphtml/


==Forcing Teamplay==
== Forcing teamplay ==
{{warning| In source SDK 2013 there is already a function called HandleCommand_JoinTeam in hl2mp_player.cpp that invalidates most of these steps. Use with caution. }}
We will now force HL2MP to be teamplay only. It will use the existing teams (Combine and Rebels) but you will learn how to change the team names if you wish.
We will now force HL2MP to be teamplay only. It will use the existing teams (Combine and Rebels) but you will learn how to change the team names if you wish.


First ensure that you have 3 teams defined in '''shareddefs.h''':
First ensure that you have 3 teams defined in '''shareddefs.h''':
<pre>#define TEAM_INVALID -1
<source lang=cpp>#define TEAM_INVALID -1
#define TEAM_UNASSIGNED 0
#define TEAM_UNASSIGNED 0
#define TEAM_SPECTATOR 1</pre>
#define TEAM_SPECTATOR 1</source>
<code>TEAM_COMBINE</code> and <code>TEAM_REBELS</code> should '''NOT''' be defined here unless you are modding a from-scratch Source SDK. Here we are modding a deathmatch SDK, so the two teams have already been defined in an enum in hl2mpgamerules.h
<code>TEAM_COMBINE</code> and <code>TEAM_REBELS</code> should '''NOT''' be defined here unless you are modding a from-scratch Source SDK. Here we are modding a deathmatch SDK, so the two teams have already been defined in an enum in hl2mp_gamerules.h


 
Now open '''teamplay_gamerules.cpp''', and in the function '''bool CTeamplayRules::ClientCommand( CBaseEntity *pEdict, const CCommand &args )''' ''below'' the lines
Now open '''teamplay_gamerules.cpp''', and in the function '''bool CTeamplayRules::ClientCommand( const char *pcmd, CBaseEntity *pEdict )''' ''below'' the lines
<source lang=cpp> if( BaseClass::ClientCommand( pEdict, args ) )
<pre> if( BaseClass::ClientCommand(pcmd, pEdict) )
return true;</source>
return true;</pre>
add these lines:
add these lines:
<pre> if (pEdict->IsPlayer() && static_cast<CBasePlayer *>(pEdict)->ClientCommand(pcmd))
<source lang=cpp> if (pEdict->IsPlayer() && static_cast<CBasePlayer *>(pEdict)->ClientCommand(args))
{
{
return true;
return true;
}</pre>
}</source>
 
to make the link between the call of CTeamMenu::OnCommand and CBasePlayer::ClientCommand


to make the link between the call of CTeamMenu::OnCommand and CBasePlayer::ClientCommand


In player.cpp, in the function '''bool CBasePlayer::ClientCommand(const char *cmd)''' we will add  
In player.cpp, in the function '''bool CBasePlayer::ClientCommand(const char *cmd)''' we will add  
<pre>else if ( stricmp( cmd, "jointeam" ) == 0 ) //start jointeam
<source lang=cpp>else if ( stricmp( cmd, "jointeam" ) == 0 ) //start jointeam
{
{
if ( engine->Cmd_Argc() < 2 )
if ( args.ArgC() < 2 )
return true;
return true;


int team = atoi( engine->Cmd_Argv(1) );
int team = atoi( args.Arg(1) );


//dont do anything if you join your own team
//don't do anything if you join your own team
if ( team == GetTeamNumber() )
if ( team == GetTeamNumber() )
return true;
return true;
Line 392: Line 391:


return true;
return true;
} //end jointeam</pre>
} //end jointeam</source>


Notice the test to check if the player belongs to TEAM_UNASSIGNED, this removes the dead body falling on the floor when we connect to a team.
Notice the test to check if the player belongs to TEAM_UNASSIGNED, this removes the dead body falling on the floor when we connect to a team.


Also to make it compile correctly add this include in '''player.cpp''' to the top of the file
Also to make it compile correctly add this include in '''player.cpp''' to the top of the file
<pre>
<source lang=cpp>
#include "hl2mp_gamerules.h"
#include "hl2mp_gamerules.h"
</pre>
</source>
 
If you continue to have compiling errors, add this after '''#include "tier0/memdbgon.h"''' in '''player.cpp''':
<source lang=cpp>
enum
{
TEAM_COMBINE = 2,
TEAM_REBELS,
};
</source>


And finally, to force teamplay to be on all the time (even if mp_teamplay is set to 0), go into '''hl2mp_gamerules.cpp''' and search for the line
And finally, to force teamplay to be on all the time (even if mp_teamplay is set to 0), go into '''hl2mp_gamerules.cpp''' and search for the line
Line 407: Line 413:
  m_bTeamPlayEnabled = true;
  m_bTeamPlayEnabled = true;


 
== Final touches ==
==Final Touches==
We're almost done, but there are first a few more things we need to do. First, we need to force players to join Spectator as soon as they connect to the server.
We're almost done, but there are first a few more things we need to do. First, we need to force players to join Spectator as soon as they connect to the server.


In '''hl2mp_player.cpp''', find function '''void CHL2MP_Player::PickDefaultSpawnTeam( void )''' and comment the entire function.
In '''hl2mp_player.cpp''', find function '''void CHL2MP_Player::PickDefaultSpawnTeam( void )''' and comment the entire function.
Add this in its place:
Add this in its place:
<pre>void CHL2MP_Player::PickDefaultSpawnTeam( void )
<source lang=cpp>void CHL2MP_Player::PickDefaultSpawnTeam( void )
{
{
     if ( GetTeamNumber() == 0 )
     if ( GetTeamNumber() == 0 )
Line 430: Line 434:
         }
         }
     }
     }
}</pre>
}</source>


Now find function '''void CHL2MP_Player::Spawn(void)''' and add this to the end of that function:
Now find function '''void CHL2MP_Player::Spawn(void)''' and add this to the end of that function:
<pre>if ( GetTeamNumber() != TEAM_SPECTATOR )
<source lang=cpp>if ( GetTeamNumber() != TEAM_SPECTATOR )
{
{
StopObserverMode();
StopObserverMode();
Line 441: Line 445:
//if we are a spectator then go into roaming mode
//if we are a spectator then go into roaming mode
StartObserverMode( OBS_MODE_ROAMING );
StartObserverMode( OBS_MODE_ROAMING );
}</pre>
}</source>
 


And lastly we will make the team menu pop up after the MOTD is closed, so the player knows to join a team.
And lastly we will make the team menu pop up after the MOTD is closed, so the player knows to join a team.
Line 451: Line 454:
  pPlayer->ShowViewPortPanel( PANEL_TEAM, true, data );
  pPlayer->ShowViewPortPanel( PANEL_TEAM, true, data );
(This is really just a temporary solution; what it does is bring up the team menu, then brings up the motd over it so when you close the motd the team menu appears to pop up)
(This is really just a temporary solution; what it does is bring up the team menu, then brings up the motd over it so when you close the motd the team menu appears to pop up)


One final thing: If you want to change the names of your teams (Combine and Rebels), you do not need to change the predefined names. If you're modding the Deathmatch SDK and decided to change TEAM_COMBINE and TEAM_REBELS in hl2mp_gamerules.h to something else, you'd have to find all instances of those everywhere in the SDK and change them to match, which is a big hassle and likely to cause problems.
One final thing: If you want to change the names of your teams (Combine and Rebels), you do not need to change the predefined names. If you're modding the Deathmatch SDK and decided to change TEAM_COMBINE and TEAM_REBELS in hl2mp_gamerules.h to something else, you'd have to find all instances of those everywhere in the SDK and change them to match, which is a big hassle and likely to cause problems.
Line 459: Line 461:
Below it you will notice the names for all the defined teams, including Combine and Rebels. Just change those names and your teams will appear with the names on the scoreboard and HUD. To change their names in the Team Menu that we created earlier, you can edit <modname>_english.txt.
Below it you will notice the names for all the defined teams, including Combine and Rebels. Just change those names and your teams will appear with the names on the scoreboard and HUD. To change their names in the Team Menu that we created earlier, you can edit <modname>_english.txt.


----
To add a [[bind|bindable]] key to the key configuration list, just open kb_act.lst in your mod's script directory, and add this to the bottom: <source lang=cpp>"chooseteam" "Choose Team"</source>
 
== Credits ==
This Tutorial was originally from [http://www.gneu.org/wiki/index.php?title=HL2:_Creating_Teams gneu.org] and moved here to combine all the tutorials into one location.


That should be everything you need to do to turn your Deathmatch mod into Teamplay Only. I may have missed something, so feel free to edit it in or discuss it in the talk page. --[[User:Wildfire|Wildfire]] 23:19, 10 December 2006 (EST)
That should be everything you need to do to turn your Deathmatch mod into Teamplay Only. I may have missed something, so feel free to edit it in or discuss it in the talk page. --[[User:Wildfire|Wildfire]] 23:19, 10 December 2006 (EST)
[[Category:Programming]]

Latest revision as of 01:00, 6 January 2024

This tutorial will lay out how to force teamplay in a Deathmatch Source SDK. If you want to set up teams for a from-scratch SDK, refer to this tutorial.

First we will create a team menu, similar to the one seen in Counter-Strike: Source, then we will force HL2MP to be teamplay only.

Warning.pngWarning:When using this method, maps that only have info_player_combine and info_player_rebel on them must be run with mp_teamplay 1, set on the server. This can be done by entering it into your Autoexec.cfg file for the game Steamapps\SourceMods\<modname>\cfg\autoexec.cfg

VGUI overview

Before you start working on the VGUI team menu, it is recommended that you look at the VGUI Documentation. You will learn that the base for all VGUI panels (not class-based, but where VGUI-objects are attached) is a viewport. This viewport is created in cl_dll/sdk/clientmode_sdk.cpp in the method ClientModeSDKNormal::InitViewport:

This is just for reference. You do not need to edit anything here.

void ClientModeSDKNormal::InitViewport()
{
  m_pViewport = new SDKViewport();
  m_pViewport->Start( gameuifuncs, gameeventmanager );
}

Team panel and menu

Let's take a look at the CBaseViewport, which handles all the different Panels used by the mod. It is defined in cl_dll/baseviewport.cpp.

This function creates all panels known to this viewport:

void CBaseViewport::CreateDefaultPanels( void )
{
  AddNewPanel( CreatePanelByName( PANEL_SCOREBOARD ) );
  AddNewPanel( CreatePanelByName( PANEL_INFO ) );
  AddNewPanel( CreatePanelByName( PANEL_SPECGUI ) );
  AddNewPanel( CreatePanelByName( PANEL_SPECMENU ) );
  // AddNewPanel( CreatePanelByName( PANEL_TEAM ) );
  // AddNewPanel( CreatePanelByName( PANEL_CLASS ) );
  // AddNewPanel( CreatePanelByName( PANEL_BUY ) );
}

Since you want to enable the team menu, uncomment

AddNewPanel( CreatePanelByName( PANEL_TEAM ) );

The "real" creation is done in the function CBaseViewport::CreatePanelByName, which takes the panelname as string and creates a corresponding class. The panel names are referenced in game_shared/viewport_panel_names.h, e.g.

#define PANEL_TEAM "team" 

PANEL_TEAM creates the class CTeamMenu, which contains all the functionality of that panel and is implemented in game_controls/teammenu.cpp. So uncomment that, too (in cl_dll/baseviewport.cpp).

  else if ( Q_strcmp(PANEL_TEAM, szPanelName) == 0 )
  {
    newpanel = new CTeamMenu( this );
  }

The class CTeamMenu is nearly functional; the only thing missing is the function which handles the messages created by the panel. Open game_controls/teammenu.h to add the declaration of the new function OnCommand to the class. Find

// command callbacks 

and below it add

virtual void OnCommand( const char *command );

Then add the implementation in game_controls/teammenu.cpp:

void CTeamMenu::OnCommand( const char *command )
{
	if ( Q_stricmp( command, "vguicancel" ) )
	{
		engine->ClientCmd( const_cast<char *>( command ) );
	}
	Close();
	gViewPortInterface->ShowBackGround( false );
	BaseClass::OnCommand(command);
}

You don't see anything done here with the commands. They are passed to the baseclass where hopefully they will be employed in a useful manner, but that is beyond the scope of this tutorial.

Menu layout

The layout and contents of your panel can be defined solely by a resource file. If you don't need to do customized things (e.g. displaying gamemode-dependent buttons or map information), you won't require any more to complete your menu.

Most resources are missing in the Source SDK; the team menu resource is no exception. You need to create one yourself (or copy one from a cache file); the standard path would be <modname>/resource/ui/Teammenu.res. I think the format is self-explanatory, and you'll find an example below.

It defines one panel (team), one label (joinTeam) and five buttons (jointeam1, jointeam2, jointeam3, autojoin, CancelButton). The interesting part in the button-definition is "Command", because this defines, which command the client executes, if he presses that button.

You don't need to edit the file by hand. Valve included a nice tool with which you may change it in-game, as mentioned in VGUI Documentation. Just press [SHIFT]+[CTRL]+[ALT]+[B] in the game to open the VGUI Build-Mode editor:

Added: replaced example with modified Counter-Strike: Source team menu, which includes HTML map info panel. You can save this example into <modname>/resource/ui/TeamMenu.res:

"Resource/UI/TeamMenu.res"
{
	"team"
	{
		"ControlName"		"CTeamMenu"
		"fieldName"		"team"
		"xpos"			"0"
		"ypos"			"0"
		"wide"			"640"
		"tall"			"480"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"SysMenu"
	{
		"ControlName"		"Menu"
		"fieldName"		"SysMenu"
		"xpos"			"0"
		"ypos"			"0"
		"wide"			"64"
		"tall"			"24"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"0"
		"tabPosition"		"0"
	}
	"MapInfoHTML"
	{
		"ControlName"		"HTML"
		"fieldName"			"MapInfoHTML"
		"xpos"				"244"
		"ypos"				"116"
		"wide"				"316"
		"tall"				"286"
		"autoResize"		"0"
		"pinCorner"			"0"
		"visible"			"1"
		"enabled"			"1"
		"tabPosition"		"0"
	}
	"joinTeam"
	{
		"ControlName"		"Label"
		"fieldName"		"joinTeam"
		"xpos"		"76"
		"ypos"		"22"
		"wide"		"450"
		"tall"		"48"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"1"
		"enabled"		"1"
		"labelText"		"#TM_Join_Team"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"	"0"
		"font"		"MenuTitle"
	}
	"mapname"
	{
		"ControlName"		"Label"
		"fieldName"		"mapname"
		"xpos"			"244"
		"ypos"			"72"
		"wide"			"180"
		"tall"			"24"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"labelText"		""
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"	"1"
	}
	"jointeam2"
	{
		"ControlName"		"Button"
		"fieldName"		"jointeam2"
		"xpos"			"76"
		"ypos"			"116"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"3"
		"labelText"		"#TM_Join_Team_2"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 2"
	}
	"jointeam3"
	{
		"ControlName"		"Button"
		"fieldName"		"jointeam3"
		"xpos"			"76"
		"ypos"			"148"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"4"
		"labelText"		"#TM_Join_Team_3"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 3"
	}	
	"autojoin"
	{
		"ControlName"		"Button"
		"fieldName"		"autojoin"
		"xpos"			"76"
		"ypos"			"212"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"1"
		"labelText"		"#TM_Auto_Join"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 0"
		"Default"		"1"
	}
	"jointeam1"
	{
		"ControlName"		"Button"
		"fieldName"		"jointeam1"
		"xpos"			"76"
		"ypos"			"244"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"2"
		"labelText"		"#TM_Join_Team_1"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 1"
	}
	"CancelButton"
	{
		"ControlName"		"Button"
		"fieldName"		"CancelButton"
		"xpos"			"76"
		"ypos"			"276"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
		"labelText"		"#TM_Cancel"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"Command"		"vguicancel"
	}
}

Menu command

The only thing missing to show the menu is a command to bring it up on screen. cl_dll/baseviewport.cpp already implements a command that you can enter from the console to show any panel:

CON_COMMAND( showpanel, "Shows a viewport panel <name>" )

but making a team menu command is no big deal:

CON_COMMAND( chooseteam, "Opens a menu for teamchoose" )
{
 if ( !gViewPortInterface )
  return;
 gViewPortInterface->ShowPanel( "team", true );
}

With this code, if you enter the command "chooseteam" in the console it will show the menu.

Localization

If you do not already have a <modname>_<language>.txt in your mod's resource folder, you will need to create one.

The example below has been extracted from the HL2MP cache and modified to include team names from the team menu. Save it as <modname>_english.txt into your mod's resource folder.

If you already have a <modname>_english.txt file, you can copy just the team parts from the example below and paste them into your existing file.

"lang" 
{ 
"Language" "English" 
"Tokens" 
{ 
//spectate HUD labels
"Cstrike_Spec_Ter_Score"		"Team 1:"
"Cstrike_Spec_CT_Score"			"Team 2:"

//team menu labels  
"TM_Join_Team"     "Join a Team"
"TM_Join_Team_1"   "Spectate"
"TM_Join_Team_2"   "Team Combine"
"TM_Join_Team_3"   "Team Rebels"
"TM_Auto_Join"     "Auto-Select"
"TM_Cancel"        "Cancel"

"hl2_AmmoFull"			"FULL"

"HL2_357Handgun"	".357 MAGNUM"
"HL2_Pulse_Rifle"	"OVERWATCH STANDARD ISSUE\n(PULSE-RIFLE)"
"HL2_Bugbait"		"PHEROPOD\n(BUGBAIT)"
"HL2_Crossbow"		"CROSSBOW"
"HL2_Crowbar"		"CROWBAR"
"HL2_Grenade"		"GRENADE"
"HL2_GravityGun"	"ZERO-POINT ENERGY GUN\n(GRAVITY GUN)"
"HL2_Pistol"		"9MM PISTOL"
"HL2_RPG"		"RPG\n(ROCKET PROPELLED GRENADE)"
"HL2_Shotgun"		"SHOTGUN"
"HL2_SMG1"		"SMG\n(SUBMACHINE GUN)"
"HL2_SLAM"		"S.L.A.M\n(Selectable Lightweight Attack Munition)"
"HL2_StunBaton"		"STUNSTICK"

"HL2_357Handgun_Menu"	".357 MAGNUM"
"HL2_Pulse_Rifle_Menu"	"PULSE RIFLE"
"HL2_Crossbow_Menu"	"CROSSBOW"
"HL2_Crowbar_Menu"	"CROWBAR"
"HL2_Grenade_Menu"	"GRENADE"
"HL2_GravityGun_Menu"	"GRAVITY GUN"
"HL2_Pistol_Menu"	"9MM PISTOL"
"HL2_RPG_Menu"		"RPG"
"HL2_Shotgun_Menu"	"SHOTGUN"
"HL2_SMG1_Menu"		"SMG"
"HL2_SLAM_Menu"		"S.L.A.M"
"HL2_StunBaton_Menu"	"STUNSTICK"

"ScoreBoard_Player"		"%s1    -   %s2 player"
"ScoreBoard_Players"		"%s1    -   %s2 players"
"ScoreBoard_Deathmatch"		"Deathmatch"
"ScoreBoard_TeamDeathmatch"	"Team Deathmatch"
"Playerid_sameteam"		"Friend: %s1 Health: %s2"
"Playerid_diffteam"		"Enemy: %s1"
"Playerid_noteam"		"%s1 Health:%s2"
"Team"				"Team %s1"

"Game_connected"			"%s1 connected"
"Game_disconnected"			"%s1 has left the game"
"Cannot_Be_Spectator"			"This server does not allow spectating"

} 
}

If you take a look back at TeamMenu.res, you'll find labeltexts beginning with a '#'. These labels are replaced by their counterparts in the translation file, only without the '#'. For example #TM_Join_Team will be replaced with "Join a Team" if your Steam language is set to English. For other languages you need to create additional files, like <modname>_german, <modname>_french.txt, etc.

Using the example TeamMenu.res provided above, you can also display an HTML mapinfo file next to the team menu. Just create a simple HTML file and fill it with map information. It should be named <mapname>_<language>.html and be located in <modname>/resource/maphtml/

Forcing teamplay

Warning.pngWarning: In source SDK 2013 there is already a function called HandleCommand_JoinTeam in hl2mp_player.cpp that invalidates most of these steps. Use with caution.

We will now force HL2MP to be teamplay only. It will use the existing teams (Combine and Rebels) but you will learn how to change the team names if you wish.

First ensure that you have 3 teams defined in shareddefs.h:

#define TEAM_INVALID			-1
#define TEAM_UNASSIGNED			0
#define TEAM_SPECTATOR			1

TEAM_COMBINE and TEAM_REBELS should NOT be defined here unless you are modding a from-scratch Source SDK. Here we are modding a deathmatch SDK, so the two teams have already been defined in an enum in hl2mp_gamerules.h

Now open teamplay_gamerules.cpp, and in the function bool CTeamplayRules::ClientCommand( CBaseEntity *pEdict, const CCommand &args ) below the lines

	if( BaseClass::ClientCommand( pEdict, args ) )
		return true;

add these lines:

	if (pEdict->IsPlayer() && static_cast<CBasePlayer *>(pEdict)->ClientCommand(args))
	{
		return true;
	}

to make the link between the call of CTeamMenu::OnCommand and CBasePlayer::ClientCommand

In player.cpp, in the function bool CBasePlayer::ClientCommand(const char *cmd) we will add

else if ( stricmp( cmd, "jointeam" ) == 0 ) //start jointeam
{
	if ( args.ArgC() < 2 )
		return true;

	int team = atoi( args.Arg(1) );

	//don't do anything if you join your own team
	if ( team == GetTeamNumber() )
		return true;

	//auto assign if you join team 0
	if ( team == 0 )
	{
		if ( g_Teams[TEAM_COMBINE]->GetNumPlayers() > g_Teams[TEAM_REBELS]->GetNumPlayers() )
			team = TEAM_REBELS;
		else
			team = TEAM_COMBINE;
	}

	if ( !IsDead() )
	{
		if ( GetTeamNumber() != TEAM_UNASSIGNED )
		{
			CommitSuicide();
			IncrementFragCount(1); //adds 1 frag to balance out the 1 subtracted for killing yourself
		}
	}

	ChangeTeam( team );

	return true;
} //end jointeam

Notice the test to check if the player belongs to TEAM_UNASSIGNED, this removes the dead body falling on the floor when we connect to a team.

Also to make it compile correctly add this include in player.cpp to the top of the file

#include "hl2mp_gamerules.h"

If you continue to have compiling errors, add this after #include "tier0/memdbgon.h" in player.cpp:

enum
{
	TEAM_COMBINE = 2,
	TEAM_REBELS,
};

And finally, to force teamplay to be on all the time (even if mp_teamplay is set to 0), go into hl2mp_gamerules.cpp and search for the line

m_bTeamPlayEnabled = teamplay.GetBool();

and replace it with

m_bTeamPlayEnabled = true;

Final touches

We're almost done, but there are first a few more things we need to do. First, we need to force players to join Spectator as soon as they connect to the server.

In hl2mp_player.cpp, find function void CHL2MP_Player::PickDefaultSpawnTeam( void ) and comment the entire function. Add this in its place:

void CHL2MP_Player::PickDefaultSpawnTeam( void )
{
    if ( GetTeamNumber() == 0 )
    {
        if ( HL2MPRules()->IsTeamplay() == false )
        {
            if ( GetModelPtr() == NULL )
            {
                ChangeTeam( TEAM_UNASSIGNED );
            }
        }
        else
        {
            ChangeTeam( TEAM_SPECTATOR );
        }
    }
}

Now find function void CHL2MP_Player::Spawn(void) and add this to the end of that function:

if ( GetTeamNumber() != TEAM_SPECTATOR )
{
	StopObserverMode();
}
else
{
	//if we are a spectator then go into roaming mode
	StartObserverMode( OBS_MODE_ROAMING );
}

And lastly we will make the team menu pop up after the MOTD is closed, so the player knows to join a team.

In hl2mp_client.cpp find the line

pPlayer->ShowViewPortPanel( PANEL_INFO, true, data );

and add this ABOVE it:

pPlayer->ShowViewPortPanel( PANEL_TEAM, true, data );

(This is really just a temporary solution; what it does is bring up the team menu, then brings up the motd over it so when you close the motd the team menu appears to pop up)

One final thing: If you want to change the names of your teams (Combine and Rebels), you do not need to change the predefined names. If you're modding the Deathmatch SDK and decided to change TEAM_COMBINE and TEAM_REBELS in hl2mp_gamerules.h to something else, you'd have to find all instances of those everywhere in the SDK and change them to match, which is a big hassle and likely to cause problems.

You can change the visual name appearance of your teams very easily in the file hl2mp_gamerules.cpp by searching for the line

char *sTeamNames[] =

Below it you will notice the names for all the defined teams, including Combine and Rebels. Just change those names and your teams will appear with the names on the scoreboard and HUD. To change their names in the Team Menu that we created earlier, you can edit <modname>_english.txt.

To add a bindable key to the key configuration list, just open kb_act.lst in your mod's script directory, and add this to the bottom:

"chooseteam"				"Choose Team"

Credits

This Tutorial was originally from gneu.org and moved here to combine all the tutorials into one location.

That should be everything you need to do to turn your Deathmatch mod into Teamplay Only. I may have missed something, so feel free to edit it in or discuss it in the talk page. --Wildfire 23:19, 10 December 2006 (EST)