27.02.2011, 00:48
(
Последний раз редактировалось admantis; 27.02.2011 в 15:20.
)
Introduction
Tired of these newbies using strcmp along with strtok. When will they learn? They say it's too hard to learn sscanf2 and zcmd. I thought it was hard too, but I had to focus, take a coffee and look the code. I learnt it in 2 minutes. I will teach you how to use ZCMD as COMMAND PROCESSORS and with sscanf2 to help us to split the parameters.
What we will need?
For this tutorial you need to download the plugin sscanf2 and the include zcmd
Sscanf2: http://forum.sa-mp.com/showthread.ph...hlight=sscanf2
Zcmd: http://forum.sa-mp.com/showthread.ph...highlight=zcmd
There is also a function version of sscanf2, no need to download anything just copy & paste in your gamemode it and sscanf2 is ready to go, but it's slow, probably just slow as strtok so for your own good im not posting it.
Remember zcmd can be implemented in apart functions so they should NOT be inside any callback, so if you are not going to use strcmp to process commands you can delete 'OnPlayerCommandText' as we won't use it
Let's get to work
To make a basic command without parameters, you will first see this.
It works the SAME way as strcmp command just change the start to COMMAND:...., and code like you would always do in PAWN.
However, if you want to heal other player, the thing gets more complex.
Now the explanation
As first, we need to put always that to split the parameters, if they weren't there it will return something (in this case 'Incomplete value' error.
In every command, it should be "if (sscanf(params," untill that point. Then you will need to choose a operator. Where "u" means a playerid OR a part of a name.
We can also use "q" and "r" for player names / ids.
We use "i" or "d" for numbers.
We use "s[size]" for strings.
We use "x" for hexadecimals
We use "f" for floats (health, positions and armour)
More operators can be also found in the official sscanf2 thread.
Also it's important to contain the parameters after the operators.
For example, a common mistake is to copy paste the previous command, and change the functions to make it a different command, but if this command has more parameters it will fail.
Example:
Do you spot the error? I do, do you? Probably not.
Here it checks if 'id' is not put, but what about 'ammount'? If you don't put the 'id' parameter it will say Incomplete input, but if you don't put ammount it will keep processing, and probably cause a bug. Therefore, the right usage is.
While you more play with it, you will learn HOW easy it's to deal with.
After you've defined the parameters and used sscanf to check them, theire automatically READY TO USE!
Now let's make a /report command for our final challenge.
If you don't understand, please don't go further and re-read.
This will make a 'fake' report message, just saying you report someone for a specific reason but it doesn't send a message to admins. Just a example.
Why it says a [128]?. When dealing with sscanf, you need to specify string sizes, or it will return a warning in RCON console and it will spam your console. Also it will have to match with string size declaration.
You can't have this:
Since 64 is not 32.
Also remember you can't use OnPlayerCommandText when you use shis system
That's all for now
I hope you've enjoy and learnt from my tutorial.
Made by Admantis.
Tired of these newbies using strcmp along with strtok. When will they learn? They say it's too hard to learn sscanf2 and zcmd. I thought it was hard too, but I had to focus, take a coffee and look the code. I learnt it in 2 minutes. I will teach you how to use ZCMD as COMMAND PROCESSORS and with sscanf2 to help us to split the parameters.
What we will need?
For this tutorial you need to download the plugin sscanf2 and the include zcmd
Sscanf2: http://forum.sa-mp.com/showthread.ph...hlight=sscanf2
Zcmd: http://forum.sa-mp.com/showthread.ph...highlight=zcmd
There is also a function version of sscanf2, no need to download anything just copy & paste in your gamemode it and sscanf2 is ready to go, but it's slow, probably just slow as strtok so for your own good im not posting it.
Remember zcmd can be implemented in apart functions so they should NOT be inside any callback, so if you are not going to use strcmp to process commands you can delete 'OnPlayerCommandText' as we won't use it
Let's get to work
To make a basic command without parameters, you will first see this.
pawn Код:
COMMAND:heal(playerid, params[]) // 'heal' is the command, so you type /heal and this will be called
{
SetPlayerHealth( playerid, 100.00 ); // Set players health
return true; // Returning a value is VERY important ! Your commands won't work if you don't do so
}
However, if you want to heal other player, the thing gets more complex.
pawn Код:
COMMAND:healplayer(playerid, params[]) // hence we've changed to 'healplayer'
{
new id; // Here we will define ALL our parameters like 'id', 'heal ammount', 'reason', and others.
if (sscanf(params, "u", id)) return SendClientMessage(playerid,0xFFFFFFFF, "Incomplete input");
SetPlayerHealth(id, 100.00);
return 1;
}
pawn Код:
if (sscanf(params, "u", id)) return SendClientMessage(playerid,0xFFFFFFFF, "Incomplete input");
In every command, it should be "if (sscanf(params," untill that point. Then you will need to choose a operator. Where "u" means a playerid OR a part of a name.
We can also use "q" and "r" for player names / ids.
We use "i" or "d" for numbers.
We use "s[size]" for strings.
We use "x" for hexadecimals
We use "f" for floats (health, positions and armour)
More operators can be also found in the official sscanf2 thread.
Also it's important to contain the parameters after the operators.
For example, a common mistake is to copy paste the previous command, and change the functions to make it a different command, but if this command has more parameters it will fail.
Example:
pawn Код:
COMMAND:healplayer(playerid, params[]) // hence we've changed to 'healplayer'
{
new id, Float:ammount; // Here we will define ALL our parameters like 'id', 'heal ammount', 'reason', and others.
if (sscanf(params, "uf", id)) return SendClientMessage(playerid,0xFFFFFFFF, "Incomplete input");
SetPlayerHealth(id, ammount);
return 1;
}
pawn Код:
new id, ammount; // Here we will define ALL our parameters like 'id', 'heal ammount', 'reason', and others.
if (sscanf(params, "uf", id)) return SendClientMessage(playerid,0xFFFFFFFF, "Incomplete input");
pawn Код:
COMMAND:healplayer(playerid, params[]) // hence we've changed to 'healplayer'
{
new id, ammount; // Here we will define ALL our parameters like 'id', 'heal ammount', 'reason', and others.
if (sscanf(params, "uf", id, ammount)) return SendClientMessage(playerid,0xFFFFFFFF, "Incomplete input");
SetPlayerHealth(id, ammount);
return 1;
}
After you've defined the parameters and used sscanf to check them, theire automatically READY TO USE!
Now let's make a /report command for our final challenge.
If you don't understand, please don't go further and re-read.
pawn Код:
COMMAND:report(playerid, params[])
{
new id, reason[128]; // Strings are usually text, and they need a size. Make them 128, NOT 256!
if (sscanf(params, "us[128]", id, reason)) return SendClientMessage(playerid, 0xFFFFFFFF,"report > incorrect usage");
new string[128]; // you should know how to work with strings already
format(string, 128, "report > you've reported playerid %i for %s", id, reason); // we format it
SendClientMessage(playerid, 0xFFFFFFFF, string); // and we send the string
return true;
}
pawn Код:
if (sscanf(params, "us[128]", id, reason)) return SendClientMessage(playerid, 0xFFFFFFFF,"report > incorrect usage");
You can't have this:
pawn Код:
new string[32];
if (sscanf(params, "s[64]")) return 1;
Also remember you can't use OnPlayerCommandText when you use shis system
That's all for now
I hope you've enjoy and learnt from my tutorial.
Made by Admantis.