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=148931)
strtok?? -
Martin_Smith - 19.05.2010
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
Re: strtok?? -
MastahServers - 19.05.2010
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;
}
Re: strtok?? -
Kyosaur - 19.05.2010
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.
Re: strtok?? -
Sergei - 19.05.2010
https://sampwiki.blast.hk/wiki/Fast_Commands
Start using zcmd and sscanf.
Re: strtok?? -
Martin_Smith - 19.05.2010
Okay ill read up on it...if i run into problems ill edit this thread..
Thanks!