[Tutorial] Simple ZCMD Commands! (Using sscanf)
#1

Simple ZCMD Commands (Using sscanf)

Firstly, the plugins and includes you will need and that we will be using are listed below:

(Installation instructions on detailed on their threads)
Now you have the includes and plugins. Let's begin.

You'll have to include both of the includes you have just downloaded.
(Don't forget to include a_samp)

pawn Код:
#include <a_samp>
#include <zcmd>
#include <sscanf2>
For the purpose of this tutorial, I am only going to use two colours. You'll have to define them.

pawn Код:
#define RED 0xFF0000AA
#define YELLOW 0xFFFF00AA

The first command we're going to create is a simple announce command. It'll display some text to all of the players currently playing in your server for a set amount of time.

pawn Код:
CMD:announce(playerid, params[])//This is the command.
{
        new text[128];
    if(sscanf(params,"s[128]",text)) return SendClientMessage(playerid, RED, "(([!]Syntax:/announce[text]))");
    GameTextForAll(text, 10000,0);
    return 1;//Here we return the command.
}//Closing the brackets end the command.
Now, Let's break it down.

pawn Код:
if(sscanf(params,"s",params)) return SendClientMessage(playerid, RED, "(([!]Syntax:/announce[text]))");
Firstly, This if statement is telling the script that if the parameters are left blank then it should send a message to the player executing the command telling him how to execute it correctly. The 's' in "s[128]" is called a specifier. It specifies that the text in the parameters is a string. The 128 is the length of the said string.

pawn Код:
GameTextForAll(text, 10000,0);
This will send the text you have put into command and display it to everyone who is currently connected to your server. It will stay on their screen for 10000 milliseconds. The 0 is the GameText Type. For different types,
Visit - GameText Styles

Now that we have finished that simple announcement command. Lets move on to something different.

pawn Код:
CMD:ooc(playerid, params[])
{
    new text[128], str[128];
    if(sscanf(params,"s[128]",text)) return SendClientMessage(playerid, RED, "(([!]Syntax:/ooc[text]))");
    format(str, 128, "((%s:%s))", PlayerName(playerid), text);
    SendClientMessageToAll(YELLOW, str);
    return 1;//Now we return our command.
}//Close the brackets.
And again, We will break it down. So you actually understand it.

pawn Код:
stock PlayerName(playerid)
{
    new pName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, pName, sizeof(pName));
    return pName;
}
Add this stock at the bottom of your gamemode. It's creating a stock function. All it will do is retrieve the name of the players, So then we don't have to write the same line over and over again.

pawn Код:
new text[128], str[128];
We create two new variables. A Text variable and a string. They're both 128 characters in length.

pawn Код:
if(sscanf(params,"s[128]",text)) return SendClientMessage(playerid, RED, "(([!]Syntax:/ooc[text]))");
As you may noticed, sscanf is a useful tool when producing commands. Again, It's telling the script to
send us a message if the parameters are not filled correctly. The "s[128]" specifies it's a string and it's 128 characters long, just like before.

pawn Код:
format(str, 128, "((%s:%s))", PlayerName(playerid), text);
Here we format the string. This is the format of which the information you have put into the command will be displayed. The "((%s:%s)) part of the line is specifying two string variables. We then define them. One of them is defined as a player name and the other is defined as the text.

pawn Код:
SendClientMessageToAll(YELLOW, str);
This will send the message to all the players logged in. The color is yellow, Which we have previously defined earlier in this tutorial. str is the string we previously formatted.

Now, Lets move on to toggling the OOC Chat channel on and off. This can come in handy when you do not want people spamming.

pawn Код:
CMD:toggleooc(playerid, params[])//This command will utilize a new variable.
{
    if(toggleooc == false)//This is the variable.
    {
        SendClientMessageToAll(YELLOW, "((The OOC Chat Channel Has Been Activated!))");
        toggleooc = true;
    }
    else if(toggleooc == true)
    {
        SendClientMessageToAll(YELLOW, "((The OOC Chat Channel Has Been Disabled!))");
        toggleooc = false;
    }
    return 1;//we return the command
}//Close the brackets.
Now we explain.

Firstly, You need to add this to the top of your script.

pawn Код:
new bool:toggleooc = false;
This is a bool. A bool can only ever be true or false. That makes it perfect for what we're doing. We set the bool to false every time the server starts.

pawn Код:
if(toggleooc == false)//This is the variable.
This if statement is telling the script that if the variable is set to false, then the OOC Channel is to be activated.

pawn Код:
SendClientMessageToAll(YELLOW, "((The OOC Chat Channel Has Been Activated!))");
toggleooc = true;
Now, When it activates. We send a message to all the online players, telling them that it is activated and now accessible. Once it's activated, We need to set the variable to true. So we can toggle it off again.

