Welcome, Guest! Login | Register

How to create a server-client message [Print this Article]
Posted by: Persuter
Date posted: Dec 17 2003
User Rating: 4.5 out of 5.0
Number of views: 2838
Number of comments: 5
Description: This article describes, step-by-step, how to create a message which can be sent from the server-side to the client-side.
This article is intended to help those of you who want to send information from the server to the client but aren't sure exactly what they need to do. This will provide a step-by-step process for creating a server-client message. You should use this basically any time you want to send data to the client-side from the server.

To create a message, you will need the following:

  • A name to identify your message on the server side. We will use "ExampleMsg". This name must be less than or equal to 12 characters. It's best to stay clear of 12 characters altogether.
  • A variable name to hold your message number on the server-side. It should generally start with "gmsg" and end with your message name, so we'll call it "gmsgExampleMsg".
  • The size of your message. If it is of variable size you should use -1. In this case we'll send one short (two bytes), one float (called coord, four bytes), and two chars (one byte apiece), so eight total.


Server side:

Go to line 200 of player.cpp:

 CODE (C++) 
int gmsgExampleMsg = 0;


Go to line 250, same file:

 CODE (C++) 
gmsgExampleMsg = REG_USER_MSG( "ExampleMsg", 8 );


(That 8 is the size of the message. Again, use -1 if you don't know what your size should be.)

You may then, in any file that you need to use the message, put:

 CODE (C++) 
external int gmsgExampleMsg;


Then, in the code, put:

 CODE (C++) 
MESSAGE_BEGIN( MSG_ONE, gmsgExampleMsg, NULL, pev )
    WRITE_SHORT( 1 );
    WRITE_COORD( 0.5 );
    WRITE_CHAR( 'a' );
    WRITE_CHAR( 'b' );
MESSAGE_END();


Note that there are many different usages of MESSAGE_BEGIN. You can send a message to everyone, only a certain pev, to everyone in a pev's PVS or PAS, etc. Experiment, and check how it's done elsewhere in the code.

Client side:

Go to line 300 of hud.cpp:

 CODE (C++) 
HOOK_MESSAGE( ExampleMsg );


Then go to line 110, same file, and create your hooking function:

 CODE (C++) 
int __MsgFunc_ExampleMsg( const char *pszName, int iSize, void *pbuf )
{
    BEGIN_READ( pbuf, iSize );
   
    int a = READ_SHORT();
    int b = READ_COORD();
    int c = READ_CHAR();
    int d = READ_CHAR();
   
    return 1;
}


If you would like your message instead to go directly to a HUD element, such as m_Example (of type CExample), you should NOT do the previous step, but instead go to your class file for the HUD element and type:

 CODE (C++) 
DECLARE_MESSAGE( m_Example, ExampleMsg );


Note that all this does is create the above function like so:

 CODE (C++) 
int __MsgFunc_ExampleMsg( const char *pszName, int iSize, void *pbuf )
{
    return gHUD.m_Example.MsgFunc_ExampleMsg(pszName, iSize, pbuf );
}


You then go to the definition of CExample and put somewhere in the public portion of it:

 CODE (C++) 
int MsgFunc_ExampleMsg( const char*, int, void* );


Then, in CExample's source file, you put:

 CODE (C++) 
int CExample::MsgFunc_ExampleMsg( const char *pszName, int iSize, void *pbuf )
{
    BEGIN_READ( pbuf, iSize );
   
    int a = READ_SHORT();
    int b = READ_COORD();
    int c = READ_CHAR();
    int d = READ_CHAR();
   
    return 1;
}


Note that in a real message-handling function, you would probably do something with the above data. I really don't want to get e-mails saying, "I read in the data, now what?".

So, a simple checklist is as follows:

 QUOTE  
Server side:

player.cpp, line 200: Declare variable
player.cpp, line 240: Register message, assign variable

Client side:

hud.cpp, line 300: Hook message

If message is not to be delivered to a HUD element:

hud.cpp, line 110: Declare and define message-handling function.

Otherwise:

In element's header file, outside of the definition: Declare message
In element's definition: Declare message-handling function
In element's source file: Define message-handling function


That's it! Very simple.

Rate This Article
This article is currently rated: 4.5 out of 5.0 (2 Votes)

You have to register to rate this article.
User Comments Showing comments 1-5

Posted By: jim_the_coder on Dec 19 2003 at 09:33:41
Well explained, and I never was too great at the message-handling function thing. Thanks

Posted By: grOOvy on Jan 23 2004 at 23:39:10
This article got me to understand what was missing in my server-client messaging (alongwith pcjoe's help). Nice article, thanks.

Posted By: jim_the_coder on Apr 15 2004 at 02:45:45
Just thought I'd add that I just found the max message size is 192 bytes :D of course you would never send one that big too often or you'd cause huge lag. Just a comment.

Posted By: Morpheus_ on Sep 09 2004 at 10:13:38
There also appears to be a limit on the size of the name of the message. I'm not sure what this limit is exactly, but if:
gmsgExampleMsg = REG_USER_MSG( "ReallyLongMessageName", 8 );

doesn't work, try:
gmsgExampleMsg = REG_USER_MSG( "ShortName", 8 );

instead.

Posted By: Persuter on Sep 09 2004 at 17:04:20
The very first item in the bulleted list of things you will need:

 QUOTE  
A name to identify your message on the server side. We will use "ExampleMsg". This name must be less than or equal to 12 characters. It's best to stay clear of 12 characters altogether.
Edited by Persuter on Sep 09 2004, 17:08:05


You must register to post a comment. If you have already registered, you must login.
Donate
Any money that is donated will go towards the server costs that I incur for running our server. Don't feel you HAVE to donate, but the link is there if you want to help us out a little. Thanks :)

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 04 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
3 State Zoom For Any Weapon
Half-Life 2 | Coding | Server Side Tutorials
By: Ennuified | Oct 18 2007
 
Storing weapons on ladder
Half-Life 2 | Coding | Snippets
By: cct | Sep 07 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: DarkNight | Aug 28 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: deedok | Aug 20 2007
 
Debugging your half-life 2 mod
Half-Life 2 | Coding | Articles
By: blackshark | Aug 17 2007
 
How to add Ammocrates for missing ammo type and/or create new ammo types
Half-Life 2 | Coding | Server Side Tutorials
By: EoD | Aug 15 2007
 
GameUI
Half-Life 2 | Coding | Client Side Tutorials
By: G.I. Jimbo | May 19 2007
 
Reading and Writing Entire Structures
General | General Coding
By: monokrome | Jan 27 2007
 
VGUI Scope
Half-Life | Coding | Client Side Tutorials
By: Pongles | Jan 19 2007
 
Where do we go from here
General | News
By: SilentSounD | Jan 18 2007
 

Site Info
296 Approved Articless
3 Pending Articles
3940 Registered Members
0 People Online (8 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!