Is this necessary?
#1

Hello everyone, I've noticed that people usually reset their variables on player disconnect. Is it really necessary to do so? I understand saving system but not resetting variables at all.

Example:

Код:
 new variable[MAX_PLAYERS];
public OnPlayerConnect(playerid)
{
  variable[playerid] = 0;
}
public OnPlayerDissconect(playerid, reason)
{
  variable[playerid] = 0;
}
Why would you put it to 0 if when a player leaves server it's over, he isn't anymore connected.
Reply
#2

In some cases. Basically you gotta reset a player's variable when the player connects/disconnects if you want to avoid the next player connecting have the same variable values.
Reply
#3

Because variables aren't tied to players, and aren't automatically reset. When a player joins the server with the ID of a player that had left the server before, the connecting player inherits their variables/arrays.

Meaning if you have a variable called spawned and it is set true on spawn and a player leaves while the variable remains set to true without resetting it. A player connects and the player will be identified as spawned by any system that uses the variable spawned in an if-then.

OMG, spawn, spawned, spawned...
Reply
#4

It is necessary, but who need reset all variables in enum manualy...
PHP код:
enum e_PlayerData {
    
data1,
    
data2,
    
data3,
    ...
    
data99
};
new 
PlayerData[MAX_PLAYERS][e_PlayerData];
//the same on player complete disconnect but why you need reset data two time
public OnPlayerConnect(playerid){
    
    
/*
    PlayerData[playerid][data1] = 0;
    PlayerData[playerid][data2] = 0;
    PlayerData[playerid][data3] = 0;
    ...
    PlayerData[playerid][data99] = 0;
    //rly ?
    */
    
    
PlayerData[playerid] = PlayerDataReset(playerid); //all data is 0, data2 is 1
    
    //your code
    
    
return 1;
}
stock PlayerDataReset(playerid){
    new 
data_construct[e_PlayerData];
    
    
PlayerData[playerid][data2] = 1;
    
    return 
data_construct;

Reply
#5

I suppose it makes sense if you want to iterate over the variable later. Like if you would want to average the score or something like that. Suppose you have a function like avg(values[]) which would then calculate the average over all the (non-zero) values in the array. In that case it would include the score of the disconnected player.

In other cases ... I really don't know what's the best best and I don't think it really matters. Using both callbacks seems to be the safest best overall and can prevent weird bugs and glitches. I'd just make a separate function so you only have one line in each callback.
Reply
#6

Only reset the variables that can cause wrong results in any case (this being god mode toggle, spawn check, log in check, etc.).

Variables that get overwritten and don't inherit their old values are not needed to be reset. For example storing a dialog response.
Reply
#7

Yeah, I get it.

I was really curious why I need to 'reset' them in first place when I reset them for players when Itheyconnect ( above is the script im talking about ).

But I've never faced that issue that variable sticks to playerid, at all. That is another reason why I made this.

Thanks all, is there anything else important or just-to-know?
Reply
#8

Quote:
Originally Posted by Fratello
Посмотреть сообщение
Yeah, I get it.

I was really curious why I need to 'reset' them in first place when I reset them for players when Itheyconnect ( above is the script im talking about ).

But I've never faced that issue that variable sticks to playerid, at all. That is another reason why I made this.

Thanks all, is there anything else important or just-to-know?
It's preferable to reset variables only once and when players disconnect instead of connecting. Players won't notice a load difference when connecting (if any - unlikely, but still... differences add up, no benefit in doing otherwise anyway).

You may have also not noticed any issues because you probably never ran a server or just never noticed a related existing issue.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)