[Tutorial] AFK command with Reason [ZCMD] [sscanf2]
#1

Hi guys,

Im going to show you guys how to make an AFK command with a reason.

First off im going to create a variable that would check if the player is afk or not and it would be used when the afk command is used.

Код:
new afk[MAX_PLAYERS];
First off you will have to declare a variable on the top of your script that will store the reason why the player is going AFK.

Код:
new afkreason[MAX_PLAYERS][128]
The last set of brackets just explains you many characters must fit into that array.

now we are going to create the command.
i am going to show you how must you start of the command just for the people who does not clearly understand it.


Код:
CMD:afk(playerid, params[])
{
    return 1;

}
Here above this text is how the basic ZCMD command looks like and the its the easiest method.
now inside this command you will have to declare three variables inside this command, like this :

Код:
new reason[100], name[128], string[128]
Now the first variable i declared was to store the reason that is going to be used later on when the player is playing again and the bracket is how many characters can be used in the variable. the second variable is to store the player's name that is going to be used when displaying to people that he is now afk. the third variable is going to be used in the format function to store the string.

now im going to make a if statement to check if the player is not already afk and to return him a message to tell him.

Код:
if (afk[playerid] == 1) return SendClientMessage(playerid, COLOR_RED, "ERROR : You are already AFK");
the "afk" variable is used to check if the user is afk or not, this variable will be changed to 1 when the command has been used.

the next thing we need to check is if the parameters of the command is correct.

Код:
else if (sscanf(params, "s[100]", reason)) return SendClientMessage(playerid, COLOR_RED, "Usage : /afk [reason]");
The "s[100]" stands for what type of characters would be inserted, for this stands for string and then the "100" is the characters that can be stored in that array. the last parameters is what you type in for your reason and if the parameters is not what it must be it will return a error message.

Ok, the next line is now to take that reason the player has entered and placing it into the variable so that the variable can be used when the player returns.

Код:
afkreason[playerid] = reason;
Now the variable is occupied to be used later.

the next line is now for storing the player's name that used the command into the string you declared as "name" at the top of the command.

Код:
GetPlayerName(playerid, name, sizeof(name));
the next line is using the format function to send all players the message that the player will be now AFK(inactive).

Код:
format(string, sizeof(string), "%s[%i] {CC6600}is now AFK {FFFFFF}- %s ", name, playerid, reason);
You guys probably know how the format function works, the string you made in the format function is now stored in the "string" variable.

The next line of coding is to send all the players the string you made in the format function.

Код:
SendClientMessageToAll(COLOR_WHITE, string);
The next line is just to pause the player so that there is absolutely no movement.

Код:
TogglePlayerControllable(playerid, 0);
Now you are finally done with the AFK command ! Just down below is the AFK command in whole

Код:
CMD:afk(playerid, params[])
{
	new reason[100], name[128], string[128];
	if (afk[playerid] == 1) return SendClientMessage(playerid, COLOR_RED, "ERROR : You are already AFK");
	else if (sscanf(params, "s[100]", reason)) return SendClientMessage(playerid, COLOR_RED, "Usage : /afk [reason]");	
	afkreason[playerid] = reason;
	afk[playerid] = 1;
	GetPlayerName(playerid, name, sizeof(name));
	format(string, sizeof(string), "%s[%i] {CC6600}is now AFK {FFFFFF}- %s ", name, playerid, reason);
	SendClientMessageToAll(COLOR_WHITE, string);
	TogglePlayerControllable(playerid, 0);
	return 1;
}
Now I will show you how to create the back command for a player to return!

the first is line of coding is to declare 2 variables.

Код:
new name[128], string[128];
the two variables will get the player name into the variable "name" and the format function will use "string" to store its data.

the next line will check if the player is afk or not.

Код:
if (afk[playerid] == 0) return SendClientMessage(playerid, COLOR_RED, "ERROR : You are not AFK");
the next line we change the afk variable to "0" because the player will not be AFK anymore.

Код:
afk[playerid] = 0;
the next line we fetch the player's name again.

Код:
GetPlayerName(playerid, name, sizeof(name));
the next thing we have to do is to format a string again just like in the AFK command except we have to tell the people that the player is back and it would the display the reason why he was AFK next to the message.

Код:
format(string, sizeof(string), "%s[%i] {CC6600}is now BACK {FFFFFF}- %s", name, playerid, afkreason[playerid]);
now you just send the string to the players

Код:
SendClientMessageToAll(COLOR_WHITE, string);
The last thing you have to is to change the player's controllable state to "0".

Код:
TogglePlayerControllable(playerid, 1);
Just below is how the BACK command looks in whole.

Код:
CMD:back(playerid, params[])
{
	new name[128], string[128];
	if (afk[playerid] == 0) return SendClientMessage(playerid, COLOR_RED, "ERROR : You are not AFK");
	afk[playerid] = 0;
	GetPlayerName(playerid, name, sizeof(name));
	format(string, sizeof(string), "%s[%i] {CC6600}is now BACK {FFFFFF}- %s", name, playerid, afkreason[playerid]);
	SendClientMessageToAll(COLOR_WHITE, string);
	TogglePlayerControllable(playerid, 1);
	return 1;
}
AFK - BACK Command with reason
Reply
#2

Good, but:

pawn Код:
new afk[MAX_PLAYERS];

CMD:afk(playerid, params[])
{
    new reason[28], Name[MAX_PLAYER_NAME], Inf[100];
    if(sscanf(params, "s[28]", reason)) return SendClientMessage(playerid, -1, "Use: /afk <Reason>");
    afk[playerid] = afk[playerid] == 1 ? 0 : 1;
    GetPlayerName(playerid, Name, sizeof(Name));
    format(Inf, sizeof(Inf), "%s are: %s. Reason: %s", Name, afk[playerid] == 1 ? ("AFK") : ("To back"), reason);
    SendClientMessageToAll(-1, Inf);
    TogglePlayerControllable(playerid, afk[playerid] == 1 ? 0 : 1);
    return 1;
}
Reply
#3

Using ternary operator will be better and will reduce such long code just like @Dolpin code.
Reply
#4

COLOR_RED & COLOR_WHITE?
Come on, define them...
Reply
#5

Quote:
Originally Posted by Topic Creator
name[128]
A player's name can be 24 characters long maximum, why make a variable 104 characters larger?
Reply
#6

Quote:
Originally Posted by tehMix
Посмотреть сообщение
I think It's better If you use [MAX_PLAYER_NAME].
My reply was meant to be a quotation to his code.
I, by accident, used the [PAWN] tags instead of the [QUOTE] tags.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)