Motd (Metamod:Source)

From AlliedModders Wiki
Revision as of 16:19, 21 March 2006 by L. Duke (talk | contribs) (Showing a MOTD Panel)
Jump to: navigation, search

Message of the Day (MOTD)

Thanks to coldfyr3 for his example of displaying info in a MOTD panel. At the end of this page, I've built on his technique to send messages greater than 255 characters in length.

Showing a MOTD Panel

To show a MOTD, you'll need to add this code to your project. You'll also need to include an MRecipientFilter class. You may want to make it part of a class instance in your program, but this will work as an example.

First add this to AllPluginsLoaded:

mVGUIMenu = UserMessageIndex("VGUIMenu");

Then add this to your project:

void ShowMOTD(edict_t *pEdict, const char* lpcMsg, const char* lpcTitle, int iType, const char* lpcCmd)
{
   KeyValues* kv = new KeyValues("data");     
   kv->SetString("title", lpcTitle);   // info panel title
 
   char Type[8];
   Q_snprintf(Type, sizeof(Type), "%d", iType);
   kv->SetString("type", Type);
                              //   TYPE_TEXT = 0,   // just display this plain text
                              //   TYPE_INDEX,      // lookup text & title in stringtable
                              //   TYPE_URL,      // show this URL
                              //   TYPE_FILE,      // show this local file
 
   kv->SetString("msg", lpcMsg);      // use this stringtable entry
 
   if(lpcCmd && *lpcCmd)
   {
      kv->SetString("cmd", lpcCmd);// exec this command if panel closed
   }
 
   ShowViewPortPanel(pEdict, "info", true, kv);
 
   kv->deleteThis();
}
 
// called from ShowMOTD
void ShowViewPortPanel(edict_t* pEdict, const char* name, bool bShow, KeyValues* data)
{
	bf_write *netmsg; // our network message object
    MRecipientFilter recipient_filter; // the corresponding recipient filter
 
    // 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 (m_Engine->IndexOfEdict (pEdict));
 
   int count = 0;
   KeyValues *subkey = NULL;
 
   if(data)
   {
      subkey = data->GetFirstSubKey();     
      while(subkey)
     {
         count++;     
         subkey = subkey->GetNextKey();
      }
 
      subkey = data->GetFirstSubKey(); // reset
   }
 
 
   netmsg = m_Engine->UserMessageBegin (&recipient_filter, mVGUIMenu);
 
   netmsg->WriteString (name); // the HUD message itself
   netmsg->WriteByte (bShow ? 1 : 0); // index of the player this message comes from. "0" means the server.
    netmsg->WriteByte (count); // I don't know yet the purpose of this byte, it can be 1 or 0
 
   // write additional data (be carefull not more than 192 bytes!)
   while(subkey)
   {
 
      netmsg->WriteString (subkey->GetName()); // the HUD message itself
      netmsg->WriteString (subkey->GetString()); // the HUD message itself
      subkey = subkey->GetNextKey();
   }
 
   m_Engine->MessageEnd ();
} 
 
// lookup index for user messages
int UserMessageIndex(const char *messageName)
{
   char name[128] = "";
   int sizereturn = 0;
   bool boolrtn = false;
   for(int x=1; x<27; x++)
   {
      boolrtn =  m_ServerDll->GetUserMessageInfo(x, name, 128, sizereturn); 
      if(name && strcmp(messageName, name) == 0)
      {
         return x;     
      }
   }
   return -1;
}

Displaying Your Info in the MOTD Panel

The types of MOTD available are:

TYPE_TEXT = 0  // just display this plain text
TYPE_INDEX = 1 // lookup text & title in stringtable
TYPE_URL = 2   // show this URL
TYPE_FILE = 3  // show this local file

For example: To show a website

ShowMOTD( pEdict, "http://www.yahoo.com", "Yahoo.com !", 2, "" )

To show some text

ShowMOTD( pEdict, "Some Text", "A Title", 0, "" )

Using TYPE_INDEX to Send More Than 255 Characters

TYPE_TEXT is limited to 255 characters. To send a longer piece of info, we need to create an entry in the StringTables and reference that using TYPE_INDEX.

First, we need to have a NetworkStringTableContainer:

INetworkStringTableContainer *m_NetworkStringtable;

Then, using the FIND_IFACE macro from the sample_mm plugin, we need to get the INetworkStringTable interface in Load():

	strcpy(iface_buffer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
	FIND_IFACE(engineFactory, m_NetworkStringtable, num, iface_buffer, INetworkStringTableContainer *);

Next, we can setup our string table entry:

	char msg[4096];
 
	INetworkStringTable *pInfoPanel = m_NetworkStringtable->FindTable("InfoPanel");
	if (pInfoPanel)
	{
		bool save = m_Engine->LockNetworkStringTables(false);
		int StrIdx = pInfoPanel->AddString("myinfo");
		pInfoPanel->SetStringUserData(StrIdx, 4096, msg);
		m_Engine->LockNetworkStringTables(save);
	}
	}

NOTE: 4096 characters is the max length, you should use the smallest size that your string data will fit into.

Finally you can show the MOTD panel with the info you added to the string tables:

ShowMOTD( EntityIndex, "myinfo", "My MOTD Window Title", 1, "" );

NOTE: I've tested this using html formatted text. I assume that the maximum length for standard text would also be 4096 characters. --L. Duke 00:45, 18 March 2006 (EST)