.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > SDK Programming discussions > Half-Life 2 SDK
Half-Life 2 SDK For developments focused around the Half-Life 2 engine Half-Life 2

Reply
 
Thread Tools
HOWTO: display HUD messages with your server plugin
Old
  (#1)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default HOWTO: display HUD messages with your server plugin - 01-02-2005

Thanks to some tips I found at the SourceMod.net forums, I have devised out a simple way to print HUD messages in server plugins.

You certainly know how to print messages to console (using Msg()), and how to print hint messages and dialog boxes (thanks to the examples provided by Valve in the empty server plugin .cpp file), but printing real HUD messages using SayText network messages like in HL1 was problematic:

1. Because Valve doesn't provide server plugins with an interface for knowing about user message IDs and names, and
2. Because in order to send a user message you need to include a recipient filter class and there is no simple way to include the SDK one in a plugin.

To solve 1, the SourceMod guys (I think it was VanceLorgin) found out that the SayText user message ID is 3. Since the code in the SDK that deals with user messages is located in "game_shared", we can without many risk assume that this value will remain constant between most if not all game DLLs.

To solve 2, you'll have to write your own recipient filter class. It doesn't need to be very complicated. A "recipient filter" is a structure that tells the Source engine about the recipients of a particular network message. This is usually a list of entity indices (such as player indices).

Here's the one I made:
Code:
// helper class
class SimpleRecipientFilter: public IRecipientFilter
{
public:
   SimpleRecipientFilter (void) { recipient_count = 0; };
   ~SimpleRecipientFilter (void) { };
   virtual bool IsReliable (void) const { return (false); };
   virtual bool IsInitMessage (void) const { return (false); };
   virtual int GetRecipientCount (void) const { return (recipient_count); };
   virtual int GetRecipientIndex (int slot) const { return (((slot < 0) || (slot >= recipient_count)) ? -1 : recipients[slot]); };
   void Flush (void) { recipient_count = 0; };
   void AddPlayer (int player_index)
   {
	  if (recipient_count > 255)
		 return;
 
	  recipients[recipient_count] = player_index;
	  recipient_count++;
   }
 
private:
   int recipients[256]; // NOTE: this filter cannot be used for more than 256 recipients
   int recipient_count;
};
You need to copy and paste it somewhere in your code, either in a .h file you create, or directly inside a .cpp file.

As you can see, this class is derived from IRecipientFilter. As a result, don't forget to #include "IRecipientFilter.h" in your project, because that's where this parent class is defined.

Now, you also need to know how to send network messages. Contrarily to HL1, in Source the network message can be a stream of bits, and not bytes. This is obviously to save on bandwidth. All the data must pass through a serialization and deserialization interface when they are sent and received. This is why network messages have their own class for writing and reading bits and bytes: bf_read and bf_write. The WriteString(), WriteByte() etc. functions you know from the HL1 enginefuncs have been moved into the bf_write class, which is the one we are interested in here (since we want to WRITE a SayText message). To use them, you must #include "bitbuf.h" in your project files.

We are ready to send our message now.
Code:
void HUD_printf (char *string, edict_t *pPlayerEdict)
{
   // this function sends "string" over the network for display on pPlayerEdict's HUD
 
   bf_write *netmsg; // our network message object
   SimpleRecipientFilter recipient_filter; // the corresponding recipient filter
 
   // BUG FIX: the Source engine "eats" the last character from HUD SayText messages :(
   strcat (string, "\n"); // so add an arbitrary carriage return to the string :)
 
   // pPlayerEdict is a pointer to the edict of a player for which you want this HUD message
   // to be displayed. There can be several recipients in the filter, don't forget.
   recipient_filter.AddPlayer (engine->IndexOfEdict (pPlayerEdict));
 
   // HACK: Valve doesn't permit server plugins to know about net messages yet, we have
   // to figure out ourselves the message numbers, which is not nice. Here "SayText" is 3.
   // Hopefully this network message code being in game_shared, we can assume many game DLLs
   // will keep the same message number. Until Valve gives us another interface...
   netmsg = engine->UserMessageBegin (&recipient_filter, 3);
   netmsg->WriteByte (0); // index of the player this message comes from. "0" means the server.
   netmsg->WriteString (string); // the HUD message itself
   netmsg->WriteByte (1); // I don't know yet the purpose of this byte, it can be 1 or 0
   engine->MessageEnd ();
 
   return; // et voilą
}
Here it is. The only thing really inelegant here, is the hardcoded network message number. But perhaps in the future, Valve will update the SDK so that we can load a true network message interface from the gameServerFactory. With such an interface we will then be able to query network message IDs from their names, like in a game DLL.

*edit* fixed typo, added to wiki



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."

Last edited by Pierre-Marie Baty; 01-02-2005 at 10:58..
  
Reply With Quote
Re: HOWTO: display HUD messages with your server plugin
Old
  (#2)
BotUser
Member
 
Status: Offline
Posts: 39
Join Date: Jun 2004
Default Re: HOWTO: display HUD messages with your server plugin - 01-02-2005

good one, PMB.

I was looking for something like this.
  
Reply With Quote
Re: HOWTO: display HUD messages with your server plugin
Old
  (#3)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: HOWTO: display HUD messages with your server plugin - 01-02-2005

the wiki version is much more documented

http://wiki.bots-united.com/index.ph...er%20plugin%3F

please contribute to our wiki as soon as you find something useful guys



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: HOWTO: display HUD messages with your server plugin
Old
  (#4)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Re: HOWTO: display HUD messages with your server plugin - 01-02-2005

pmb, you do realize you code with C++ now do ya?

very nicely done!


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me
  
Reply With Quote
Re: HOWTO: display HUD messages with your server plugin
Old
  (#5)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: HOWTO: display HUD messages with your server plugin - 01-02-2005

Quote:
pmb, you do realize you code with C++ now do ya?
yes! to my shame... but I can't do it otherwise



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com