[Tutorial] zcmd + sscanf | Detailed Tutorial
#1

Welcome to my "not-so-detailed" tutorial, I am bored and I want to make this, and let's get the things started already.
We will create a very simple "/ban" command.

NOTE: Please read it carefully before going into another step or else you're dead.

First of all we will start at the includes. You must download ZCMD and SSCANF.
You must put it on "<your samp folder>/pawno/includes".
After putting those includes on the said folder, we will now open the infamous .pwn editor, "PAWN".
Press the "New" button on the PAWN and CTRL + A, then backspace to delete all shit, or use ALT + F + N + CTRL + A + Backspace.

I. Getting Started
Put #include <a_samp> at the very top of your script or else you're doomed, because it holds the data of the basic San Andreas Multiplayer script, the one that has "public OnPlayerConnect(playerid)" bla bla bla.

You must put #include <zcmd> too, because you are here to learn ZCMD.
#include <sscanf> too.

So this will be your code now:
pawn Code:
#include <a_samp>
#include <zcmd>
#include <sscanf>
Now let's get fired up!

II. /ban command.
Let's go and create the /ban command already!

First, this is the ZCMD parameters:
CMD:<cmdname>(playerid, params[])
COMMAND:<cmdname>(playerid, params[])


Of course, we will do CMD:ban(playerid, params[]).

Add a bracket facing } right!
And also press TAB to indentate it!
pawn Code:
CMD:ban(playerid, params[])
{
     l <-- here, just press 1 tab
Now, we will create the "variables".

pawn Code:
new id; //Will create variable called "id" which means we will put the Target ID into this variable to recognize the TARGET ID. Don't forget to add ";" at the end.
new reason[20]; //Will create variable called "reason" which means we will put the Ban Reason into this variable to recognize the Ban Reason, and the 20 thing is called Variable Length, or whatever length you want, but the preferred length was 20.
Okay, now we will use sscanf.
pawn Code:
if(sscanf(params,"is",id,reason[20])) //Okay what the hell is this? This means the SSCANF will check if the //parameters is CORRECT. These "i" and "s" beside PARAMS are called PLACEHOLDERS.
Placeholders are like this:
Code:
i	Integer (whole number)
d	Integer (whole number).
s	String (Letters)
f	Floating-point number (Float: tag)
c	ASCII character
x	Hexadecimal number
b	Binary number
Okay so what the hell is placeholders? As I said earlier, it will check if the parameters is correct.
It's like this: "i" and "d" are for whole numbers, so if I put if(sscanf(params,"id",id,reason[20])) will make it wrong, because REASON VARIABLE is for STRING(composed of letters), and IF ITS NOT THE RIGHT PLACEHOLDER, your server might crash, or whatever. But it's best to match the placeholders.

pawn Code:
if(sscanf(params,"is",id,reason[20])) return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /ban [id] [reason]");
If the code you entered is wrong, you can stop right there and say on the player that they have the wrong parameters. To stop it, we have something called "return", it can stop you from the script.

If it doesnt have a "RETURN", it will be like if you make the command fail, you can still continue on the script, which will cause some bugs.

pawn Code:
if(id == playerid) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: You cannot ban yourself.");
Now, if you insert "ID" on the COMMAND(in-game) and you put your own ID(which is playerid(and playerid can be anyone, depends on your script, but on this tutorial I will just show you that you are the only playerid.)) will stop(return) you from script, and sends the playerid an "ERROR".

pawn Code:
if(IsPlayerConnected(id)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player is not connected!");
IsPlayerConnected means if the Player is connected(which was ID, ID is our target ID), will generate a error saying "Player is not connected."

III. Message, and Ban.
Now we will get the two player id's, which was (you(playerid) and target(id)).

pawn Code:
//We will use name1 & name2 name variable. You can do any names.
new name1[MAX_PLAYER_NAME], name2[MAX_PLAYER_NAME], string[256];
//What is MAX_PLAYER_NAME? It is a Player's name. If you don't put the "MAX_PLAYER_NAME" the a_samp will not detect if you have created a variable to store a name. String is for where we put the variables. I'll explain later for the string.
We will get the two player names.
Parameters: playerid, namevariable, sizeof(namevariable));
The size/len of the name is the MAX_PLAYER_NAME itself.
pawn Code:
GetPlayerName(playerid, name1, sizeof(name1));
GetPlayerName(id, name2, sizeof(name2));
Time for the message!
We will use the string now, and I'll explain it now.
SendClientMessage is for normal messages, which means only text.
format is for not normal messages, which means you can put a variable and use it in the text.
We will put the two variables(name1, and name2) on the text to show that (playerid) has banned (id). Reason: (reason)!
pawn Code:
format(string, //We will use the string on the format.
pawn Code:
format(string, sizeof(string), //The size of string beside the variable string, which was 20
pawn Code:
format(string, sizeof(string), "%s [%d] has banned %s [%d]. Reason: %s.",name1,playerid,name2,id,reason);
Remember the placeholders I told you earlier? Now I'll give you again the placeholders.
Code:
%i	Integer (whole number)
%d	Integer (whole number).
%s	String
%f	Floating-point number (Float: tag)
%c	ASCII character
%x	Hexadecimal number
%b	Binary number
%%	Literal '%'
Okay, the %s [%d] mans a STRING and a NUMBER. It is like "Name [playerid]".
It does same for the other one, %s [%d] too, to show the names.
And the reason is %s, because the REASON Variable is a STRING.

Okay, what the fok is this placeholders for format?
Can you see the variables name1,playerid,name2,id,reason beside it?

Remember our variable name1(which was playerid, we put the playerid name there.),
name2(which was id, we put the id(targetid) name there.),
playerid(which was the id of the playerid, and it's you.)
id(same as playerid above).

Output:
Code:
  %s     [%d] has banned  %s    [%d]. Reason: %s
name1,playerid..........name2,...id,.........reason
Now we are finished on the format! We will now send it to the players.
pawn Code:
SendClientMessageToAll(-1, string); //Use string instead of "string", else it will say "string" not the string that you've made
And the ultimate of all the ultimate... the BAN HAMMER!
Ban(id);

Put it under SendClientMessageToAll.

Now put "return 1;" under SendClientMessageToAll, the server will recognize that it was a command.

And add a bracket facing left. {

pawn Code:
CMD:ban(playerid, params[])
{
//codes here...
     return 1;
} //remove indentation
Thank you! I hope you got something from me.
#Bored
Reply
#2

Nice
Reply
#3

You are teaching people to use string sizes that are completely unnecessary.

How big can a reason be? 128 is the max input and you set that as the size of a message supposed to be really short. 256 shouldn't ever be used in this case, either.
Reply
#4

Quote:
Originally Posted by Scaleta
View Post
You are teaching people to use string sizes that are completely unnecessary.

How big can a reason be? 128 is the max input and you set that as the size of a message supposed to be really short. 256 shouldn't ever be used in this case, either.
The idea is to use it but know your comment is truth.
Good tutorial!
Reply
#5

Good Tuto !!!!
Reply
#6

Quote:
Originally Posted by Scaleta
View Post
You are teaching people to use string sizes that are completely unnecessary.

How big can a reason be? 128 is the max input and you set that as the size of a message supposed to be really short. 256 shouldn't ever be used in this case, either.
Its just a tutorial... well, let me change it for you.
Reply
#7

just pointing it out, if it is sscanf plugin, which likely is, sscanf string requires size near "s" character, and no []
for string variable in scanf
Code:
if(sscanf(params,"is[20]",id,reason))
Reply
#8

Nice tutorial though. +Rep
Reply
#9

Thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)