Scripting questions
#1

Hello, I'm learning scripting and everyday, I'm discovering more and more complicated things. I need the answer of few questions, mayve stupid questions, but I really need answers so I can Continue!
I'll give 2 examples a question for each example: here is a /me cmd
pawn Код:
if(!strcmp(cmdtext, "/me", true, 3)) // 3 is the length of /me
    {
        if(!cmdtext[3])return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
        new str[128];
        GetPlayerName(playerid, str, sizeof(str));
        format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
        SendClientMessageToAll(0xFFFF00AA, str);
        return 1;
    }
now "cmdtext" is an array right? And what does if (!cmdtext[3]) mean? I always want to translate the code to english, so everything gets clear to me. For example there's GetPlayerName(playerid, str, sizeod(str)); which means that str contains the playername, then format(str, sizeof(str), "* %s %s", str, cmdtext[4]); so str is the output: What I understood from this : Str recieves Str + cmdtext[4] so we will get a string containing the player name and cmtext[4]..what is cmdtext[4] mean? if cmdtext is an array cmdtext[4] should be the character right after "/me", looks stupid but I need an explanation.

the second example is :
pawn Код:
CMD:heal(playerid, params[])
{
    new
        id;
    if (sscanf(params, "u", id)) SendClientMessage(playerid, 0xFF0000AA, "Usage: \"/heal <playerid>\"");
    else if (id == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
    else
    {
        SetPlayerHealth(id, 100.0);
        SendClientMessage(id, 0x00FF00AA, "You have been healed");
        SendClientMessage(playerid, 0x00FF00AA, "Player healed");
    }
    return 1;
}
what does if (sscanf(params, "u", id) mean? I understood that sscanf gets the input which is a string and checks it, the "u" is the type wanted in the command and the id is the output?So it checks if the "params" is a player ID or not? So confusing,
id is just a normal variable, when I /heal "string" , how does pawno know that id should recieve the "string"? As I see in the script, id is considered as the text that comes after the /heal!
Take it easy please :P I know it looks stupid
Reply
#2

Quote:
Originally Posted by Sarra
Посмотреть сообщение
Hello, I'm learning scripting and everyday, I'm discovering more and more complicated things. I need the answer of few questions, mayve stupid questions, but I really need answers so I can Continue!
I'll give 2 examples a question for each example: here is a /me cmd
pawn Код:
if(!strcmp(cmdtext, "/me", true, 3)) // 3 is the length of /me
    {
        if(!cmdtext[3])return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
        new str[128];
        GetPlayerName(playerid, str, sizeof(str));
        format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
        SendClientMessageToAll(0xFFFF00AA, str);
        return 1;
    }
now "cmdtext" is an array right? And what does if (!cmdtext[3]) mean? I always want to translate the code to english, so everything gets clear to me. For example there's GetPlayerName(playerid, str, sizeod(str)); which means that str contains the playername, then format(str, sizeof(str), "* %s %s", str, cmdtext[4]); so str is the output: What I understood from this : Str recieves Str + cmdtext[4] so we will get a string containing the player name and cmtext[4]..what is cmdtext[4] mean? if cmdtext is an array cmdtext[4] should be the character right after "/me", looks stupid but I need an explanation.

the second example is :
pawn Код:
CMD:heal(playerid, params[])
{
    new
        id;
    if (sscanf(params, "u", id)) SendClientMessage(playerid, 0xFF0000AA, "Usage: \"/heal <playerid>\"");
    else if (id == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
    else
    {
        SetPlayerHealth(id, 100.0);
        SendClientMessage(id, 0x00FF00AA, "You have been healed");
        SendClientMessage(playerid, 0x00FF00AA, "Player healed");
    }
    return 1;
}
what does if (sscanf(params, "u", id) mean? I understood that sscanf gets the input which is a string and checks it, the "u" is the type wanted in the command and the id is the output?So it checks if the "params" is a player ID or not? So confusing,
id is just a normal variable, when I /heal "string" , how does pawno know that id should recieve the "string"? As I see in the script, id is considered as the text that comes after the /heal!
Take it easy please :P I know it looks stupid
k ill try to explain im using mobile but ill fo my best xD in the second example the zcmd command tge sscanf u checks if after you type the cmd you type a number the id an integer get me? in the first one the cmdtext is like the sscanf it checks if u type sth after the me to put it in the format if no it send the usage client message get me
Reply
#3

Quote:
Originally Posted by Sarra
Посмотреть сообщение
Hello, I'm learning scripting and everyday, I'm discovering more and more complicated things. I need the answer of few questions, mayve stupid questions, but I really need answers so I can Continue!
I'll give 2 examples a question for each example: here is a /me cmd
pawn Код:
if(!strcmp(cmdtext, "/me", true, 3)) // 3 is the length of /me
    {
        if(!cmdtext[3])return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
        new str[128];
        GetPlayerName(playerid, str, sizeof(str));
        format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
        SendClientMessageToAll(0xFFFF00AA, str);
        return 1;
    }
now "cmdtext" is an array right? And what does if (!cmdtext[3]) mean? I always want to translate the code to english, so everything gets clear to me. For example there's GetPlayerName(playerid, str, sizeod(str)); which means that str contains the playername, then format(str, sizeof(str), "* %s %s", str, cmdtext[4]); so str is the output: What I understood from this : Str recieves Str + cmdtext[4] so we will get a string containing the player name and cmtext[4]..what is cmdtext[4] mean? if cmdtext is an array cmdtext[4] should be the character right after "/me", looks stupid but I need an explanation.
- cmdtext is a string.

- if (!cmdtext[3]) means if there is no character after the 2nd place (computers start at 0!), it will send a usage message.

-cmdtext[4] will return the cmdtext starting from that number in the brackets.

Quote:
Originally Posted by Sarra
Посмотреть сообщение
the second example is :
pawn Код:
CMD:heal(playerid, params[])
{
    new
        id;
    if (sscanf(params, "u", id)) SendClientMessage(playerid, 0xFF0000AA, "Usage: \"/heal <playerid>\"");
    else if (id == INVALID_PLAYER_ID) SendClientMessage(playerid, 0xFF0000AA, "Player not found");
    else
    {
        SetPlayerHealth(id, 100.0);
        SendClientMessage(id, 0x00FF00AA, "You have been healed");
        SendClientMessage(playerid, 0x00FF00AA, "Player healed");
    }
    return 1;
}
what does if (sscanf(params, "u", id) mean? I understood that sscanf gets the input which is a string and checks it, the "u" is the type wanted in the command and the id is the output?So it checks if the "params" is a player ID or not? So confusing,
id is just a normal variable, when I /heal "string" , how does pawno know that id should recieve the "string"? As I see in the script, id is considered as the text that comes after the /heal!
Take it easy please :P I know it looks stupid
sscanf is a string splitter. In this command, it is splitting the "params" into different data types which, in this case, is "u". The values are stored into the variables, in this case: "id". Look for the other specifiers on https://github.com/Y-Less/sscanf/wiki.

If you typed in a string, it wouldn't work because the specifier isn't a string, which has the specifier of "s". The storing variable is also not a string.
Reply
#4

Yes, cmdtext is an array (you can call it a string). Everything that is written after "/" is stored in that array.
"if(!cmdtext[3])" will check if player wrote just "/me" because after "me" there is a null, or the end of the string.
And yeah, cmdtext[4] would be "/me(space)(first letter)", i.e. "/me s(hots the player)"
Reply
#5

thanks +rep for all
Reply
#6

pawn Код:
if(!strcmp(cmdtext, "/me", true)) // 3 is the length of /me
    {
        if(!cmdtext[4]) return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
        new str[128];
        GetPlayerName(playerid, str, sizeof(str));
        format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
        SendClientMessageToAll(0xFFFF00AA, str);
        return 1;
    }
It should really start from the 4th character, because players could simply do "/me " and the command would still be processed.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)