[Tutorial] Making simple commands using zcmd and sscanf
#1

Hello guys, This is my first tutorial. Have fun looking at this!
What are we going to see in this tutorial? This tutorial will show you how to make some simple commands using sscanf and zcmd

You can get zcmd.inc by clicking on me.
You can get sscanf.inc by clicking on me.
Shall we start?
First Step.
Let's include the necessary files for making commands.
You need to have sscanf.inc and zcmd.inc files inside your pawno/includes folder. Else, It'll give you an error just like
fatal error 100: cannot read from file: "sscanf" or fatal error 100: cannot read from file: "zcmd"
INCLUDES
pawn Код:
#include <sscanf>
#include <zcmd>
Let's also define some colors.
pawn Код:
#define COLOR_GREEN 0x33AA33AA
#define COLOR_RED 0xFF0000AA
Let's start now.

Код:
At first, I'm going to say how to make a simple /heal and /armour command. (I mean like healing and armouring other players)
Let's see the /heal command now.

pawn Код:
CMD:heal(playerid,params[]) First line.
{ // Opening bracket
   new string[128], ID, name[MAX_PLAYER_NAME], aname[MAX_PLAYER_NAME];  // Here ID is the target, name is your name.(the player who is going to execute it) and aname is the target's name.
   if(sscanf(params,"u",ID)) // "u" can be defined for inserting a name or an ID
   { // then
        SendClientMessage(playerid,COLOR_RED,"[ERROR] /heal (Player Name/ID)"); //When the player didn't enter the fields correctly, It'll say this message to him.
        return 1;
   }
   if(!IsPlayerAdmin(playerid)) // If the player is not an RCON admin
   { // then
        SendClientMessage(playerid,COLOR_RED,"[ERROR] You are not authorized to use this command.");
        return 1;
   }
   if(!IsPlayerConnected(ID)) // If the target ID is not connected
   { // then  
        SendClientMessage(playerid,COLOR_RED,"[ERROR] Entered player ID is not connected.");  
        return 1;
   }
   GetPlayerName(playerid,aname,sizeof(aname)); // Getting the name of the player who executes the command.
   GetPlayerName(ID,name,sizeof(name)); // Getting the name of the target ID.
   format(string,sizeof(string),"[ADMIN] RCON Administrator %s(%d) has healed %s(%d).",aname,playerid,name,ID);
   SendClientMessageToAll(COLOR_GREEN,string); // Sends a message to everyone who is connected.
   SetPlayerHealth(ID,100); // Sets the target ID's health to 100.
   return 1;
} // Closing bracket.
Great! You completed one command.
You can make it short by using it like this
pawn Код:
CMD:heal(playerid,params[])
{
   new string[128], ID, name[MAX_PLAYER_NAME], aname[MAX_PLAYER_NAME];
   if(sscanf(params,"u",ID)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] /heal [player name/id]");
   if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] You are not authorized to use this command.");
   if(!IsPlayerConnected(ID)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] Entered player ID is not connected.");
   GetPlayerName(playerid,aname,sizeof(aname)); // Getting the name of the player who executes the command.
   GetPlayerName(ID,name,sizeof(name)); // Getting the name of the target ID.
   format(string,sizeof(string),"[ADMIN] RCON Administrator %s(%d) has healed %s(%d).",aname,playerid,name,ID);
   SendClientMessageToAll(COLOR_GREEN,string); // Sends a message to everyone who is connected.
   SetPlayerHealth(ID,100); // Sets the target ID's health to 100.
   return 1;
}
Let's see /armour now

pawn Код:
CMD:armour(playerid,params[])
{
   new string[128], ID, name[MAX_PLAYER_NAME], aname[MAX_PLAYER_NAME];
   if(sscanf(params,"u",ID)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] /armour [player name/id]");
   if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] You are not authorized to use this command.");
   if(!IsPlayerConnected(ID)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] Entered player ID is not connected.");
   GetPlayerName(playerid,aname,sizeof(aname)); // Getting the name of the player who executes the command.
   GetPlayerName(ID,name,sizeof(name)); // Getting the name of the target ID.
   format(string,sizeof(string),"[ADMIN] RCON Administrator %s(%d) has armoured %s(%d).",aname,playerid,name,ID);
   SendClientMessageToAll(COLOR_GREEN,string); // Sends a message to everyone who is connected.
   SetPlayerArmour(ID,100); // Sets the target ID's armourto 100.
   return 1;
}
Let's see another command : /me
Let's make this simple

