/command [x/y/z]- I just cannot make it happen!
#1

Hello. This is probably an extremely dumb question, but I've been coding, compiling, Googling, coding, compiling etc. for hours and I just cannot get a solution to my problem. I am trying to write a command that would execute code 1 on option x etc, like so:

/command x
SendClientMessage(playerid, COLOR_RED, LOL);

/command y
SendClientMessage(playerid, COLOR_RED, ROFL);

I have tried using switch and case and if, else if and else. Can someone provide me with an elegant, working example so I can adapt it to my script? Hopefully not using if, else if and else as I heard that is really inefficient. Cheers.
Reply
#2

Quote:
Originally Posted by TomSlominski
Посмотреть сообщение
Hello. This is probably an extremely dumb question, but I've been coding, compiling, Googling, coding, compiling etc. for hours and I just cannot get a solution to my problem. I am trying to write a command that would execute code 1 on option x etc, like so:

/command x
SendClientMessage(playerid, COLOR_RED, LOL);

/command y
SendClientMessage(playerid, COLOR_RED, LOL);

I have tried using switch and case and if, else if and else. Can someone provide me with an elegant, working example so I can adapt it to my script? Hopefully not using if, else if and else as I heard that is really inefficient. Cheers.
If you don't have strtok or sscanf, there is the very basic way to get a param:

pawn Код:
// At the start of the command
new param = cmdtext[POS]; // Change POS, --- Example: "/hello 1" is lenght 7 (start from pos 0)
if(param == 'x')
{
    //
}
else if(param == 'y')
{
    //
}
That will work only for the first character after "/hello ". If you want detect a word, you might use strtok or sscanf
Reply
#3

A long time ago, I would suggest you strtok, but that is really unefficent, but if you want to extract and determine the parameters in a command I guess your best solution is zcmd, where you can make use of it's splendid "params" argument.

pawn Код:
CMD:command( playerid,params[] ) // Standard ZCMD command, called when a player types "/command"
{

    if ( !strlen ( params ) ) /* params is null */ return 1;   
    if ( strcmp ( params, "x", true ) == 0 ) /* if the params match with "x" */
    {
        SendClientMessage( playerid, 0xffffffff, "LOL");   
    }
    else if ( strcmp ( params, "y", true ) == 0 ) /* else if the params match with "y" */
    {
        SendClientMessage( playerid, 0xffffffff, "OMG");
    }
    return true; /* return a value, very important! */
}
There is a vast ammount of tutorials regarding the usage of these systems, and by the way, you were kinda cheated, the "else , else if" structure is not unefficent, and the perfomance difference between a switch statement and a else else if statement is really close to null.

You can't use a switch & case statement with the strcmp function.

To finish, if you want more advanced arguments and parameters, with a large ammount of them, you can recuur to sscanf there are fairly enough tutorials regarding on how to use these systems.
Reply
#4

Cheers for that, admantis' solution looks great. Added you a +1 to reputation
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)