SA-MP Forums Archive
strtok - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: strtok (/showthread.php?tid=135085)



strtok - abineri - 18.03.2010

After looking at its definition on samp wiki i reallly dont get what strtok dose or means can someone tell me in bassic terms :P i am reallly new at this so i gota know


Re: strtok - Carlton - 18.03.2010

It's used to find a variable after a space. Like a command /ban <playerid>. It will not work if you do /ban5 ( Trying to ban id 5 ). So strtok finds the variable after a space so you can do /ban 5 ( Bans id 5 ). The parameters are explained like this ( in my way ):

const string[], &index
The const string[] is to search a string and find a variable after the space, so like in OnPlayerCommandText, strtok looks for a space ( Like the example above /ban (THE SPACE) 5 ) after the command you put in.

The &index is not something I don't understand 100%, but all I know is that it's used to store a random variable and it's needed for the function to run.

I'll show you a strtok examples.

pawn Код:
public OnPlayerText(playerid, text[]) {
  new ptext[128], rvar;
  ptext = strtok(text, rvar);
  if(strcmp(ptext, "@", true) == 0)
  {
    if(!strlen(ptext)) return SendClientMessage(playerid, MY_COLOR, "@ <Admin chat message>");
  }
}
strlen checks the length of a string. So if strlen is 0 that means it will return. So read closely, I will explain how this worked out.

if(!strlen(ptext))

if = if
!strlen(ptext) = The string text is 0.

So if the string text is 0 then return that message.
Translated
If there's no space between @ and the message, then return that message


Do you understand now?


Re: strtok - abineri - 19.03.2010

Quote:
Originally Posted by Carlton
It's used to find a variable after a space. Like a command /ban <playerid>. It will not work if you do /ban5 ( Trying to ban id 5 ). So strtok finds the variable after a space so you can do /ban 5 ( Bans id 5 ). The parameters are explained like this ( in my way ):

const string[], &index
The const string[] is to search a string and find a variable after the space, so like in OnPlayerCommandText, strtok looks for a space ( Like the example above /ban (THE SPACE) 5 ) after the command you put in.

The &index is not something I don't understand 100%, but all I know is that it's used to store a random variable and it's needed for the function to run.

I'll show you a strtok examples.

pawn Код:
public OnPlayerText(playerid, text[]) {
  new ptext[128], rvar;
  ptext = strtok(text, rvar);
  if(strcmp(ptext, "@", true) == 0)
  {
    if(!strlen(ptext)) return SendClientMessage(playerid, MY_COLOR, "@ <Admin chat message>");
  }
}
strlen checks the length of a string. So if strlen is 0 that means it will return. So read closely, I will explain how this worked out.

if(!strlen(ptext))

if = if
!strlen(ptext) = The string text is 0.

So if the string text is 0 then return that message.
Translated
If there's no space between @ and the message, then return that message


Do you understand now?
Yes thanks very much