Creating Join and Leave Messages

From SA-MP Wiki

Jump to: navigation, search

Creating a Join Message

public OnPlayerConnect(playerid)
{
    new pname[MAX_PLAYER_NAME], string[22 + MAX_PLAYER_NAME];
    GetPlayerName(playerid, pname, sizeof(pname));
    format(string, sizeof(string), "%s has joined the server", pname);
    SendClientMessageToAll(0xAAAAAAAA, string);
    return 1;
}

The above code will show a join message in the client chat. It is placed in the OnPlayerConnect callback, which is called every time a player connects. That means that this code will be executed each time a player joins the server. First, a new array pName will be created with size of MAX_PLAYER_NAME (defined as 24 in a_samp.inc). Later, we will store the player's name in this array.

Then we define a new array called string. In this array we will be saving the join message. The size is 48, so that both player's name (pName, 24 chars) and message (" has joined the server.", 23 chars) and an invisible NUL-byte to terminate the string fit into our array.

Now, GetPlayerName can be called to fetch the player's name. It will be stored in the pName array (the second parameter), with a max length of sizeof(pName), the size of pName (which was 24).

Since we know the player's name now, we can format the join message with format. The first parameter allows us to specify where we want to have the formatted string. We created the string array for this purpose earlier. The second parameter is the max length of the formatted string. That will be the size of the string. The third parameter is the actual string we want to format. %s is a placeholder for another string. Using the forth parameter we specify that it should be replaced with pName. That way we will have "<player's name> has joined the server." in the string array.

Finally, we can send the message to the client chat using the SendClientMessageToAll function. The first parameter specifies a gray color, the second one is the actual message.

Creating a Leave Message

Now we will make the leave messages and add if they timed out, left, or were kicked.

public OnPlayerDisconnect(playerid, reason)
{
    new pname[MAX_PLAYER_NAME], string[39 + MAX_PLAYER_NAME];
    GetPlayerName(playerid, pname, sizeof(pname)); 
    switch(reason)
    {
        case 0: format(string, sizeof(string), "%s has left the server. (Lost Connection)", pname);
        case 1: format(string, sizeof(string), "%s has left the server. (Leaving)", pname);
        case 2: format(string, sizeof(string), "%s has left the server. (Kicked)", pname);
    } 
    SendClientMessageToAll(0xAAAAAAAA, string);
    return 1;
}

This code is quite similar to the join code, except that it placed in the OnPlayerDisconnect callback, when is executed each time a player leaves. The switch statement allows us to create different messages according to the reason. Reason 0 is a sudden disconnect (a timeout), reason 1 is a disconnect initiated by the client (the player exited GTA) and reason 2 is a disconnect initiated by the server (kick or ban).

Notice that the string array now has a size of 56 cells, to accommodate the largest possible message (24 + 31 + 1). In this case, the timeout message is the longest.

Personal tools
Navigation
Toolbox
In other languages