SA-MP Forums Archive
[Include] GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - 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: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] GetPlayerId - accept both player ID's and names in every SA-MP function, including commands (/showthread.php?tid=471812)



GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - Sydcul - 25.10.2013

GetPlayerId
Accept both player ID's and names in every SA-MP function, including commands

It can be very annoying for your players to have to input player IDs in every command, especially because these change with every rejoin, but sometimes it can be really handy to input a player ID, when somebody has a really annoying name, for example. This is where GetPlayerId comes in, a useful include which converts a player's name into a player ID for use in your code. If a player prefers entering a player ID, these are also accepted. The output is INVALID_PLAYER_ID when the name of a player which is not connected is entered.

How to install
It's very easy to install this include.
First of all, download it from the bottom of this post, then place it in your samp-server\pawno\includes folder (of your developing computer).

Then, add this code to the top of your script, at your includes:
Код:
#include <getplayerid>
Let's say you have this code in one of your commands:
Код:
CMD:like(playerid, params[]) {
	if(!IsPlayerConnected(strval(params))) {
		SendClientMessage(playerid, 0xffffffaa, "Invalid player");
	} else {
		SendClientMessage(id, 0xffffffaa, "Somebody liked you!");
	}
}
The above code will only accept player IDs, and will even fail to work correctly when you enter a player name!
You can fix the code by changing it into this:

Код:
CMD:like(playerid, params[]) {
	if(GetPlayerId(strval(params)) == INVALID_PLAYER_ID) {
		SendClientMessage(playerid, 0xffffffaa, "Invalid player");
	} else {
		SendClientMessage(id, 0xffffffaa, "Somebody liked you!");
	}
}
The new code will accept both player IDs AND player names (also with wrong case). It will also never crash.

Download
Pastebin
Dropbox
You can also download the attachment of this post, but please only do that if both of the above mirrors will stop working.


Respuesta: GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - DanDRT - 25.10.2013

i don't like :/


Re: GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - Danny - 25.10.2013

... you can also fix this by using sscanf. It has the same effect, but works much faster and you can use it for more than only this.


Re: GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - PT - 26.10.2013

nice try, but i prefer sscanf...


Re: GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - Sydcul - 26.10.2013

I know you can use sscanf, but this is implementable in every function and it works a lot easier than sscanf (for example when using dialogs).


Re: GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - Jochemd - 21.05.2014

Quote:
Originally Posted by Sydcul
Посмотреть сообщение
I know you can use sscanf, but this is implementable in every function and it works a lot easier than sscanf (for example when using dialogs).
I know - old topic - but why don't you use sscanf in your include then?


Re: GetPlayerId - accept both player ID's and names in every SA-MP function, including commands - RajatPawar - 21.05.2014

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
I know - old topic - but why don't you use sscanf in your include then?
Even though your bump is perfectly valid, you aren't going to get a reply, the OP was last active 4 months ago!

Cheers