[Tutorial] How to Write Commands using sscanf & zcmd
#1

Writing Commands using sscanf2 & zcmd


Before we start you must include these two lines in the top of your script:
pawn Код:
#include <a_samp>
#include <sscanf2>
#include <zcmd>
Include download:
sscanf.inc
zcmd.inc

Plugin downloads:
sscanf.dll Windows only!

Now lets start with a simple command.
pawn Код:
COMMAND:help(playerid, params[]) //Command is: help
{
So when a player types /help the above code will start to take place.

Now when a player only types: /Help. Nothing after it (which is called "params") then you may include this line:
pawn Код:
COMMAND:help(playerid, params[]) //Command is: help
{
    #pragma unused params
//Tells the script that there is no text after the "/help" command, (IS OPTIONAL)
Now that we have our beginning ready, time to move on to the main part.
Now we will use this function: to send a message to the player who types: "/help"
pawn Код:
SendClientMessage(playerid, COLOR, "this is the text the player will see");
//Where "playerid" is the player who types "/help"
//"COLOR" is the color which you will need to define in the top of your script.
//"text"); is the text you want the player to see.
Now we will implement "SendClientMessage" in our /help command:
pawn Код:
COMMAND:help(playerid, params[]) //Command is: help
{
    #pragma unused params
//Tells the script that there is no text after the "/help" command
    SendClientMessage(playerid, red, "_________________________Server Help_____________________________");
    SendClientMessage(playerid, green, "[HELP] This is a BLANK server, Work with your team to");
    SendClientMessage(playerid, green, "[HELP] destroy other teams and earn the highest score");
    SendClientMessage(playerid, green, "[HELP] See /admins if you need any help");
    SendClientMessage(playerid, green, "[HELP] For more information on the commands see: /cmds");
    SendClientMessage(playerid, green, "[HELP] /rules - For server rules");
    SendClientMessage(playerid, red, "_________________________________________________________________");
}
And there you go, you have made a simple /help command using zcmd. So far we haven't touched sscanf. This is the 2nd part of the tutorial.
Part 2:
Using sscanf + zcmd to make commands.
Lets start with a /givemoney command.
pawn Код:
CMD:givemoney(playerid, params[])
{
Now as you have noticed i used
pawn Код:
CMD:
instead of
pawn Код:
COMMAND:
They both implement the same idea, but one is faster to write then the other.
pawn Код:
CMD:givemoney(playerid, params[])
{
     new giveplayerid, amount
Now I used two different variables: "giveplayerid" to get the player id of which the player (who types /givemoney) types. And "amount" for the amount of money the player wants to send.
pawn Код:
CMD:givemoney(playerid, params[])
{
     new giveplayerid, amount;
     new string[128];
Now I defined my "string" to 128 bits (can be changed depending on the amount of words/text needed). A string is used if you want to get a specific text into which you don't know what it will be.
pawn Код:
CMD:givemoney(playerid, params[])
{
     new giveplayerid, amount;
     new string[128];
     if (sscanf(params, "ud", giveplayerid, amount)) return SendClientMessage(playerid, 0xFF0000AA, "Usage: /givecash [playerid/partname] [amount]");
Now I gave different parameters to the two texts the player will enter. I used "u" and "d". You can find more information on what types of "data types" there are here.
pawn Код:
CMD:givemoney(playerid, params[])
{
     new giveplayerid, amount;
     new string[128];
     if (sscanf(params, "ud", giveplayerid, amount)) return SendClientMessage(playerid, 0xFF0000AA, "Usage: /givecash [playerid/partname] [amount]");
    else if (giveplayerid == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
    else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, 0xFF0000AA, "Insufficient Funds");
    else if (giveplayerid == playerid) SendClientMessage(playerid, 0xFF0000AA, "You cannot send money to yourself");
Now let me explain each part.
pawn Код:
else if (giveplayerid == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
This says that if the id of the player that the money is going to be sent to, is not a valid id (playerid is not available) then it will send the message "Player Not Found".
pawn Код:
else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, 0xFF0000AA, "Insufficient Funds"
This says that if the money amount you want to send is more then what the player has, then it will send that message.
Now before we go on, let me clarify that "giveplayerid" is the player which the money is being sent to.
And "playerid" is the player who is typing that command.
pawn Код:
else if (giveplayerid == playerid) SendClientMessage(playerid, 0xFF0000AA, "You cannot send money to yourself");
Now if that id you want to send the money to is yourself then it will show that msg.
pawn Код:
CMD:givemoney(playerid, params[])
{
     new giveplayerid, amount;
     new string[128];
     if (sscanf(params, "ud", giveplayerid, amount)) return SendClientMessage(playerid, 0xFF0000AA, "Usage: /givecash [playerid/partname] [amount]");
    else if (giveplayerid == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
    else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, 0xFF0000AA, "Insufficient Funds");
    else if (giveplayerid == playerid) SendClientMessage(playerid, 0xFF0000AA, "You cannot send money to yourself");
    GivePlayerMoney(giveplayerid, amount);
    GivePlayerMoney(playerid, -amount);
    format(string, sizeof string, "[SERVER] %d amount of Money has been Sent to %s", amount, giveplayerid);
    SendClientMessage(playerid, COLOR_RED, string);
    format(string, sizeof string, "[SERVER] %d amount of Money has Sent by %s", amount, playerid);
    SendClientMessage(giveplayerid, COLOR_RED, string);
    return 1;
}
Now I used the Function:
pawn Код:
GivePlayerMoney(giveplayerid, amount);
This will give the money to the players id you entered.
This:
pawn Код:
GivePlayerMoney(playerid, -amount);
Will subtract that amount from you.
pawn Код:
format(string, sizeof string, "[SERVER] %d amount of Money has been Sent to %s", amount, giveplayerid);
    SendClientMessage(playerid, COLOR_RED, string);
This will create a new string(msg) and send it to the player who sent the money.
pawn Код:
format(string, sizeof string, "[SERVER] %d amount of Money has Sent by %s", amount, playerid);
    SendClientMessage(giveplayerid, COLOR_RED, string);
This will send the message to the id who is receiving the money.
pawn Код:
return 1;
This will return the command as true. (Must be needed otherwise script will fail!!)

Please comment and rep.
Also please inform me of any bugs.
Reply
#2

pawn Код:
#pragma unused params
No need to use that.
Reply
#3

Quote:
Originally Posted by gtakillerIV
Посмотреть сообщение
pawn Код:
#pragma unused params
No need to use that.
At times it can be helpful.

P.S. I said its optional.
Reply
#4

bump 2 weeks passed.
Reply
#5

Nice but you said admin commands, I don't see you using any admin command, they are normal commands that anyone can use it, fix this topic please.

P.S: Good work.
Reply
#6

Quote:
Originally Posted by James_Nick
Посмотреть сообщение
Nice but you said admin commands, I don't see you using any admin command, they are normal commands that anyone can use it, fix this topic please.

P.S: Good work.
Thanks
Reply
#7

There are alot of this kind of tutorials, nothing special
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)