Question about Name's and getting them
#1

So is this better

PHP код:
new playerName[MAX_PLAYERS][MAX_PLAYER_NAME];
public 
OnPlayerConnect(playerid)
{
    
GetPlayerName(playeridPlayer_Name[playerid], MAX_PLAYER_NAME);
    return 
1;
// Don't mind not reseting it I know just giving an example 
Or

PHP код:
stock GetName(playerid)
{
    new 
Nick[MAX_PLAYER_NAME];
    
GetPlayerName(playeridNicksizeof(Nick));
    return 
Nick;

Command Example with GetName
PHP код:
CMD:kick(playeridparams[])
{
    new
        
id,
        
reason[100];
    if (
sscanf(params"rs[100]"idreason)) return // syntax..
    // ...
    
new
        
string[128],
        
ip[16];
    
GetPlayerIp(idip16);
    
format(stringsizeof (string), "You have been kicked by %s. Reason: %s"GetName(playerid), reason);
    
SendClientMessage(id, -1string);
    
format(stringsizeof (string), "You kicked %s. Reason: %s"GetName(id), reason);
    
SendClientMessage(playerid, -1string);
    
format(stringsizeof (string), "%s kicked %s. Reason: %s"GetName(playerid), GetName(id), reason);
    
SendClientMessageToAll(id, -1string);
    
format(stringsizeof (string), "%s kicked %s [IP: %s]. Reason: %s"GetName(playerid), GetName(id), ipreason);
    
SendMessageToAdmins(-1string);
    
    
// kick player..
    
return 1;

In this command would the first example be better or the second one?
Reply
#2

Better more if you use a anum for player data storage and get his name only once when connecting, it is better this way of course rather than using resources everytime to get a guy's name.
Of course if you want to allow players to change nick in game you will need some editings, but yeah that is

EDIT: I have to edit this.

Quote:
avoid enumerated arrays because they are not arrays
they work, but they don't adhere to the same rules as actual arrays
so to save confusion, just use a variable
and, if you're doing modules (which you should in 2018 ) use static instead of new to restrict the scope of the variable to that module only
then provide access to it via functions

so, the answer is
PHP код:
static Name[MAX_PLAYERS][MAX_PLAYER_NAME 1
Thanks to who explained me this.
Reply
#3

Quote:
Originally Posted by v1k1nG
Посмотреть сообщение
Better more if you use a anum for player data storage and get his name only once when connecting, it is better this way of course rather than using resources everytime to get a guy's name.
Of course if you want to allow players to change nick in game you will need some editings, but yeah that is
he actually asked which is better?

well 2nd one is better, because u dont have to type
getplayername and u will not have to make them 2 times like for id different and for playerid
so 2nd is better
Reply
#4

The most optimized is the variable, for sure.

And i prefer it too.
pawn Код:
//  OnPlayerDeath(...)
format(string, sizeof string, "%s(%d) killed %s(%d) with a %s.", playerName[killerid], killerid, playerName[playerid], playerid, GetWeaponNameFromID(reason);
Reply
#5

Код:
new playerName[MAX_PLAYERS][MAX_PLAYER_NAME]; 

public OnPlayerConnect(playerid) 
{ 
    GetPlayerName(playerid, Player_Name[playerid], MAX_PLAYER_NAME); 
    return 1; 
} // Don't mind not reseting it I know just giving an example
This will be more efficient as said in previous comments, Why? You're calling GetPlayerName once and playerName[playerid] is always availble for you to use when you need it. If you rather use a method to get a player name, you can always extend what's above with:

Код:
GetName(playerid)
{
	return playerName[playerid];
}
which you can use everywhere

Код:
 printf("Hello %s", GetName(playerid));
Reply
#6

I edited my post
Reply
#7

Using a variable is the same as using the native function, it's the same process except with a local variable, you're not fetching the string into a new local variable each time it's used. Either way is efficient, 2-3 m/'s difference has never had a change in SAMP.

If you do use a variable, please hook SetPlayerName(..)
Reply
#8

Don't use a stock for this function, and it's better like: ReturnName(playerid){...} It's more like a SA:MP native, same for ReturnIP etc. (If you're creating a gamemode, use the function, if you don't use the player's name that much just use the variable)
Reply
#9

Don't return local array.See
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)