01.01.2013, 05:21
(
Last edited by deffo; 01/01/2013 at 05:28 PM.
)
Hello,
This is my second tutorial in samp forum, to explain about toggling chat for a player.
Tutorial Level: Beginner
Important points (Basics):
(If you know scripting basics, no need to read this part)
Implementation:
Command:
Drawbacks:
That is all!
thankyou
This is my second tutorial in samp forum, to explain about toggling chat for a player.
Tutorial Level: Beginner
Important points (Basics):
(If you know scripting basics, no need to read this part)
- Most of you already know that by returning 0 in the OnPlayerText native callback, the player is not able to say anything on the chat.
It can be done by:
pawn Code:public OnPlayerText(playerid, text[])
{
return 0;
}
- The above thing will disable the chat for all players, however we wanna make the chat toggle-able for all players. So now the second thing I would like to mention is about player parameter. You can have any player parameter to use in this toggle chat system. You can use either a parameter declared in enum or a bool: playerchatoff array (playerchatoff[MAX_PLAYERS]) or any other method of your choice, here I am using the bool method.
pawn Code:new Bool:playerchatoff[MAX_PLAYERS];
Note: This would be a global variable.
- By default we wanna make the chat on for all players. So simply we can add this in the OnPlayerConnect callback.
pawn Code:public OnPlayerConnect(playerid)
{
playerchatoff[playerid]=false; //This means the player has his chat enabled.
return 1;
}
pawn Code:public OnPlayerText(playerid, text[])
{
if(!playerchatoff[playerid])
{
return 1; //Now we are returning 1, which means the chat is on for players with playerchatoff=false
}
else
{
return 0; //Now we are returning 0, which means the chat is off for players with playerchatoff=true
}
}
Implementation:
- Now comes main part, i.e. implementing this on your script. Wherever you are sending a message on the chat using any of the functions such as SendClientMessage, SendPlayerMessageToPlayer, SendClientMessageToAll, etc. you will need to add a condition before them like this,
pawn Code:if(!playerchatoff[playerid])
SendClientMessage(playerid,color,message);
Note:You will have to add that if condition everywhere in your code where ever you are sending a message on the chat.
- Now when the player types only a "/" or an invalid command, it will show him message "Server unknown command" on the mainchat, hence to disable that, simply add the else condition under OnPlayerCommandText callback function (native).
Example:
pawn Code:public OnPlayerCommandText(playerid, cmdtext[])
{
if (strcmp("/command1", cmdtext, true) == 0)
{
//whatever
return 1;
}
else
{
if(!playerchatoff[playerid])
SendClientMessage(playerid,-1,"Server unknown command");
return 1;
}
}
- Now after doing the above, the player who has disabled the chat, will still be able to see the messages from other players who have enabled the chat. So now we have to modify out OnPlayerText callback once again as follows (See the pawn code below).
pawn Code:public OnPlayerText(playerid, text[])
{
if(!playerchatoff[playerid]) //We have to ensure such that all message go via function SendClientMessage.
{
new msg[155],pName[25];
//We will need to format the message so that it can be seen who typed the message.
GetPlayerName(playerid,pName,25);
format(msg,sizeof(msg),"%s (%d): %s",pName,playerid,text);
for(new i=0;i<MAX_PLAYERS;i++) //We will loop the message to all online players and send message to everyone with some if conditions as mentioned.
if(IsPlayerConnected(i))
if(!playerchatoff[playerid]) //This condition will ensure that the message is displayed only to players who have there player chat as enabled.
SendClientMessage(i,-1,msg);
}
return 0; //We will return 0 for all players because we will now send message through SendClientMessage function.
}
Command:
- Command to script the above can be used along with clear chat as follows,
pawn Code:public OnPlayerCommandText(playerid, cmdtext[])
{
if (strcmp("/togglechat", cmdtext, true) == 0)
{
if(!playerchatoff[playerid])
{
for(new i=0;i<20;i++)
SendClientMessage(playerid,-1," "); //Clears the chat for the player, this is inside a loop (for loop)
playerchatoff[playerid]=true; //If the player has turned the chat ON, then turn OFF.
}
else
playerchatoff[playerid]=false; //If the player has turned the chat OFF then turn ON.
return 1;
}
}
Drawbacks:
- The default maximum length of a message is 128 characters in samp, however this will decrease the maximum length as per characters in the players name along with 5 characters.
Note:The maximum length can decrease upto a limit of 100 characters, not beyond that if the player name is of 20 characters and ID is in 100s.
That is all!
thankyou