Sunday, February 2

How To Make AMX Mod X Plugins With Colored Messages

Many people usually ask me how to make existing plugins which they download to show colored messages. I usually redirect them to Dyn Native / Stock version of color chat by ConnorMcLeod on AM forums. 90% of them come back to me and say that they were unable to understand what was done in there and I have to re do it while explaining everything over and over again. So I have decided to make a post here so they can read here whenever necessary. I'll basically explain both the methods below, the one I use and the other one which is redirect others to.


My Method (Easy)

In this method you just add some code to the end of your existing or new plugin and you just replace all the print methods.

Step 1 : Copy the code below and add it to the last of your plugin's source.

stock client_print_color(id, type, const text[], any:...)
{
 if(type == print_chat)
 {
  new g_iMsgidSayText;
  g_iMsgidSayText = get_user_msgid("SayText");

  new szMsg[191], iPlayers[32], iCount = 1;
  vformat(szMsg, charsmax(szMsg), text, 3);

  replace_all(szMsg, charsmax(szMsg), "!g","^x04");
  replace_all(szMsg, charsmax(szMsg), "!n","^x01");
  replace_all(szMsg, charsmax(szMsg), "!t","^x03");

  if(id)
   iPlayers[0] = id;
  else
   get_players(iPlayers, iCount, "ch");

  for(new i = 0 ; i < iCount ; i++)
  {
   if(!is_user_connected(iPlayers[i]))
    continue;
   
   message_begin(MSG_ONE_UNRELIABLE, g_iMsgidSayText, _, iPlayers[i]);
   write_byte(iPlayers[i]);
   write_string(szMsg);
   message_end();
  }
 }
}

Step 2 :
After you add this code to your plugin you just have to replace all the client_print code.
I'll give an example, use your brain and do the rest with all the code.

Change this -
client_print(id,print_chat,"This is a normal chat message in your plugin")
to
client_print_color(id,print_chat,"!gThis is a !tcolored chat!n message which you edited!")

You can use these codes for color chats -
!g = Green
!n = Normal
!t = Team

These codes can also be used in the language file. No extra changes necessary.


AM Stock Include Method (Intermediate)

Step 1 :
For this method you have to download the stock colorchat.inc from here. You have to copy this file to your compiler's includes folder.

Step 2 :
Open your plugin's source and add the include code first
#include <colorchat>

Step 3 :
After this you have to look for all instances of client_print code and replace them as below
client_print(id,print_chat,"This is a normal chat message in your plugin")
to
client_print_color(id,id/RED/BLUE/GREY,"^4This is a ^3colored chat^1 message in your plugin")
In this code you have to change the second part to either id, Red, Blue or Grey. This is the base color which will be used when you use team color (^3).

If your plugin uses a language file, i.e it has register_dictionary and you wish to use color chat in that too then you need to change
Eg -
register_dictionary("common.txt")
to
register_dictionary_colored("common.txt")

In this method you have to use the following color codes -
If adding to source code then
^4 = Green
^3 = Team
^1 = Normal

If adding in Lang file then -
!g = Green
!t = Team
!n = Normal

This post was written in a hurry but I'm pretty sure it covered what it meant to.
Still if you face any problems implementing the code please leave a comment below and please be descriptive, don't just say its not working.

12 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. i have tried the first option on statsx plugin added the code and changed the rank line to this

    "client_print_color"

    and changed in the .txt file the language file to this:

    YOUR_RANK_IS = !tYour rank is !g%d !tof !g%d !twith !g%d kill(s), %d hit(s), %0.2f%% eff. !tand !g%0.2f%% acc.

    and in game when i type /rank it gives me


    *SPEC* tfl.!@#$^*+= : /rank
    * ML_NOTFOUND:

    HELP !!!

    ReplyDelete
  3. it still gives

    *SPEC* tfl.!@#$^*+= : /rank
    * ML_NOTFOUND:

    HELP :)

    also i did not change this

    register_dictionary

    to

    register_dictionary_colored

    because i am using first method i does not say to do i have to ?

    ReplyDelete
  4. also i did not add !g !t !n to the

    "client_print_color" in the source code i only changed it in the .txt file do i have to add color codes in the "client_print_color"
    and also in the txt file for it to work ?

    ReplyDelete
    Replies
    1. You first need to add the stock code to the end of your plugin's source. Check that step if you have added that code to your plugin.
      Then go for editing the language file or the text in the source code as per your needs.

      Delete
  5. I've tried your advanced method. Replacing it with client_print_color(id,id/RED/BLUE/GREY,"^4This is a ^3colored chat^1 message in your plugin"), you'll only display only 1 message to only 1 player, without displaying it for the rest of the players. You need to add a for loop in front e.g.

    for(new i; i<iNum; i++)
    {
    player = iPlayers[i]
    client_print_color(id,id/RED/BLUE/GREY,"^4This is a ^3colored chat^1 message in your plugin")
    }
    so that each player will get a chat message in his own team color.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete

Comment anything you want. Just be polite and give respect to others!
I am simply going to remove the comments which are offensive or are off topic.
And please don't spam your website links in comments. I don't, neither should you.