The Source engine is a 3D game engine developed by Valve Corporation. Its unique features include a large degree of modularity and flexibility, an artist-driven, shader-based renderer, accurate lip sync and facial expression technology, and a powerful, efficient and completely network-enabled physics system.

Report article RSS Feed Creating Teams

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.

Posted by bob_gneu on Nov 28th, 2008
Intermediate Server Side Coding.

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\\cfg\autoexec.cfg

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''':

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

cpp code:
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:

cpp code:
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 ) );
Y

Since you want to enable the team menu, '''uncomment'''
 

cpp code:
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.
 

cpp code:
#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.

cpp code:
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
 

cpp code:
// command callbacks

and below it add

 

cpp code:
virtual void OnCommand( const char *command );

Then add the implementation in '''game_controls/teammenu.cpp''':

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

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 /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 /resource/ui/TeamMenu.res:

cpp code:
"Resource/UI/TeamMenu.res"
{
    "team"
    {
        "        "me"        "CTeamMenu"
        "
        "
        "team"
        "
   "xpos"            "0"            " "ypos"            "0"480"      "wide"            "640"
        "tall"            "480"1"       "autoResize"        "0"
        "pinCorner"        "0"
        "visible"        "
        "
    "enabled"        "1"
        "tabPosition"        "0"
    }
    "SysMenu"
    {
        "ControlName"        "Menu"
        "fieldName"        "SysMenu"0"       "xpos"            "0"
        "ypos"            "0"
        "wide"            "ControlName"   "tall"            "            "   "autoResize"        "0"
        "pinCorner"        "0"
        "visible"        "316"       "enabled"        "0"
        "tabPosition"        "0"
    }
    "MapInfoHTML"
    {
        "ControlName"        "HTML"
        "fieldName"            "MapInfoHTML"
        "xpos"                "244"
        "ypos"                "116"
        "22"e"                "316"
        "tall"                "0"6"
        "autoResize"0"       "0"
        "pinCorner"            "0"
        "visibl#TM_Join_Team"
     "textAlignment"  "enabled"            "1"
        "tabPosition"        "0"
    }
    "joinTeam"
    {
        "ControlName"        "Label"
        "fieldName"        "joinTeam"xpos"      "xpos"        "76"
        "ypos"        "22"
        "wide"        "450"
        "tall"        "48"
        "autoResize"        "0"
        "pinCorner"        "0"
        "visible"        "1"
        "enabled"        "        "     "labelText"        " }
    "jointe

        "textAlignment"        "west"
        "dulltext"        "0"
        "brighttext"    "0"
        "font"
        "
  "            "   }
    "mapname"
    {
        "ControlName"        "Label"
        "fieldName"        "mapname"
        "xpos"            "244"
        "ypos"           #TM_Join_Team_2"
  "wide"textAlignment"    "180"
        "tall"        "       "24"
        "0"toResize"        "0"
        "pinCorner"        "0"
        "visible"        "0"
        "enabled"        "1"
        "labelText"        ""
        "textAlignment"        "
        "
      "dulltext"        "0"
        "
        "
"    "1"
    }
    "jointeam2"        "
        "ControlName"        "Button"
        "fieldName"        "jo#TM_Join_Team_3"
   "xpos"            "76"
        "
   "ypos"            "116"
        "wide"            "148"
        "tall"            "20"
        "autoResize"        "0"
        "pinCorner"        "2"
        "visible"        "212"       "enabled"        "1"
        "tabPosition"        "3"        "   "labelText"        ""
        "visib

        "textAlignment"        "west"
        "dulltext"        "0"
      #TM_Auto_Join"
       "0"
        "west"nd"        "jointeam 2"
    }
    "jointeam3"
    {
        "ControlName"        "Button"
        "fieldName"        "jointeam3"ControlName"  "xpos"Button"        "76"
        "ypos"
        "
      "148"
        "wide"            "148"wide"      "tall"            "20"
        "autoResize"        "0"
        "pinCorner"
        "
  "2"
        "visible"        "1"
        "enabled"        "1"
        "tabPo#TM_Join_Team_1"
  "4"
        "        ""        "ulltext"       
        "textAlignment"        "command"       "dulltext"        "0"
        "brighttext"        "0"
        "fieldName"       "jointeam 3"
    }   
    "autojoin"
    {
        "ControlName"wide"     "Button"
        "
   "fieldName"        "autoResize"       "xpos"            "76"
        "        "          "212"
        "wide"            "148"
        "tall" #TM_Cancel"
   "20"
        "autoResize"
        "
  "0"
        "pinCorner"        "2"0"       "visible"        "1"
   

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:
 

cpp code:
CON_COMMAND( showpanel, "Shows a viewport panel " )

but making a team menu command is no big deal:

cpp code:
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 _.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 _english.txt into your mod's resource folder.

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

cpp code:
"lang"
{
"Language" "English"
"Tokens"
{

"TM_Join_Team"     "Join a Team"
"TM_Join_Team_1"Spectate"ctate"
"TM_Join_Team_2"   "Team Combine"
"TM_Join_Team_3"   "Team Rebels"
"TM_Auto_Join"     "TM_Cancel"t"
"TM_Cancel"        "Cancel"

"hl2_AmmoFull"HL2_357Handgun"   "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_SLAM"tgun"        "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"GRAVITY GUN"SBOW"
"HL2_Crowbar_Menu"    "CROWBAR"
"HL2_Grenade_Menu"    "GRENADE"
"HL2_GravityGun_Menu"    "GRAVITY GUN"
"SMG"Pistol_Menu"    "9MM PISTOL"
"
"
2_RPG_Menu"        "RPG"
"HL2_Shotgun_Menu"    "SHOTGUN"
"HL2_SMG1_Menu"        "SMG"
"HL2_SLAM_Menu"%s1    -   %s2 players"
"ScoreBoard_Deathmatch"  "STUNSTICK"

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

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 '''_german''', '''_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 _.html and be located in /resource/maphtml/

Forcing teamplay

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''':

cpp code:
#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 hl2mpgamerules.h

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

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

add these lines:

code:
if (pEdict->IsPlayer() && static_cast(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

cpp code:
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

cpp code:
#include "hl2mp_gamerules.h"

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:

cpp code:
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:

cpp code:
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 _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: "chooseteam"                "Choose Team"

Post comment Comments
RedRogueXIII
RedRogueXIII Nov 29 2008, 9:00pm says:

Awesome, thanks for the tutorials!

+1 vote     reply to comment
bob_gneu
bob_gneu Dec 1 2008, 11:41am says:

Hey no problem. These are actually mirrors of the tutorials that were lost some time back and thrown on the VDC. You can find some others on there as well.

developer.valvesoftware.com/wiki/Main_Page

+1 vote     reply to comment
DJMidKnight
DJMidKnight Nov 13 2009, 4:03pm says:

I keep getting this error
\game_controls\teammenu.cpp(404) : error C2059: syntax error : '('

not sure why I am not a coder and really cant see what is wrong with this code line
engine->ClientCmd( const_cast( command ) );

+1 vote     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Platforms
Windows, PS3, X360, XBOX
Company
Valve
Contact
Send Message
Official Page
Valvesoftware.com
Licence
Commercial
Release Date
Released Oct 31, 2004
Engine Watch
Track this engine
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Engines
Source
Source Commercial Released Oct 31, 2004
Related Groups
Gneu
Gneu Developer & Publisher with 3 members
Valve
Valve Developer & Publisher