[Tutorial] Making a command "/skin [ID]" [ZCMD + SSCANF]
#1

Hello guys, this is my very very first tutorial, if this tutorial means a lot to you, remember to +rep me (Not necessary). If I haven't explain anything or it's not clear to you, I'm sorry, I'm not quite good in explaining.

Everything is explained in the lesson!

So, let's start our lesson!
1. What we need to do this command?
pawn Код:
#include <a_samp>
#include <zcmd>
#include <sscanf2>
Question: Why do you need ZCMD and SSCANF but not STRCMP or w/e it is?
Answer: ZCMD is the fastest command processor besides Y_Command, and I use SSCANF so we can make our job easier!

2. Alright, after including the includes, let's start scripting!
pawn Код:
CMD:skin(playerid,params[]) //Our command!
Alright, if you're using new.pwn, remove the "public OnPlayerCommandText" because we don't need it. We just use this instead of using STRCMP.

3. After adding this, we shall start with our skin scripting! Let's add something like a "new"
pawn Код:
new skinid, skinnumber, str[100]; //It's useful after this script.
Question: Why do you add these new's?
Answer: This is because we're going to add some stuff and use it in this command, if we don't use this, the /skin command is gonna be hard to do.

4. Alright, just start with a sentence, maybe.
pawn Код:
if(sscanf(params, "d", skinid)) SendClientMessage(playerid, -1, "USAGE: /skin (skinid)"); //Shows the player usage IF player doesn't type anything after "/skin"
According to sscanf's specifiers:

Specifier(s) Name Example values
b Binary 01001, 0b1100
c Character a, o, *
f Float 0.7, -99.5
g IEEE Float 0.7, -99.5, INFINITY, -INFINITY, NAN, NAN_E
h, x Hex 1A, 0x23
i, d Integer 1, 42, -10
l Logical true, false
n Number 42, 0b010, 0xAC, 045
o Octal 045 12
q Bot name/id ShopBot, 27
r Player name/id ******, 42
u User name/id (bots and players) ******, 0
We can use "d" or "i" because SKINID is a number!

5. Now it's time to do some prevention for the /skin that can make you crash! (If skin ID is below than 0, or above 299!)
pawn Код:
else if(skinid < 0 || skinid > 299) SendClientMessage(playerid, 0xFF000000, "ERROR: You must choose a skin ID between 0 to 299"); //After you doing this, the player can't choose -1, or 300 (or above/below)
Question: Why? Why you do this?
Answer: It's because if you choose a skin below 0, or above 299, it can crash your client (If not wrong).

6. After doing our first part, let's getting deep into the script!
pawn Код:
else {
    SetPlayerSkin(playerid, skinid);
    skinnumber = GetPlayerSkin(playerid);
    format(string, sizeof(string), "INFO: You have changed your skin to %d", skinnumber);
    SendClientMessage(playerid, -1, string);
}
So, let me explain one by one.

6a.
pawn Код:
SetPlayerSkin(playerid,skinid);
Explanation: This will set player's skin to the ID, for example: /skin 167. And the script will respond and change the skin to the ID!

6b.
pawn Код:
skinnumber = GetPlayerSkin(playerid);
Explanation: We're doing this because we're going to make a message and show it to player that the player has change the skin successfully!

6c.
pawn Код:
format(string, sizeof(string), "INFO: You have changed your skin to %d", skinnumber);
Explanation: We're not using SendClientMessage because it contains a string (%d), we must convert the string before we go on to our SendClientMessage!

6d.
pawn Код:
SendClientMessage(playerid, -1, string);
Explanation: Now we're using the "format" and the string has been converted to the ID. (For example: "INFO: You have change your skin to 167")

7. After doing this part, don't forget to do a
pawn Код:
return 1;
Explanation: If you don't do this, the server will show: "SERVER: Unknown command" although you've successfully changed your skin!