pawn Код:
CMD:me(playerid,params[])
{
    new string[128];
    new name[MAX_PLAYER_NAME];
    if(!strlen(params)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] /me [message]"); // if the player didn't enter the fields it'll say this.
    if(IsSpawned[playerid] != 1) return SendClientMessage(playerid,COLOR_RED,"[ERROR] You must be spawned to use this command."); // if the player is not spawned it'll say this
    format(string,sizeof(string),"%s %s",name,params); // Here the first %s defines the player's name, and the second %s defines the text entered by the player(params)
    SendClientMessageToAll(COLOR_GREEN,string); // Sending a message to all with the color green.
    return 1;
}
Final Command.
Its /kick
pawn Код:
CMD:kick(playerid,params[])
{
   new string[128], name[MAX_PLAYER_NAME], aname[MAX_PLAYER_NAME], ID, cmdreason;
   if(sscanf(params,"us",ID,cmdreason)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] /kick [player name/id] [reason]");
   if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] You are not authorized to use this command.");
   if(!IsPlayerConnected(ID)) return SendClientMessage(playerid,COLOR_RED,"[ERROR] Entered player ID is not connected.");
   GetPlayerName(playerid,aname,sizeof(aname)); // Getting the name of the player who executes the command.
   GetPlayerName(ID,name,sizeof(name)); // Getting the name of the target ID.
   format(string,sizeof(string),"[ADMIN] RCON Administrator %s(%d) has kicked %s(%d) for the reason : %s",aname,playerid,name,ID,cmdreason);
   SendClientMessageToAll(COLOR_GREEN,string);
   Kick(ID);
   return 1;
}
If I'm wrong with that first and fourth command in (if(sscanf(params,"u",ID)), Please let me know it. Since, i'm very much confused with it.

Credits
Me(rakshith122) - For making this tutorial
Y-Less - For sscanf
Zeex - For zcmd
You for looking on this tutorial.


Код:
 If you like this tutorial, Please +rep me. :)
Reply
#2

Good work!!!
Reply
#3

Great job!
Reply
#4

Expecting some comments, 52 views and just 2 replied.
Come on people, Comment.
Reply
#5

Any comments?
Reply
#6

Can you explain it more so newbies can understand this?
Reply
#7

Good Work.. But You Must Try To Explain More..
Reply
#8

Quote:
Originally Posted by rakshith122
Посмотреть сообщение
Any comments?
You shall receive what you ask for...

Quote:

Great! You completed one command.
You can make it short by using it like this

They've completed jack. You've done everything, the viewer will not think on his own and therefore not learn a thing. Furthermore, you show nothing about the use of sscanf and zcmd.

As it is now, it's just two random command snippets, not a tutorial...
Reply
#9

I have quite the same thoughts as the people above, you don't explain well enough how stuff works.
There's small comments on the sides of the script, which is fine, but you should also go in-depth and explain HOW stuff works, and WHY it works like that.
Here's a random snippet from your heal command.
pawn Код:
if(sscanf(params,"u",ID)) // "u" can be defined for inserting a name or an ID
   { // then
        SendClientMessage(playerid,COLOR_RED,"[ERROR] /heal (Player Name/ID)"); //When the player didn't enter the fields correctly, It'll say this message to him.
You do not explain several things here. Etc, what is this
pawn Код:
if(sscanf(params,"u",ID)) // "u" can be defined for inserting a name or an ID
How does it work? Like what does the "u" actually do, and in relation to other characters like "s" or something.
All in all it would be nicer with some more depth in the tutorial, than commands that has short comments barely scratching the surface of what stuff does.

Or even for newbies, what does the "if" do, or "params".
Reply
#10

good tutorial, still-which i think would be more efficient using sscanf slots and fail to declare as much local variable, greetings and good luck.

pawn Код:
CMD:heal(playerid, params[])
{
    if(IsPlayerAdmin(playerid)) // Note: first is to check if you are an administrator.
    {
        if(sscanf(params, "u", params[0])) return SendClientMessage(playerid, -1, "usage: /heal [playerid/name].");
        if(IsPlayerConnected(params[0]) && params[0] != INVALID_PLAYER_ID)
        {
            new g_string[91]; // You don't need 128 in its string.
            SetPlayerHealth(params[0], 100.0);
            format(g_string, sizeof(g_string), "RCON Administrator %s(%d) has healed %s(%d).", Name(playerid), playerid, Name(params[0]), params[0]);
            SendClientMessage(params[0], -1, g_string);
        }
    }
    else
    {
        SendClientMessage(playerid, -1, " You are not authorized to use this command.");
    }
    return true;
}

stock Name(playerid)
{
    new name_player[24];
    GetPlayerName(playerid, name_player, 24);
    return name_player;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)