getping
#1

So, I just randomly wanted to make a /getping CMD. But when I type IG, /getip I get this lol

[spoiler][/spoiler]

This is the code :
PHP код:
CMD:getping(playeridparams[])
{
   new 
targetplayer;
   if(
IsPlayerAdmin(playerid))
   {
    if(
sscanf(params"i"targetplayer))
    
SendClientMessage(playeridCOLOR_LIGHTBLUE"Players ping is %i");
    
GetPlayerPing(playerid);
   }
     else
   {
        
SendClientMessage(playerid, -1"SERVER: Unknown command.");
   }
   return 
1;

NOTE: I don't want it in /getping [ID], yet. Just want to get my ping first.
Reply
#2

You need to format a string and then send it to a player, wonder how the server did not crash...

Код:
CMD:getping(playerid, params[]) 
{ 
	new targetplayer, string[21]; 
	if(IsPlayerAdmin(playerid)) 
	{ 
    	if(sscanf(params, "i", targetplayer)) return SendClientMessage(playerid, -1, "SERVER: /getping [id]."); 
    	if (!IsPlayerConnected(targetplayer)) return SendClientMessage(playerid, -1, "SERVER: No player found."); 

    	format(string, sizeof (string), "Player's ping is %i", GetPlayerPing(targetplayer));
    	SendClientMessage(playerid, COLOR_LIGHTBLUE, string); 
   	} 
 	else 
   	{ 
		SendClientMessage(playerid, -1, "SERVER: Unknown command."); 
   	} 
   	return 1; 
}
Reply
#3

Use format.
pawn Код:
if(IsPlayerAdmin(playerid))
{
   new string[22];
   if(sscanf(params, "i", targetplayer)) {}
   format(string, sizeof(string), "Players ping is %i", GetPlayerPing(playerid));
   SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
}
Reply
#4

Thank you it is fixed, although it has something I don't like, namely;
When I type /getping, it says
USAGE: /getping [ID] ((that's good)) but it also says
my ping at the same time.
If you didn't understand this, I will explain it further.
Reply
#5

pawn Код:
CMD:getping(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "SERVER: Unknown Command");
    new targetplayer;
    if(sscanf(params, "u", targetplayer)) return SendClientMessage(playerid, -1, "USAGE: /getping [ID]");
    if(!IsPlayerConnected(targetplayer) || targetplayer == INVALID_PLAYER_ID) return SendClientMessage(playerid, -1, "Player is not connected.");
    new str[25];
    format(str, sizeof(str), "Player's ping is %i.", GetPlayerPing(targetplayer));
    SendClientMessage(playerid, COLOR_LIGHTBLUE, str);
    return 1;
}
You should use the "u" parameter when dealing with player names and/or IDs. Read more about SSCANF specifiers here:
https://github.com/Y-Less/sscanf/wiki
Reply
#6

Got that! thank you.
But, what's the difference between using
PHP код:
new targetplayerstring[25]; 
and using
PHP код:
new targetplayer
PHP код:
new string[25
twice?
You used that , new twice, I did both in the same line...
Reply
#7

You created them both at the top of the command.
So it will waste the 100 bytes of memory allocated to "string" if sscanf or IsPlayerConnected stops the command, because then the format isn't used, and there's no reason to allocate 100 bytes of memory.
So creating it where you need it (above format) is more optimised.
Reply
#8

So if I get it;
It'd be better to use the
PHP код:
new string 
above the format?
and the
PHP код:
new targetplayer
top of the cmd script?
just the string needs to be above the format right?
Reply
#9

Yes, because you're using targetplayer in your sscanf check, so have it above the sscanf check.
But you're only using the string to format, so use it above your format.
Reply
#10

Ok bro, I got that! thank you for explaining it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)