[Tutorial] Group Chat Without Symbols(Includes Admin Chat)
#1

Player Friendly Group/Team/Admin Chat

What is this?
This tutorial explains you in detail how to develop a group chat system that is different from the usual command way and is much more user-friendly.Many of us use "/a [message] " or "/teammsg [message]" to make group chats.But typing / and a command is actually not very friendly.Instead, in this special way, if a player just adds a prefix such as # and sends a message, it would automatically be treated as a group message.You might have already observed this in many servers.

Example:
You are in a cop team and you want to send an urgent message to the other cops requesting backup
Which of the following would be easier?
  • /copmsg I need backup
  • #I need backup
Both do the same.Case 1 is the way which most of them would do.Case 2 is the one I wish to show here.I find that case 2 would be easier since it involves less typing.

In this tutorial we shall learn how to develop such a chat system where players can send a group message by just adding a prefix character.

NOTE:This idea is NOT mine and is already being used in many servers


The Purpose of the Tutorial:
I find typing long commands,etc irritating sometimes.This symbol-prefix chat is very player-friendly and many would like it.And also someone asked me how to do it so I decided to write a tutorial on it.


Pros and Cons(Advantages & Disadvantages):
Positives:
  • User-friendly
  • Easy and Simple Coding
  • Efficient
Negatives:
No Negative Aspects about it

Requirements
You need to know at least the basics of PAWN,little about arrays and few natives(format,IsPlayerAdmin,SendClientMessage).

Let's start
For this tutorial I am using # as the prefix for sending an administrator message.Any message that comes from a player which begins with a # will be considered as an administrator message and will be sent to his administrators only.If the player is not an administrator then it will sent to the main chat.

Before we start :P
  • When a player types a message, the message is sent to OnPlayerText Callback.
  • We need to check the first character of the text to see if the first letter matches with the special prefix symbol.
  • If it matches forward the message to other administrators.

Step 1:Checking for the special prefix
If a player sends a special message with a prefix, the first letter of the message will have the prefix letter.The first element in the text array will be holding the first character so we must check text[0].

We'll use an if statement to check if text[0] matches.
And its 0 not 1.For computers the first number is always 0.The first letter will be stored in text[0].The second letter will be stored in text[1].....the third in text[2]...

Code:
public OnPlayerText(playerid,text[])
{
     if(text[0] == '#') //Checks for the symbol
    {
	//It matches  
        return 0; //Returning 0 will stop the server from sending the message to everyone  
    }
    return 1;
}
Step 2:Check if the player is an administrator
Since this feature is an administrator feature, normal players must not be allowed from using this.So we must check if the player is an administrator.

Once again we'll need to use an if statement to do the check.
Code:
public OnPlayerText(playerid,text[])
{
     if(text[0] == '#') //Checks for the symbol
    {
	if(IsPlayerAdmin(playerid)) //Will check if the player is an administrator
        {
            return 0; 
        }         
    }
    return 1;
}
Step 3:Changes to the text
We get the playerid and the text.If we send just the text to the other administrators, they would just receive the message and without the senders name.We will need to add the player name manually. Also we would like make some changes to the message such as add some additional prefix such as [ADMIN-CHAT] to the message.Lets do these modifications here.
This might be a bit complex compared to the code that we've tried till now.So I will try my best to explain part by part.

First we'll need to create a string(a variable that can hold text) variable that will store the modified text.
We have to use the new operator.Strings are a text and text are nothing but a group of characters so we need to create an array (this [some number comes here] ,for example abc[23]) of characters.

This is how we need to declare an array.

Code:
new IDENTIFIER[ARRAY_SIZE];
Identifier is the name given to a variable to identify it ( :P ).
Array Size is the number of elements that variable will have.It will be 128 here because we are sending the administrator message via client messages and the maximum length of a client message can be no more than 128.A larger value would be useless and also reduce the efficiency of the code.

Code:
new string[128];
To add [ADMIN-CHAT] and the Player Name to the message we need to use a SAMP native function 'format'.
What format does is it takes an input we give special escape characters(%s,%d,...) which the format replaces with the values that we provide.

%s stands for string
%d stands for an integer
There are more but that's enough for this tutorial

If you are interested about format, check SAMP Wiki page on format
Format @ SAMP Wiki

format of the function format
format(string[], length,format[], values,.....)

For example:
Code:
new a,b,c,str[32];
a = 10;
b = 20;
c = a + b; //The value of c will be a + b = 10 + 20 = 30 
format(str,32,"The sum of %d and %d is %d",a,b,c);
The result will be
"The sum of 10 and 20 is 30"

This is what format does.We'll use this awesome function to do the string modifications.

Here is my plan.First we will add "[ADMIN-CHAT]" then add the player name.
It should look like this
Quote:

[ADMIN-CHAT]Yashas(0):Testing :P :P :P

Wait, how do we get the player name?


We need to use the function GetPlayerName(playerid,string_to_store_in[],length);
GetPlayerName will take the playerid and give us the players name as a string.

And for that we need one more string variable to store the name

This code will do it.This code must be self-explanatory :P
Code:
new pName[24];
GetPlayerName(playerid,pName,24);
Back to format work.We got the name.We have all the information we need and we can proceed with format.

Code:
format(string,sizeof(string),"[ADMIN-CHAT]%s:%s",pName,text[1]);
Everything seems nice except that text[1].
Do you know why that 1 has come?
The first letter is the symbol...did you get it??
Since the first letter was just used for identification, we don't want that to be in the message.So what text[1] does is it gives the format the message from the the 2nd character.The # won't be there in the final message.

For example, the text was "#omg"
If we give text[0] to format it will add "#omg"
but if we give text[1] it will add only "omg"

that symbol won't be shown


The code would look something like this at this stage
Code:
new string[128],pName[24];
if(text[0] == '#')
{
    if(IsPlayerAdmin(playerid))
    {
          GetPlayerName(playerid,pName,24);
          format(string,sizeof(string),"[ADMIN-CHAT]%s:%s",pName,text[1]);
	  return 0;
    }
}
Step 4:Sending the message to all administrators
The message is nicely formatted and the last thing left is to send it to all the online administrators.
We will need to loop through all the players and check if he/she is an administrator and send the message.

I am using a for loop that will check all players and send the message if the player is an administrator.
Code:
for(new i;i < MAX_PLAYERS;i++)
{
      if(IsPlayerAdmin(i))
     {
	 SendClientMessage(i,0xFF9900FF,string);
     }
}
The above code will loop through all the players and check if he/she is an administrator then if he/she is then send the client message.
Final Code
The final code should be somewhat like this
Code:
public OnPlayerText(playerid, text[])
{
     new string[128],pName[24];
     if(text[0] == '#')
     {
	 if(IsPlayerAdmin(playerid))
	 {
              GetPlayerName(playerid,pName,24);
	      format(string,sizeof(string),"[ADMIN-CHAT]%s:%s",pName,text[1]);
	      for(new i;i < MAX_PLAYERS;i++)
	      {
	          if(IsPlayerAdmin(i))
	          {
	               SendClientMessage(i,0xFF9900FF,string);
	          }
	      }
	      return 0;
	 }
     }
     return 1;
}
Notes:
To reduce the complexity of the code I had not used sizeof and defines,etc.If you wish to keep the code neat and safe then do it.Its a good programming habit to keep the code organised and neat.

Screenshots:




Thanks:
I hope you've understood and learned a lot from this tutorial.If you find any mistakes,typos,bugs or have any suggestions, please let me know.

Thank You
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)