[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
#2

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.
Reply
#3

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.
Reply
#4

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

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.
Reply
#6

Oh awesome! Makes it much easier for me, thanks!
Reply
#7

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;

Reply
#8

@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
Reply
#9

CrazyFrenzy: That code won't even compile.

Misiur: There's a newer version in YSI.
Reply
#10

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.
Reply
#11

But the example didn't work.
Reply
#12

i see. thank you
Reply
#13

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)