02.09.2018, 12:54
(
Last edited by Eoussama; 02/09/2018 at 12:56 PM.
Reason: Some typo correction.
)
“Spoonfeeding is not tutoring”, don't ever present ready snippets of code on a silver plate and call it a tutorial. Unless you present a full code analysis that is.
There are some notes to raise here though like string sizes “256” sounds horrifyingly big. Here's a cleaner version of your code:
There are some notes to raise here though like string sizes “256” sounds horrifyingly big. Here's a cleaner version of your code:
PHP Code:
// A boolean array makes more sense in this context, a player is going to be
// either AFK (true) or not AFK (false).
new bool:AFKMode[MAX_PLAYERS];
// Hex-colors definitions.
#define COLOR_RED 0xFF000088
#define COLOR_YELLOW 0xFFFF0088
// We can omit the “params” arguments as it's not needed here.
CMD:afk(playerid)
{
// We test to see if AFKMode is equal to true, if so, then the player is already AFK, and thus we return the message
// “You are already AFK.” and the code below it won't bother to execute.
if (AFKMode[playerid]) return SendClientMessage(playerid, COLOR_RED, "You are already AFK.");
// Remember, readable variable names, and following a naming pattern makes it easier on the eye, in this case, it's camelCase.
// afkMessage size: 14 + 1 (NUL character) + MAX_PLAYER_NAME + 1 (NUL character).
// pName size: MAX_PLAYER_NAME + 1 (NUL character).
new afkMessage[15 + MAX_PLAYER_NAME + 1], pName[MAX_PLAYER_NAME + 1];
// We set the AFKMode variable to true, meaning that the player is AFK.
AFKMode[playerid] = true;
// We freeze the player so that he or she cannot move or control their camera.
// Passing false or 0 (negative values) as the second parameter acts the same.
TogglePlayerControllable(playerid, false);
// We get the player's name and store it in the pName variable.
// If the player's name is “player_killer_123”, that is going to be the value of pName.
GetPlayerName(playerid, pName, sizeof(pName));
// We format a chunk of text that will result in something like “player_killer_123 is now AFK.”
format(afkMessage, sizeof(afkMessage), "%s is now AFK.", pName);
// We send the formatted message to all players, including the one going AFK.
SendClientMessageToAll(COLOR_YELLOW, afkMessage);
return 1;
}