tmp = strtok(cmdtext, idx); Help.
#1

Im trying to convert strcmp commands to zcmd.
Could anyone teach me what to do with
Код:
		tmp = strtok(cmdtext, idx);
		if(!strlen(tmp))
Reply
#2

delete this its not needed with zcmd! zcmd is made to write quick commands all of this you need to do with strcmp is included in the .inc file from zcmd you don't have to worry about this anymore
Reply
#3

Out of OnPlayerCommandText like a new function add this:

Код:
CMD:your_command(playerid, params[])
or

Код:
COMMAND:your_command(playerid, params[])
This is an example of the command: "/helpme". Let's add just some message:

pawn Код:
CMD:helpme(playerid, params[])
{
    SendClientMessage(playerid, 0xFFFFFFF, "My first command ^^ ");
    return 1;
}
If you want to use params:

pawn Код:
CMD:heal(playerid, params[])
{
   new
      tmp[20],
      id;
   tmp = strtok(cmdtext, index);
   if (strlen(tmp))
   {
      id = strval(tmp);
      if (IsPlayerConnected(id))
      {
         new
            string[128],
            name1[MAX_PLAYER_NAME],
            name2[MAX_PLAYER_NAME];
         SetPlayerHealth(id, 100.0);
         GetPlayerName(playerid, name1, sizeof  name1);
         GetPlayerName(id, name2, sizeof name2);
         format(string, sizeof string, "Fuiste curado por %s", name1);
         SendClientMessage(id, 0x00FF00AA, string);
         format(string, sizeof string, "Curaste a %s", name2);
         SendClientMessage(playerid, 0x00FF00AA, string);
      }
      else
      {
         SendClientMessage(playerid, 0xFF0000AA, "Jugador no encontrado");
      }
   }
   else
   {
      SendClientMessage(playerid, 0xFF0000AA, "USO: "/heal ID"");
   }
   return 1;
}
Reply
#4

strtok, in a sense, splits a string by a delimiter. In this specific case, strtok will go through cmdtext at the index specified by idx, and when it gets to the first space, it will set idx to that location, and put the string into tmp.

With zcmd, it's not very much different, where you would be doing that in a command like this...
pawn Код:
if(strcmp(cmdtext, "/hi", true, 3) == 0)
{
    new idx = 0, tmp[128];
    tmp = strtok(cmdtext, idx);
    if(!strlen(tmp))
}
In zcmd, it's very similar.
pawn Код:
CMD:hi(playerid, params[])
{
    new idx = 0, tmp[128];
    tmp = strtok(params, idx);
    if(!strlen(tmp))
}
So it's not really too much different, as all it does is split a string into multiple strings by spaces.

Now, just a little tip, I suggest you switch to sscanf (use the search function), it will do this process a lot quicker and make it so much easier to do and read.
With sscanf + zcmd:
pawn Код:
CMD:hi(playerid, params[])
{
    new str[128];
    if(sscanf(params, "s[128]", str)) return SendClientMessage(playerid, -1, "usage message here.");
    // your code etc.
}
And that's just the bottom of sscanf, the thread for sscanf holds a lot more info.

Now, the reason to use zcmd and sscanf is to improve speed and readability of your code, it will greatly help you in the future.

Happy coding!
Reply
#5

Delet it,...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)