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:
Go to line 250, same file:
| | 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:
| | external int gmsgExampleMsg; |
Then, in the code, put:
| | 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:
| | HOOK_MESSAGE( ExampleMsg ); |
Then go to line 110, same file, and create your hooking function:
| | 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:
| | DECLARE_MESSAGE( m_Example, ExampleMsg ); |
Note that all this does is create the above function like so:
| | 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:
| | int MsgFunc_ExampleMsg( const char*, int, void* ); |
Then, in CExample's source file, you put:
| | 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:
| | 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. |
|
User Comments
Showing comments 1-5
Well explained, and I never was too great at the message-handling function thing. Thanks |
|
This article got me to understand what was missing in my server-client messaging (alongwith pcjoe's help). Nice article, thanks. |
|
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. |
|
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. |
|
The very first item in the bulleted list of things you will need:
| | 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.
|
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 :)
|
296 Approved Articless
3 Pending Articles
3940 Registered Members
0 People Online (8 guests)
|
|