strtok?
#1

Hi, I am kinda new to pawn but I understand most of the scripting basics from the wiki.
Could anyone post some code here with comments to explain strtok better? I can't understand the wiki "using strcmp()" wich also describes strtok.
Thanks.
Reply
#2

I learned how strtok works looking at the definition and usage and so...
Reply
#3

Quote:
Originally Posted by ¤Adas¤
I learned how strtok works looking at the definition and usage and so...
... use the wiki
Reply
#4

I used the wiki, I mentioned in the message above I didn't understand all of it. Thats why I placed this request. A link to some better explaination is also ok. Thnx
Reply
#5

I fixed the problem myself, I understand it better now. For everyone who has the same problem https://sampwiki.blast.hk/wiki/Strtok
Reply
#6

Although the example given there is weird, as a simple

new targetid = strval(cmdtext);

would do the exact same thing..
Reply
#7

I just made a /ban command wich I fully understand
Have a look
Quote:

public OnPlayerCommandText(playerid, cmdtext[])
{
new cmd[128], idx;
cmd = strtok(cmdtext, idx);

if(strcmp(cmd, "/ban", true) == 0 )
{
(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You need to be Admin to use /ban");
tmp = strtok(cmdtext, idx);
if(strlen(tmp)) return SendClientMessage(playerid, 0xFFFFFFFF, "Usage /ban [playerid]");
pid = strval(tmp)
if(!IsPlayerConnected(pid)) return SendClientmessage(playerid, 0xFFFFFFFF, "Player offline");
Ban(pid);
return 1;


}
return 0;
}

Good?
Reply
#8

Well, strtok is mostly used when youґve got more params, like /ban ID REASON.

Your command would work aswell, BUT a lot of easier and nicer method would be this:

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{  
  if(strcmp(cmd, "/ban", true) == 0 )
  {
   new pid = strval(cmdtext);
   if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xFFFFFFFF, "You need to be Admin to use /ban");
   if(!IsPlayerConnected(pid)) return SendClientmessage(playerid, 0xFFFFFFFF, "Player offline");
   Ban(pid);
   return 1;
     
  }
  return 0;
}
And please, once again - using DCMD is much easier, because you dont need any strtok at all there.
Reply
#9

Yeh, That code of yours is much easier. I will try dcmd too.
Reply
#10

The easiest way is zcmd + sscanf:

pawn Код:
COMMAND:ban(playerid,params[])
{
    if(IsPlayerAdmin(playerid))
    {
      new targetid;
      if(sscanf(params,"u",targetid)) SendClientMessage(playerid,0xFFFFFF,"/ban playerid reason");
      else if(targetid == INVALID_PLAYER_ID) SendClientMessage(playerid,0xFFFFFF,"Invalid player ID");
      else
      {
          SendClientMessage(playerid,0xFFFFFF,"Player banned");
          Ban(targetid);
      }
    }
    else SendClientMessage(playerid,0xFFFFFF,"You are not admin");
    return 1;
}
Using strval to get player ID is bad way since iif you write anything instead of type integer it will return 0 and innocent palyer will be banned. Use ReturnUser or sscanf instead.

And DCMD is just command processor (like zcmd) and has nothing to do with strtok. As replacement for strtok you can use sscanf.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)