PDA

View Full Version : getping


saffierr
29/03/2015, 09:01 PM
So, I just randomly wanted to make a /getping CMD. But when I type IG, /getip I get this lol

http://i61.tinypic.com/2a65qb6.png

This is the code : CMD:getping(playerid, params[])
{
new targetplayer;
if(IsPlayerAdmin(playerid))
{
if(sscanf(params, "i", targetplayer))
SendClientMessage(playerid, COLOR_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.

Evocator
29/03/2015, 09:08 PM
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;
}

CalvinC
29/03/2015, 09:16 PM
Use format (http://wiki.sa-mp.com/wiki/Format).
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);
}

saffierr
29/03/2015, 10:05 PM
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.

Threshold
30/03/2015, 03:06 AM
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

saffierr
30/03/2015, 01:20 PM
Got that! thank you.
But, what's the difference between using new targetplayer, string[25]; and using new targetplayer;
new string[25] twice?
You used that , new twice, I did both in the same line...

CalvinC
30/03/2015, 01:30 PM
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.

saffierr
30/03/2015, 01:56 PM
So if I get it;
It'd be better to use the new string above the format?
and the new targetplayer; top of the cmd script?
just the string needs to be above the format right?

CalvinC
30/03/2015, 02:11 PM
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.

saffierr
30/03/2015, 02:42 PM
Ok bro, I got that! thank you for explaining it.

saffierr
30/03/2015, 03:03 PM
But there are a few mistakes in the cmd yet,
CMD:getping(playerid, params[])
{
new targetplayer;
if(IsPlayerAdmin(playerid))
{
if(sscanf(params, "i", targetplayer))
SendClientMessage(playerid, COLOR_ORANGE, "USAGE: /getping [ID]");
if(!IsPlayerConnected(targetplayer)) return SendClientMessage(playerid, COLOR_RED, "Player is not connected!");

new string[25];
format(string, sizeof string, "Players ping is %i", GetPlayerPing(playerid));
SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
}
else
{
SendClientMessage(playerid, -1, "SERVER: Unknown command.");
}
return 1;
}
That's the code now.
But at the very moment I enter /getping, just /getping I get this : http://i57.tinypic.com/2iw9jb4.png
Instead of just getting the message: "USAGE: /getping [ID]" I get my ping at the same time too, I want that to be removed, but I don't know how.

CalvinC
30/03/2015, 03:12 PM
Use return to stop the command.
if(sscanf(params, "i", targetplayer))
SendClientMessage(playerid, COLOR_ORANGE, "USAGE: /getping [ID]");
to
if(sscanf(params, "i", targetplayer))
return SendClientMessage(playerid, COLOR_ORANGE, "USAGE: /getping [ID]");
http://wiki.sa-mp.com/wiki/Control_Structures#return

saffierr
30/03/2015, 03:18 PM
Thank you, it's fixed.