17.04.2012, 09:53
(
Последний раз редактировалось TheBetaFox; 17.08.2013 в 21:14.
Причина: Corrections within the text, promises for updates.
)
Things to do with OnPlayerText
First of all, as you may know, the callback OnPlayerText gets called every single time a player types something in the server chat.
There are many uses for it which could help in personalizing your server or even enhancing the way the gamemode itself is played, so I am going to show you some of the more basic uses.
NOTE: This tutorial assumes that you have at least a basic knowledge of the PAWN language and of the default SA-MP functions.
1. IntroductionOnPlayerText itself is easy to understand.
It has two parameters, 'playerid' and 'text[]'.
- playerid is an integer (i.e a whole number) and it represents the ID of the player who has typed the message.
- text[] is a string and it represents the text that the player has typed into the chat.
pawn Код:
public OnPlayerText(playerid, text[])
{
return 1;
}
- Returning 1 sends the default message, like: "Your name: Message"
- Returning 0 doesn't send the default message, allowing you to... improvise.
2. Uses
- Changing the text that was supposed to show up
First of all we change 'return 1;' to 'return 0;'. If we do so, the original message will not be sent.
pawn Код:
public OnPlayerText(playerid, text[])
{
return 0;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
new msg[128]; // The string. We do not need to have it larger than 128, because a message can't be larger than that.
return 0;
}
%d means integer, and we will put the player ID in its place.
%s means string, and we will put the text in its place.
pawn Код:
public OnPlayerText(playerid, text[])
{
new msg[128]; // The string. We do not need to have it larger than 128, because a message can't be larger than that.
format(msg, sizeof(msg), "[%d] %s", playerid, text); // The formatted message. Remember, %d means integer (whole number) and %s is string - they are associated with playerid, respectively text.
return 0;
}
Call SendPlayerMessageToAll, with senderid as playerid and with the text as 'msg', as so:
pawn Код:
public OnPlayerText(playerid, text[])
{
new msg[128]; // The string. We do not need to have it larger than 128, because a message can't be larger than that.
format(msg, sizeof(msg), "[%d] %s", playerid, text); // The formatted message. Remember, %d means integer (whole number) and %s is string - they are associated with playerid, respectively text.
SendPlayerMessageToAll(playerid, msg); // SendPlayerMessageToAll will send a message from the player to everyone, like what would happen normally when a player chats, but with the message we choose.
return 0;
}
You could potentially adjust this so you can see anything you want - if you have a function in your gamemode that returns something like the team name or the rank of the player as a string (i.e as a (group of) word(s), not as a number), we could use it and show it in the message.
pawn Код:
public OnPlayerText(playerid, text[])
{
new msg[128]; // The string. We do not need to have it larger than 128, because a message can't be larger than that.
format(msg, sizeof(msg), "[%s] %s", GetPlayerTeamName(playerid), text); // The formatted message. Remember, %s means string - the strings are associated with GetPlayerTeamName(playerid), respectively text.
SendPlayerMessageToAll(playerid, msg); // SendPlayerMessageToAll will send a message from the player to everyone, like what would happen normally when a player chats, but with the message we choose.
return 0;
}
- Making an admin chat
If the admin makes a message that starts with #, it will automatically be shown to other admins!
We will once again start from the 'default' OnPlayerText, but you can continue on the 'old' one as well.
pawn Код:
public OnPlayerText(playerid, text[])
{
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsPlayerAdmin(playerid))
{
// our code will be here
}
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsPlayerAdmin(playerid) && text[0] == '#')
{
// our code will be here
}
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsPlayerAdmin(playerid) && text[0] == '#')
{
new msg[128];
format(msg, sizeof(msg), "[ADMIN CHAT] %s: %s", pName, text[1]);
}
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsPlayerAdmin(playerid) && text[0] == '#')
{
new msg[128], pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
format(msg, sizeof(msg), "[ADMIN CHAT] %s: %s", pName, text[1]);
}
return 1;
}
Add this to the end of your gamemode:
pawn Код:
stock SendMessageToAdmins(text[])
{
for(new i = 0, i < MAX_PLAYERS, i++)
{
if(IsPlayerAdmin(i))
{
SendClientMessage(i, -1, text);
}
}
}
Now, add it as so into OnPlayerText:
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsPlayerAdmin(playerid) && text[0] == '#')
{
new msg[128], pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
format(msg, sizeof(msg), "[ADMIN CHAT] %s: %s", pName, text[1]);
SendMessageToAdmins(msg);
}
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsPlayerAdmin(playerid) && text[0] == '#')
{
new msg[128], pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
format(msg, sizeof(msg), "[ADMIN CHAT] %s (ID %d): %s", pName, playerid, text[1]);
SendMessageToAdmins(msg);
}
return 1;
}
- Using strfind
This can be used for various things, and I will give an example.
Once again, we start with the 'base' OnPlayerText.
pawn Код:
public OnPlayerText(playerid, text[])
{
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(strfind(text, "how", true) != -1 && strfind(text, "get car", true) != -1) {
// our code goes here
}
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(strfind(text, "how", true) != -1 && strfind(text, "get car", true) != -1) {
SendClientMessage(playerid, -1, "To get a car, please use the /v command.");
return 0;
}
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(strfind(text, "help me up", true)) {
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, X, Y, Z);
SetPlayerPos(playerid, X, Y, Z + 10);
SendClientMessage(playerid, -1, "I've set your position 10 meters higher - I hope you aren't stuck anymore!");
return 0;
}
return 1;
}
- Answering questions
Once again, we start with the base OnPlayerText function.
pawn Код:
public OnPlayerText(playerid, text[])
{
return 1;
}
At the top of your gamemode, put this:
pawn Код:
#undef MAX_PLAYERS
#define MAX_PLAYERS 200
Now, we can create the variables! Make IsAnswering and pAge, with the size of MAX_PLAYERS, and define QUESTION_AGE as 1 and QUESTION_NONE as 0, like so:
pawn Код:
new IsAnswering[MAX_PLAYERS];
new pAge[MAX_PLAYERS];
#define QUESTION_NONE 0
#define QUESTION_AGE 1
pawn Код:
public OnPlayerSpawn(playerid)
{
SendClientMessage(playerid, -1, "QUESTION: How old are you? (please answer the question in the chat.)");
IsAnswering[playerid] = QUESTION_AGE;
return 1;
}
Now, let's add an if statement which checks whether the player is answering QUESTION_AGE.
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsAnswering[playerid] == QUESTION_AGE)
{
// our code will be here!
}
return 1;
}
Here's where we get saved by a very magic function - strval! We will declare a new variable and store the value of the string in it.
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsAnswering[playerid] == QUESTION_AGE)
{
new age; // Declare the age variable.
age = strval(text); // Set it to the value of the text.
pAge[playerid] = age; // Set the Age variable to the typed age, which can be saved with the system of your choice.
IsAnswering[playerid] = QUESTION_NONE; // You can add multiple questions and change it to the ID of the next question, then put the next question below as a ClientMessage.
return 0;
}
return 1;
}
pawn Код:
public OnPlayerText(playerid, text[])
{
if(IsAnswering[playerid] == QUESTION_AGE)
{
new age;
age = strval(text);
if(age >= 18 && age <= 100) // If you're old enough to play San Andreas and in realistic boundaries of age
{
pAge[playerid] = age; // Set the Age variable to the typed age, which can be saved with the system of your choice.
}
else if(age <= 18)
{
SendClientMessage(playerid, -1, "You're not old enough to play San Andreas!");
Kick(playerid);
}
else if(age >= 100)
{
SendClientMessage(playerid, -1, "You can't be that old, and if you are, why are you playing San Andreas?");
Kick(playerid);
}
return 0;
}
return 1;
}
This tutorial might be updated soon with more examples!
(EDIT: as of the 17th of August, 2013, I have not yet managed to do this; however, expect an update soon!)
3. Challenges for beginners
- Make a VIP chat, alongside the admin chat! If you understood anything of what was going on, it should be a piece of cake for you. Otherwise, do read the tutorial again. (Extremely Easy)
- Show a chat bubble above the player with the text that he has typed! (tip: make use of SetPlayerChatBubble) (Extremely Easy)
- Make a mute system! If the player is muted, his message doesn't get sent and he gets a message like 'You are muted. You cannot talk.' (Easy)
- Make a full tutorial! This shouldn't be hard, and only requires you to add more defines and if statements. (Easy)
- Make chat channels! If the player is in channel 1, he can only get messages from channel 1, if he's in channel 2, only from channel 2 and so on. (Medium)
- Make a censorship script: if the player says something like 'hi, <bad word here> you' change it into 'hi, **** you' WITHOUT removing the entire message. (tip: make use of strfind) (Medium/Hard)
P.S: If you actually read the whole tutorial and you have found mistakes in the text, please tell me. Thank you in advance!