SA-MP Forums Archive
[Tutorial] Resetting of Variables - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Resetting of Variables (/showthread.php?tid=498503)



Resetting of Variables - AlonzoTorres - 03.03.2014

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!


Re: Resetting of Variables - Macluawn - 03.03.2014

There was a cleaner way of doing it.
pawn Code:
gPlayerInfo[playerid] = pBlueprint;
or something like that. I dont exactly remember the syntax required, I'm sure someone can correct me on this.


Re: Resetting of Variables - AlonzoTorres - 03.03.2014

Quote:
Originally Posted by Macluawn
View Post
There was a cleaner way of doing it.
pawn Code:
gPlayerInfo[playerid] = pBlueprint;
or something like that. I dont exactly remember the syntax required, I'm sure someone can correct me on this.
Yes of course, although this tutorial was aimed at beginners but here it is:
pawn Code:
enum pBlueprint{
    pAdminLVL,
};
new gPlayerInfo[MAX_PLAYERS][pBlueprint];

for(new i; pBlueprint:i < pBlueprint; i++)
{
    gPlayerInfo[playerid][pBlueprint:i] = 0;
}
Added to main thread.


Re: Resetting of Variables - Luis- - 06.03.2014

So using the loop method will reset all the variables in the enum?


Re: Resetting of Variables - AlonzoTorres - 06.03.2014

Quote:
Originally Posted by Luis-
View Post
So using the loop method will reset all the variables in the enum?
Yes, or give them the value of zero.


Re: Resetting of Variables - Luis- - 06.03.2014

Oh awesome! Makes it much easier for me, thanks!


Re: Resetting of Variables - CrazyFrenzy - 06.03.2014

You forgot to mention how to reset a string.

STRING:
PHP Code:
new string[64];
format(string64"Vacant");
OnPlayerConnect(playerid)
{
    
string EOS
    or
    
string '\0';

VARIABLE
PHP Code:
new var;
var = 
50;
OnPlayerConnect(playerid)
{
    var = 
0;




Re: Resetting of Variables - Misiur - 06.03.2014

@Macluawn: Sorry, but no. pBlueprint is equal to the size of enum. I think you meant
pawn Code:
static const
    emptyInfo[pBlueprint]
;
gPlayerInfo[playerid] = emptyInfo;
@OP: http://forum.sa-mp.com/showthread.ph...81#post1606781 - faster than loops and stuff

#e: Latest version: http://forum.sa-mp.com/showthread.ph...et#post1974747


Re: Resetting of Variables - ColeMiner - 06.03.2014

CrazyFrenzy: That code won't even compile.

Misiur: There's a newer version in YSI.


Re: Resetting of Variables - CrazyFrenzy - 07.03.2014

Quote:
Originally Posted by ColeMiner
View Post
CrazyFrenzy: That code won't even compile.

Misiur: There's a newer version in YSI.
I was just giving an example of use in a public, my point wasn't to make a full example but to show only the variable reset.


Re: Resetting of Variables - Luis- - 07.03.2014

But the example didn't work.


Re: Resetting of Variables - DandyCorleone - 01.01.2017

i see. thank you


Re: Resetting of Variables - SickAttack - 01.01.2017

Quote:
Originally Posted by Misiur
View Post
@Macluawn: Sorry, but no. pBlueprint is equal to the size of enum. I think you meant
pawn Code:
static const
    emptyInfo[pBlueprint]
;
gPlayerInfo[playerid] = emptyInfo;
Note: This only works for single dimension arrays.