EXTRA:
For the lazy guy, who didn't read in this tutorial:
pawn Код:
CMD:skin(playerid,params[])
{
    new skinnumber, skinid, string[128];
    if(sscanf(params, "d", skinid)) SendClientMessage(playerid, -1, "{ffff00}=USAGE=: {ffffff}/skin <skinid>");
    else if(skinid < 0 || skinid > 299) SendClientMessage(playerid, 0xFF000000, "{ff0000}=ERROR=: {ffffff}Choose a skin between 0 to 299!");
    else
    {
        SetPlayerSkin(playerid, skinid);
        skinnumber = GetPlayerSkin(playerid);
        format(string, sizeof(string), "{ffff00}=INFO=: {ffffff}You have changed your skin to %d", skinnumber);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}
Hope this tutorial helps you in your script! +rep me or ask me anything if you don't understand which part, or you can just give me some feedback or criticize so I can improve myself in teaching.

If you came in to copy, don't come in.
If you came in to learn, you're welcome!
Reply
#2

Whatever, it's up to people if they want to read the tutorial or not. I saw the whole tutorial, I didn't see anything wrong at all, but whatever, don't order people

Good job though, and also don't ask for rep, earn it. Rep+
Reply
#3

Nice to see this as your first tutorial. Although I noticed some mistakes, but since this is your first I won't be that rude.
The "new's" are actually called variables. Just to mention it.
And the 6c. is a bit incorrect.
Quote:

Explanation: We're not using SendClientMessage because it contains a string (%d), we must convert the string before we go on to our SendClientMessage!

Actaully it doesn't contain a string, but an integer. (Correct me if i'm wrong )
But don't take it so badly .

Almost forgot it, one more thing that the last why do you have:

pawn Код:
else
    {
        SetPlayerSkin(playerid, skinid);
        skinnumber = GetPlayerSkin(playerid);
        format(string, sizeof(string), "{ffff00}=INFO=: {ffffff}You have changed your skin to %d", skinnumber);
        SendClientMessage(playerid, -1, string);
    }
In my opinion i don't see why you wanted to add the else statement, but (not sure about this) you could just have removed it. It is actually hard to explain in words so i'll just show you the way I would have done this command.

