[Include] GetPlayerId - accept both player ID's and names in every SA-MP function, including commands
#1

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


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)