[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
#2

i don't like :/
Reply
#3

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

nice try, but i prefer sscanf...
Reply
#5

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).
Reply
#6

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?
Reply
#7

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
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)