Short Tutorial by TheLazySloth
Step 1: The Command
First off we need a way for the player to tell the server he wants to send messages to players near-by.
We'll call the command /s. So let's make it!
> - Is where we need to indent.
public OnPlayerCommandText(playerid, cmdtext[]) {
>if(strcmp(cmdtext, "/s", true, 2) == 0) { // Checks if cmdtext starts with '/s'
>>return true; // Tells the server it does.
>}
>return false; // Tells the server it doesn't and sends cmdtext to OnPlayerText
}
Step 2: Making the parameters.
Now that the server can detect when the player typed the command /s we need the server to detect the parameters. Here's how.
> - Is where we need to indent.
public OnPlayerCommandText(playerid, cmdtext[]) {
>if(strcmp(cmdtext, "/s", true) == 0) {
>>strdel(cmdtext, 0, 2); // This will delete '/s ' from the cmdtext.
>>return true;
>}
>return false;
}
Step 3: Checking if other players are in range of the player.
Now that the server detected the command and "grinded the excess" parameters. We need a way for our server to check for players that are in range. Here's how.
> - Is where we need to indent.
public OnPlayerCommandText(playerid, cmdtext[]) {
>if(strcmp(cmdtext, "/s", true) == 0) {
>>strdel(cmdtext, 0, 2);
>>
>>new Float: pX, // These are the variables we need.
>>> Float: pY,
>>> Float: pZ;
>>
>>GetPlayerPos(playerid, pX, pY, pZ); // This gets the player's exact position.
>>
>>for(new i = 0; i < MAX_PLAYERS; i++) { // This will loop through every player.
>>>if(IsPlayerConnected(i) && IsPlayerInRangeOfPoint(i, 15.0, pX, pY, pZ)) { // This is the range check.
>>>}
>>}
>>return true;
>}
>return false;
}
Step 4: Sending the message!
Okay we got every single player that is in range of the player who sent the command... Now our last and final step is telling the other players who sent the command and the message the types after the commands.
> - Is where we need to indent.
public OnPlayerCommandText(playerid, cmdtext[]) {
>if(strcmp(cmdtext, "/s", true) == 0) {
>>strdel(cmdtext, 0, 2);
>>
>>new Float: pX,
>>> Float: pY,
>>> Float: pZ,
>>> pName[MAX_PLAYER_NAME], // the name variable we need.
>>> String[128]; // where we store our string.
>>
>>GetPlayerPos(playerid, pX, pY, pZ);
>>GetPlayerName(playerid, pName, MAX_PLAYER_NAME); // Gets the player's name.
>>format(String, 128, "%s says, \"%s\"", pName, cmdtext); // Makes the message sent to other players.
>>
>>for(new i = 0; i < MAX_PLAYERS; i++) {
>>>if(IsPlayerConnected(i) && IsPlayerInRangeOfPoint(i, 15.0, pX, pY, pZ)) {
>>>>SendClientMessage(i, -1, String); // Send the message for the other players.
>>>}
>>}
>>return true;
>}
>return false;
}
And that's how you do it! +rep appreciated =] (Yes, I got bored.)