Activating the VGUI


In this tutorial I will show you how to activate a VGUI menu from your code. It's pretty simple and straightforward, and it needs no client dll editing.

The key thing is the VGUIMenu message which is already implemented on the client. It is processed in TeamFortressViewport::MsgFunc_VGUIMenu(), which calls into TeamFortressViewport::ShowVGUIMenu() which in turn does the actual job.

To send this message from the server dll, the first thing you need to do is register the message. First, add the following code to player.cpp, around line 191:

int gmsgShowMenu = 0;
int gmsgGeigerRange = 0;

int gmsgVGUIMenu = 0;

This is the identifier (ID) for the message. The user message has to be registered in LinkUserMessages(), which is right below the previous code. Add the following code at the end of the function:

	gmsgVGUIMenu = REG_USER_MSG("VGUIMenu", 1);

The first parameter to REG_USER_MSG() is the message name (you guessed it). The second parameter is the length of the message, which is one byte (the ID of the menu we want to see).

Now whenever you want a VGUI menu to show up you have to send a message with the gmsgVGUIMenu ID. Let's add a function CBasePlayer::ShowVGUIMenu(int iMenuID); to make life simpler. First, add this line to the declaration of CBasePlayer in player.h (around line 90):

	void ShowVGUIMenu(int iMenuID);

Add the code for the function somewhere in player.cpp:

void CBasePlayer::ShowVGUIMenu(int iMenuID)
{
	MESSAGE_BEGIN(MSG_ONE, gmsgVGUIMenu, NULL, pev);
		WRITE_BYTE( iMenuID );
	MESSAGE_END();
}

Now all you have to do is call this function somewhere in the code. To show a menu when a player joins, add a call to pPlayer->ShowVGUIMenu() in your gamerules' InitHUD() function. If you just want to test out VGUI menus I suggest you to add something like this in the if-else-chain in ClientCommand() (in client.cpp):

	else if ( FStrEq(pcmd, "vguimenu" ) )
	{
		if (CMD_ARGC() > 2)
		{
			GetClassPtr((CBasePlayer *)pev)->ShowVGUIMenu(atoi(CMD_ARGV(1)));
		}
	}

Now you just have to type vguimenu xx in the console to activate a menu.

NOTE: The default menu IDs are listed in tf_defs.h. They're defines named MENU_XXX.

 


If you have any questions, suggestions, bugfixes, creative critizism or you want to give me donations for this great tutorial, mail me or post on the Wavelength forums. Go to Wavelength, click on Forums and then on Coding. You have to register yourself to post there, but visiting it regularly is good anyway. There is a forum on HL Programming Planet that I regularly visit, too, but it has less traffic and less people visit it.


You may use this tutorial for any non-commercial mod development, as long as you give me some credit for it.

(c) 2000 by Nicolai Haehnle aka Prefect