STRTOK issue
#1

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	new cmd[128], idx;
	cmd = strtok(cmdtext, idx);
 
	if(strcmp(cmd, "/kick", true) == 0)
	{
		new tmp[128];
		tmp = strtok(cmdtext, idx);
 
		if(strlen(tmp) == 0)
	{
		SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /kick [playerid]");
 	}
		Kick(strval(tmp));
		return 1;
	}
	return 0;
}
do I need to create the var tmp all the time in command or can I create tmp as a global variable.
Reply
#2

You Most To Do every commands the var commands like tmp!
you cannot do this Globlay!

and i suggest you to do your Command like this:

Код:
if(strcmp(cmd, "/Kick", true) == 0)
{
  new tmp[256];
  tmp = strtok(cmdtext, idx);
  
   if(!strlen(tmp)) return SendClientMessage(playerid,COLOR_RED,"Usage: /Kick [playerid]!");

  new id;
  id = strval(tmp);
  
  if(!IsPlayerConnected(id)) return SendClientMessage(playerid,COLOR_RED,"Invaild id!");
  
  Kick(id);
  return 1;
}
Hope I Help you! =]
Reply
#3

Okay so with much time I have created it (my way)
I used TMP globally and I found out that the cause was that, so now the command atleast works, but there is one more problem:

I globally defined the GetPlayerName and at the top and the name ID vars etc...so it is like this:

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
new cmd[256];
new idx;
cmd = strtok(cmdtext, idx);
new playername[MAX_PLAYER_NAME];
new pname[18];
new target[18];
new targetid = strval(tmp);
new string[128];
new reason[128];
new ipvar1[16];
new ipvar[16];
GetPlayerName(playerid, pname, sizeof(pname));
GetPlayerName(targetid, target, sizeof(target));
and then after the /register and /login commands:

Код:
	if(strcmp(cmd, "/kick",true) == 0)
			{
			if(PlayerInfo[playerid][pAdmin] > 1)
			{
		 	new tmp[65];
			tmp = strtok(cmdtext, idx);
			reason = bigstrtok(cmdtext, idx);
			if(!strlen(tmp))
			{
			SendClientMessage(playerid, COLOR_WHITE, "USAGE: /kick [playerid] [reason]");
			return 1;
			}
			if(targetid == playerid)
			{
			SendClientMessage(playerid, COLOR_LIGHTRED, "Selecting your Own ID might be messy");
			}
			else if(IsPlayerConnected(targetid) == 0)
			{
			SendClientMessage(playerid, COLOR_LIGHTRED, "Player is not connected");
			}
			else
			{
			GetPlayerIp(playerid, ipvar1, 16);
			GetPlayerIp(targetid, ipvar, 16);
			format(string, sizeof(string), "%s(%d) has been kicked by administrator %s | Reason: %s", target, targetid, pname, reason);
			SendClientMessageToAll(COLOR_YELLOW, string);
			format(string, sizeof(string), "You kicked %s", target);
			SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
			SendClientMessage(targetid, COLOR_LIGHTRED, "You were kicked!");
			printf("%s(%d)[%d] kicked %s(%d)[%d] | Reason: %s", pname, playerid, ipvar1, target, targetid, ipvar, reason);
			Kick(targetid);
			}
	}
}
Don't worry bout the PlayerInfo stuff, I have already defined that etc... I just want to know if there is an easier way to use the GetPlayerName globally without having to type it out all the time. because no there is no TMP to support the above statement..
I also tried defining the variable
Код:
new targetid = strval(tmp);
as
Код:
new targetid = strval(strtok(cmdtext, idx));
but it seems to not work...so..is there any other way to get a globally defined variable in this case?


Reply
#4

Quote:
Originally Posted by [LorD
bAr[Sp] ]
You Most To Do every commands the var commands like tmp!
you cannot do this Globlay!

and i suggest you to do your Command like this:

Код:
if(strcmp(cmd, "/Kick", true) == 0)
{
  new tmp[256];
  tmp = strtok(cmdtext, idx);
  
  if(!strlen(tmp)) return SendClientMessage(playerid,COLOR_RED,"Usage: /Kick [playerid]!");

  new id;
  id = strval(tmp);
  
  if(!IsPlayerConnected(id)) return SendClientMessage(playerid,COLOR_RED,"Invaild id!");
  
  Kick(id);
  return 1;
}
Hope I Help you! =]
Why are you saying that? What's that matter if you define 'id' as strval(something) while you can use this strval and have less lines.
Reply
#5

Quote:
Originally Posted by /^We(stie|z+[e|a
r)$/ ]
Just so you know, the maximum input and input string length is 128, which means in basically every example is wasting half of its memory on unallocated/unused cells.

Because SA-MP (or rather PAWN) is event driven scripting (IE, the OnCommandText callback), you can effectively make every single variable global without the risk of the variables overwriting one another.
Not really... It isn't the maximum. It would be better than other cells.


And why not using ReturnUser?
Reply
#6

There's no reason to use the tmp as 256 or 128 cell strings since there's no any text to abuse these cells.
Reply
#7

Right, I see a way to not waste memory there then, thanks.
Reply
#8

Still, you can use ReturnUser..

pawn Код:
if(!strcmp(cmd,"/kick",true))
{
  new cmd[128];
  cmd=strtok(cmdtext,idx);
  if(!strlen(cmd))
    return SendClientMessage(playerid,color,"usage: /kick [id]");
  new id=ReturnUser(cmd);
  if(!IsPlayerConnected(id))
    return SendClientMessage(playerid,color,"invalid playerid");
  return Kick(id);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)