Which one would be better to use?
#1

To get the player's name, which method would be better to use? Like creating a global variable and getting the player's name on connect then use that var to fetch the name wherever you want.
I mean, for example

PHP код:
static
    
g_PlayerName[MAX_PLAYER_NAME][MAX_PLAYERS];
public 
OnPlayerConnect(playerid)
{
    
GetPlayerName(playeridg_PlayerName[playerid], MAX_PLAYER_NAME);
    return 
1;

Then using that var to get the player's name like;

PHP код:

CMD
:kick(playeridparams[])
{
    
// Some codes
    
format(stringsizeof(string), "Admin %s has kicked %s"g_PlayerName[playerid], gPlayerName[id]);
    
// More codes
    
return 1;

Or creating a local variable to get the player's name inside the kick command is better?
Reply
#2

pawn Код:
stock GetName(playerid)
{
    new
        name[MAX_PLAYER_NAME];
       
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    return name;
}
and then use it as:

pawn Код:
CMD:kick(playerid, params[])
{
    // Some codes
    format(string, sizeof(string), "Admin %s has kicked %s", GetName(playerid), GetName(otherplayerid));
    // More codes
    return 1;
}
Reply
#3

A local variable because it will be dynamic, as in case you change the player's name after connection, there wont be any issue.
Reply
#4

Yeah, why would you even create a global variable to store a players name if the player has it and you can get it whenever you want if the player is connected.
Reply
#5

Well, i thought creating too many local variables would increase the memory size? That's why i thought of using only one var. (Also i don't change name while player is still playing)

Anyway thanks for the reply, will be using the local ones.

Quote:
Originally Posted by ChunkyGaming26
Посмотреть сообщение
Bottom One Please Rep me and i will rep you
Reply
#6

Well global ones cost you even more on the memory. As there is no point using the variable to store a name when you can grab it just like i proven in my first post.

Also, everytime someone disconnects and reconnects the function needs to get a name and store it to a variable.
Reply
#7

Global variables are not stored on the stack.

I generally try to discourage the use of globals, but in this particular case I'd gladly make an exception, because of how widely it will be used and there being almost zero chance that a player's name will change while he's connected (the exception being SetPlayerName, of course, which must be caught). Accessing a variable is much faster than calling a function. And if you use a wrapper like GetName() then that will be even slower, still.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)