[Tutorial] Implementing local chat into your script!
#1

Local Chat

This is my first tutorial, and this tutorial will teach you how to implement a local chat system that is used by most roleplay servers.

pawn Код:
//Colors required for Local Chat tutorial
#define COLOR_FADE1 0xFFFFFFFF
#define COLOR_FADE2 0xC8C8C8C8
#define COLOR_FADE3 0xAAAAAAAA
#define COLOR_FADE4 0x8C8C8C8C
#define COLOR_FADE5 0x6E6E6E6E
#define COLOR_WHITE  0xFFFFFFFF
1. Firstly, you will need to include a function called ProxDetector.
pawn Код:
ProxDetector(Float:radi, playerid, string[], col1, col2, col3, col4, col5)
{
    new Float:pPositionX[3], Float:oPositionX[3];
    GetPlayerPos(playerid, pPositionX[0], pPositionX[1], pPositionX[2]);
    foreach(Player, i)
    {
        GetPlayerPos(i, oPositionX[0], oPositionX[1], oPositionX[2]);
        if(IsPlayerInRangeOfPoint(i, radi / 16, pPositionX[0], pPositionX[1], pPositionX[2])) { SendClientMessage(i, col1, string); }
        else if(IsPlayerInRangeOfPoint(i, radi / 8, pPositionX[0], pPositionX[1], pPositionX[2])) { SendClientMessage(i, col2, string); }
        else if(IsPlayerInRangeOfPoint(i, radi / 4, pPositionX[0], pPositionX[1], pPositionX[2])) { SendClientMessage(i, col3, string); }
        else if(IsPlayerInRangeOfPoint(i, radi / 2, pPositionX[0], pPositionX[1], pPositionX[2])) { SendClientMessage(i, col4, string); }
        else if(IsPlayerInRangeOfPoint(i, radi, pPositionX[0], pPositionX[1], pPositionX[2])) { SendClientMessage(i, col5, string); }
    }
    return 1;
}
This code mainly does all the work, finding what players near you, and sends the message to all the local players within the radius specified in the first parameter. The color parameters are going to be used on whether how far the player is within the player radius, COL1 is the closest, going in descending order.

2. Using the ProxDetector function.

Now that we have the function added onto our script, we will be able to use it, although we will still have to restrict the talk settings otherwise everyone will be able to use regular chat.
All of the text entered is processed through a event called OnPlayerText, you will be creating it so you cannot use regular talk, and restrict it to local talk unless you use a specific command such as: /OOC

pawn Код:
public OnPlayerText(playerid, text[])
{
    new pname[MAX_PLAYER_NAME], str[128];//reduce it if you want
    GetPlayerName(playerid, pname, sizeof(pname));
    strreplace(pname, '_', ' ');
    format(str, sizeof(str), "%s says: %s", pname, text);//Appearance of the text submitted by a nearby player.
    /*
     * The first parameter states the radius of the area where the text will be submitted and if a player is inside the radius, the person will receive it.
     * The second parameter is the player that is submitting the text.
     * The third parameter will specify what exactly the text is, in this case %speaker says: %text.
     * The rest of the parameters are the fading colors, where how far the receiver is far from the talker.
    */

    ProxDetector(30.0, playerid, str, COLOR_FADE1, COLOR_FADE2, COLOR_FADE3, COLOR_FADE4, COLOR_FADE5);
    return 0;//So that the text is not submitted regularly.
}
The explanation has been commented inside the code.

3. Adding OOC
Alternatively, if you want to speak to all the players in the server, you can use the snippet below, although you require ZCMD.

pawn Код:
CMD:ooc(playerid, params[])
{
    new sendername[MAX_PLAYER_NAME], string[128];
    GetPlayerName(playerid, sendername, sizeof(sendername));
    strreplace(sendername, '_', ' ');
    format(string, sizeof(string), "(( %s: %s ))", sendername, params);
    SendClientMessageToAll(COLOR_WHITE, string);
    return 1;
}
If you don't have the strreplace function, here it is.
pawn Код:
stock strreplace(string[], find, replace)
{
    for(new i=0; i < sizeof(string[i]); i++)
    {
        if(string[i] == find)
        {
            string[i] = replace;
        }
    }
}
I hope this tutorial has taught you the basic information you need to develop a local chat system.
-Aleksander

Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)