SA-MP Forums Archive
getping - 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)
+--- Thread: getping (/showthread.php?tid=569340)



getping - saffierr - 29.03.2015

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.


Re: getping - Evocator - 29.03.2015

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; 
}



Re: getping - CalvinC - 29.03.2015

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);
}



Re: getping - saffierr - 29.03.2015

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.


Re: getping - Threshold - 30.03.2015

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


Re: getping - saffierr - 30.03.2015

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...


Re: getping - CalvinC - 30.03.2015

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.


Re: getping - saffierr - 30.03.2015

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?


Re: getping - CalvinC - 30.03.2015

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.


Re: getping - saffierr - 30.03.2015

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