[Tutorial] AFK/BACK Simple System. [YSI]
#2

“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:

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(playeridCOLOR_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(playeridfalse);
    
    
// 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(playeridpNamesizeof(pName));
    
    
// We format a chunk of text that will result in something like “player_killer_123 is now AFK.”
    
format(afkMessagesizeof(afkMessage), "%s is now AFK."pName);
    
// We send the formatted message to all players, including the one going AFK.
    
SendClientMessageToAll(COLOR_YELLOWafkMessage);
    return 
1;

Reply


Messages In This Thread
AFK/BACK Simple System. [YSI] - by TheRealMavericK - 02.09.2018, 12:13
Re: AFK/BACK Simple System. [YSI] - by Eoussama - 02.09.2018, 12:54
Re: AFK/BACK Simple System. [YSI] - by CaptainBoi - 03.09.2018, 07:35
Re: AFK/BACK Simple System. [YSI] - by Sanady - 05.09.2018, 16:10
Re: AFK/BACK Simple System. [YSI] - by solstice_ - 05.09.2018, 21:16
Re: AFK/BACK Simple System. [YSI] - by TheRealMavericK - 07.09.2018, 18:03

Forum Jump:


Users browsing this thread: 1 Guest(s)