YOURS:
pawn Код:
CMD:skin(playerid,params[])
{
    new skinnumber, skinid, string[128];
    if(sscanf(params, "d", skinid)) SendClientMessage(playerid, -1, "{ffff00}=USAGE=: {ffffff}/skin <skinid>");
    else if(skinid < 0 || skinid > 299) SendClientMessage(playerid, 0xFF000000, "{ff0000}=ERROR=: {ffffff}Choose a skin between 0 to 299!");
    else
    {
        SetPlayerSkin(playerid, skinid);
        skinnumber = GetPlayerSkin(playerid);
        format(string, sizeof(string), "{ffff00}=INFO=: {ffffff}You have changed your skin to %d", skinnumber);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}
MINE:
pawn Код:
CMD:skin(playerid,params[])
{
    new skinnumber, skinid, string[128];
    if(sscanf(params, "d", skinid)) return SendClientMessage(playerid, -1, "{ffff00}=USAGE=: {ffffff}/skin <skinid>");
    if(skinid < 0 || skinid > 299) return SendClientMessage(playerid, 0xFF000000, "{ff0000}=ERROR=: {ffffff}Choose a skin between 0 to 299!");
    SetPlayerSkin(playerid, skinid);
    skinnumber = GetPlayerSkin(playerid);
    format(string, sizeof(string), "{ffff00}=INFO=: {ffffff}You have changed your skin to %d", skinnumber);
    SendClientMessage(playerid, -1, string);
    return 1;
}
It's actually pretty similar to yours, but still a bit different.

Anyway my conclusion would be nice tutorial! Just gonna 'hit' the star today
Reply
#4

Thanks for the feedback, both!

@CrazyChoco, everyone has their own style to do their script, don't take it as offensive language.
Reply
#5

Quote:
Originally Posted by LeeXian99
Посмотреть сообщение
Thanks for the feedback, both!

@CrazyChoco, everyone has their own style to do their script, don't take it as offensive language.
Yeah, Of course. But it was just a little tip . But i don't see anything wrong with your code!
Reply
#6

Isn't there supposed to be a return at before SendClientMessage at the lines of if and else if?

pawn Код:
if(sscanf(params, "d", skinid)) return SendClientMessage(playerid, -1, "{ffff00}=USAGE=: {ffffff}/skin <skinid>");
    if(skinid < 0 || skinid > 299) return SendClientMessage(playerid, 0xFF000000, "{ff0000}=ERROR=: {ffffff}Choose a skin between 0 to 299!");
Reply
#7

Quote:
Originally Posted by Sublime
Посмотреть сообщение
Isn't there supposed to be a return at before SendClientMessage at the lines of if and else if?

pawn Код:
if(sscanf(params, "d", skinid)) return SendClientMessage(playerid, -1, "{ffff00}=USAGE=: {ffffff}/skin <skinid>");
    if(skinid < 0 || skinid > 299) return SendClientMessage(playerid, 0xFF000000, "{ff0000}=ERROR=: {ffffff}Choose a skin between 0 to 299!");
Oops, my bad! Fixed.
Reply
#8

Very good tutorial, but what should i add to make it like admin command and ussage would be /skin <playerid> <skinid>?
Reply
#9

Quote:
Originally Posted by fonetic
Посмотреть сообщение
Very good tutorial, but what should i add to make it like admin command and ussage would be /skin <playerid> <skinid>?
I may make a tut about that
Reply
#10

Quote:
Originally Posted by CrazyChoco
Посмотреть сообщение
YOURS:
pawn Код:
CMD:skin(playerid,params[])
{
    new skinnumber, skinid, string[128];
    if(sscanf(params, "d", skinid)) SendClientMessage(playerid, -1, "{ffff00}=USAGE=: {ffffff}/skin <skinid>");
    else if(skinid < 0 || skinid > 299) SendClientMessage(playerid, 0xFF000000, "{ff0000}=ERROR=: {ffffff}Choose a skin between 0 to 299!");
    else
    {
        SetPlayerSkin(playerid, skinid);
        skinnumber = GetPlayerSkin(playerid);
        format(string, sizeof(string), "{ffff00}=INFO=: {ffffff}You have changed your skin to %d", skinnumber);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}
MINE:
pawn Код:
CMD:skin(playerid,params[])
{
    new skinnumber, skinid, string[128];
    if(sscanf(params, "d", skinid)) return SendClientMessage(playerid, -1, "{ffff00}=USAGE=: {ffffff}/skin <skinid>");
    if(skinid < 0 || skinid > 299) return SendClientMessage(playerid, 0xFF000000, "{ff0000}=ERROR=: {ffffff}Choose a skin between 0 to 299!");
    SetPlayerSkin(playerid, skinid);
    skinnumber = GetPlayerSkin(playerid);
    format(string, sizeof(string), "{ffff00}=INFO=: {ffffff}You have changed your skin to %d", skinnumber);
    SendClientMessage(playerid, -1, string);
    return 1;
}
Both ways can be improved.

pawn Код:
CMD:skin(playerid,params[])
{
    new
        skinid;
   
    if (sscanf(params, "i", skinid)) return SendClientMessage(playerid, 0xFFFF00FF, "=USAGE=: {ffffff}/skin <skinid>");
    if (!(0 <= skinid <= 299) || skinid == 74) return SendClientMessage(playerid, 0xFF0000FF, "=ERROR=: {ffffff}Choose a skin between 0 to 299. ID 74 is also an invalid skin!");
   
    SetPlayerSkin(playerid, skinid);
   
    new
        string[50];
   
    format(string, sizeof (string), "=INFO=: {ffffff}You have changed your skin to %i", skinid);
    SendClientMessage(playerid, 0xFFFF00FF, string);
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)