[Tutorial] Resetting of Variables
#1

Resetting of Variables

This is a quick tutorial which will cover a subject which always end up in the Scripting Help section of the forum. I'm talking about resetting of variables. The goal of this tutorial is to teach you why sometimes when a player joins this player receives another player's variable.

Let's say you have an script setup like this:

pawn Code:
#include <a_samp>
#include <YSI\y_commands>
#include <sscanf2>

enum pBlueprint{
    pAdminLVL,
};
new gPlayerInfo[MAX_PLAYERS][pBlueprint];

public OnPlayerConnect(playerid){
    if( !fexist( GetUserPath(playerid ) ) )
    {
        SendClientMessage(playerid, COLOR_YELLOW, "Please /register to register an account.");
    }
    else{
        SendClientMessage(playerid, COLOR_YELLOW, "Please /login to login to your account.");
    }
    return 1;
}

public OnPlayerDisconnect(playerid, reason){
   SavePlayer(playerid);
   return 1;
}

CMD:register(playerid, params[]){
   // Here you save the user using your system of choice.
}

CMD:login(playerid, params[]){
   // Here you login the user.
}

CMD:setadmin(playerid, params[]){
   new TargetID, TargetLVL;
   if(sscanf(params, "ii", TargetID, TargetLVL)) return SendClientMessage(playerid, COLOR_YELLOW,
   "Usage: /setadmin [Target ID] [Target LVL]"
   );

   gPlayerInfo[TargetID][pAdminLVL] = TargetLVL;  
   return 1;
}

Well once the player has logged in and if there's no one else on the server he'll get ID 0,

James_Carter (ID: 0) Connected.

Quote:
LOG: James_Carter used /setadmin 0 5

Above you can tell by the logs that James_Carter set THE ID OF ZERO (0) to be level 5 administrator.

James_Carter (ID: 0) Disconnected.

Please note that gPlayerInfo[0][pAdminLVL] is still set to 5!


Hannah_Lake (ID: 0) Connected.

This is when the real problem occur. Now that Hannah connects she inherits the ID of James_Carter and so also the level 5 administrator rank. How can we solve this? Easy! All you have to do is the following:

pawn Code:
public OnPlayerConnect(playerid){
    gPlayerInfo[playerid][pAdminLVL] = 0;
}
EDIT: (Thanks to Macluawn for reminding me.) There's also a more efficient but advanced way to do it by resetting the enum in a loop:

pawn Code:
public OnPlayerConnect(playerid){

    gPlayerInfo[playerid][pAdminLVL] = 51;
    printf("%d", gPlayerInfo[playerid][pAdminLVL]);
    for(new i; pBlueprint:i < pBlueprint; i++)
    {
        gPlayerInfo[playerid][pBlueprint:i] = 0;
    }
    printf("%d", gPlayerInfo[playerid][pAdminLVL]);

}
This will print:
Quote:

51
0

Conclusion

You have to reset all your variables when the user connect and after that you continue with the login/register process which will initialize the correct values for all your variables. That's it, thanks for reading!
Reply


Messages In This Thread
Resetting of Variables - by AlonzoTorres - 03.03.2014, 18:31
Re: Resetting of Variables - by Macluawn - 03.03.2014, 21:20
Re: Resetting of Variables - by AlonzoTorres - 03.03.2014, 23:00
Re: Resetting of Variables - by Luis- - 06.03.2014, 07:53
Re: Resetting of Variables - by AlonzoTorres - 06.03.2014, 08:02
Re: Resetting of Variables - by Luis- - 06.03.2014, 12:22
Re: Resetting of Variables - by CrazyFrenzy - 06.03.2014, 12:52
Re: Resetting of Variables - by Misiur - 06.03.2014, 13:43
Re: Resetting of Variables - by ColeMiner - 06.03.2014, 16:45
Re: Resetting of Variables - by CrazyFrenzy - 07.03.2014, 07:27
Re: Resetting of Variables - by Luis- - 07.03.2014, 11:24
Re: Resetting of Variables - by DandyCorleone - 01.01.2017, 00:38
Re: Resetting of Variables - by SickAttack - 01.01.2017, 04:42

Forum Jump:


Users browsing this thread: 5 Guest(s)