pawn Код:
else if(toggleooc == true)
Now we check if it's set to true already, Meaning it's activated. Meaning the channel is set to be de-activated.

pawn Код:
SendClientMessageToAll(YELLOW, "((The OOC Chat Channel Has Been Disabled!))");
      toggleooc = false;
So we send a message, notifying every player online and now set it back to false so you can toggle it back on when you feel the need.

Now we have the toggle feature scripted. We now need to implement it into our current command.

pawn Код:
CMD:ooc(playerid, params[])
{
        new text[128];// We define a new text variable with the size of 128.
    if(toggleooc == false)//We can use the variable we created earlier, and utilize it so that we can make the OOC
    //Chat channel unavailable when toggled off.
    {
        SendClientMessage(playerid, RED, "((The OOC Chat Channel is not activated!))");
        return 1;
        //When it's unavailable. The script will send this message to the player trying to use the command.
    }
    if(toggleooc == true)//When it's available however, You can continue to script the command as normal.
    {
        if(sscanf(params, "s[128]",text)) return SendClientMessage(playerid, RED, "(([!]Syntax:/ooc[text]))");
        SendClientMessageToAll(YELLOW,text);
        return 1;
    }
    return 1;//Return the command
}//Close the brackets
I'll explain the changes.

pawn Код:
if(toggleooc == false)
We can use the variable we created earlier, and utilize it so that we can make the OOC Chat channel unavailable when toggled off.

pawn Код:
SendClientMessage(playerid, RED, "((The OOC Chat Channel is not activated!))");
        return 1;
When it's unavailable. The script will send this message to the player trying to use the command.

pawn Код:
if(toggleooc == true)
    {
        if(sscanf(params, "s[128]",text)) return SendClientMessage(playerid, RED, "(([!]Syntax:/ooc[text]))");
        SendClientMessageToAll(YELLOW,text);
        return 1;
    }
When it's available however, You can continue to script the command as normal.

Unnecessary Extras

If you do not want everyone to be able to toggle your OOC command. You can add this line.

pawn Код:
if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, RED, "(([!]You cannot access this command as you're not admin!))");

if(IsPlayerAdmin(playerid))
{
     Command here...
}
This will make it so only people logged into the RCON can access the command.

Also, Some command shortcuts can be made:

pawn Код:
CMD:o(playerid, params[])
{
    return cmd_ooc(playerid, params);
}

CMD:an(playerid, params[])
{
        return cmd_announce(playerid, params);
}
The "return cmd_announce(playerid, params);" is telling the script it's the same command under a different name.

I hope this tutorial was helpful to you new scripters.

Credits

a_samp by the SA-MP team.
ZCMD - The command processor used. Created by Zeex.
sscanf - Created by ******
This tutorial - Created by Keyhead
Reply
#2

Nice job .
But its kinda useless for newbies
Reply
#3

Good tutorial explaining it, out of pure interest though, do you use any other programming languages?
Reply
#4

If they have actually read through it. They should have a basic understanding of how to use the things i've shown them.

Quote:
Originally Posted by DanishHaq
Посмотреть сообщение
Good tutorial explaining it, out of pure interest though, do you use any other programming languages?
I've fiddled around with HTML, CSS and what not, I don't have extensive knowledge in either of them though. I do it for fun mostly. Just messing around.
Reply
#5

i think there is an mistake in the end.

pawn Код:
if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, RED, "(([!]You cannot access this command as you're not admin!))");

if(IsPlayerAdmin(playerid))
{
     Command here...
}
you forgot to add '!' . which checkes whether he isn't an admin

Anyway Good Job
Reply
#6

It's a good tutorial but you chose 2 commands that can be done without sscanf.

For example, in /announce command you used an array of size 128 when it's not needed at all and in the /ooc command you used 2 arrays of size 128 when 1 is enough!

pawn Код:
CMD:announce( playerid, params[ ] )
{
    if( isnull( params ) ) return SendClientMessage( playerid, RED, "(([!]Syntax:/announce[text]))" );
    GameTextForAll( params, 10000, 0 );
    return 1;
}

CMD:ooc( playerid, params[ ] )
{
    if( isnull( params ) ) return SendClientMessage( playerid, RED, "(([!]Syntax:/ooc[text]))" );
    new
        str[ 128 ]
    ;
    GetPlayerName( playerid, str, MAX_PLAYER_NAME );
    format( str, sizeof( str ), "((%s:%s))", str, params );
    SendClientMessageToAll( YELLOW, str );
    return 1;
}
Reply
#7

i found now how to use "isnull" xD.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)