strtok??
#1

HEllo, i am looking at the example on samp wiki and i got a few questions..
Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
  new cmd[30];
  new idx;
  cmd = strtok(cmdtext, idx);
 
  if(strcmp(cmd, "/sayhello", true) == 0)
  {
    new tmp[30];
    // assign the id (written by the user) to tmp
    tmp = strtok(cmdtext, idx);
 
    // convert the id to an integer using strval (this is essential)
    // and assign to otherplayer
    new otherplayer = strval(tmp);
 
    if(IsPlayerConnected(otherplayer))
    {
      SendClientMessage(otherplayer, 0xFFFF00AA, "Hi, hello!");
    }
    return 1;
  }
  return 0;
}
Okay... First of all, what exactly does strtok do? i thought it takes a string and searchs it to find the first space, Then it stores whatever is after it, into a string...but that would mean if you used

Код:
* Martin_Smith 20...
then

Код:
tmp = strtok(cmdtext, idx);
would mean that tmp is equal to 20??

If this is true then why does it use


Код:
  new cmd[30];
  new idx;
  cmd = strtok(cmdtext, idx);
at the start...This means when you compare cmd to the command name, it wont match because cmd would be equal to 20? so it is comparing 20 to /sayhello

-Thanks for helping in advance
Reply
#2

I bet you forgot

pawn Код:
strtok(const string[], &index, const seperator[] = " ")
{
    new index2, result[30];

    index2 = strfind(string, seperator, false, index);


    if(index2 == -1)
    {
        if(strlen(string) > index)
        {
            strmid(result, string, index, strlen(string), 30);
            index = strlen(string);
        }
        return result; // This string is empty, probably, if index came to an end
    }
    if(index2 > (index + 29))
    {
        index2 = index + 29;
        strmid(result, string, index, index2, 30);
        index = index2;
        return result;
    }
    strmid(result, string, index, index2, 30);
    index = index2 + 1;
    return result;
}
Reply
#3

You're doing yourself a great disservice by using strcmp/strtok. You should really look into zcmd/sscanf, as its a lot faster / a hell of alot more user friendly.
Reply
#4

https://sampwiki.blast.hk/wiki/Fast_Commands
Start using zcmd and sscanf.
Reply
#5

Okay ill read up on it...if i run into problems ill edit this thread..

Thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)