[Tutorial] Using PVars
#1

Using PVar's by Shadow™
INTRODUCTION

Well... This is going to be a nice, small and relaxed tutorial about PVar's and their usage. Only recently I started to use PVar's so feel free to correct me if I get anything wrong, but I doubt I will... So, let's begin shall we?

NAVIGATION
  1. Different PVar's
    • SetPVarInt
    • SetPVarString
    • SetPVarFloat
    • DeletePVar
  2. Why PVar's?
  3. Example
  4. Credits?
DIFFERENT PVAR'S

Well, there's 3 different types of PVars: Integer, String & Float. As you probably already know, an integer is any of the natural numbers (positive or negative) or zero. A string is obviously text related, for example: "Hi". And a Float as we know it are basically things which go into decimal places, such as Co-ordinates, Health, Armour and so on.
Integers
As I previously said, an integer is any of the natural numbers (positive or negative) or zero... Integers are used alot in Pawno especially with Player Statistics such as Kills, Deaths, Money, Drugs, Ammo, Guns... Obviously, with an Integer we have a function... Which is:

pawn Код:
SetPVarInt(playerid,varname[],amount);
Down to the explaination... The Variable Name could be anything you want, but for this purpose we'll call it tutorial.
So, now:

pawn Код:
public OnPlayerConnect(playerid)
{
    SetPVarInt(playerid,"Tutorial",0);
    return 1;
}
Basically, this sets the Variable "Tutorial" to 0, so we can refer back to it at a later date:

pawn Код:
public OnPlayerSpawn(playerid)
{
    if(GetPVarInt(playerid,"Tutorial") == 0)
    {
        SendClientMessage(playerid,WHITE,"Sorry, you have to take the tutorial.");
    }
    return 1;
}
As I just shown, we can use the variable to control anything about the player, which doesn't involve strings or floats, now to change the Tutorial from 0 to 1 to establish that he's completed it.

pawn Код:
public OnTutorialComplete(playerid)
{
    if(GetPVarInt(playerid,"Tutorial") == 0)
    {
        SetPVarInt(playerid,"Tutorial",1);
        SendClientMessage(playerid,WHITE,"Thank you for completeing the tutorial, you'll now be spawned.");
        Spawn(playerid);
    }
    return 1;
}
Strings
Well, as we already know that strings usually refer to words, IP Addresses, and so on... We can firmly say that Strings in this event also refer to those, for example:

pawn Код:
SetPVarString(playerid,varname[],string_value[]);
Down to the explaination... The Variable Name could be anything you want, but for this purpose we'll call it MarriedTo.
So, now:

pawn Код:
public OnPlayerConnect(playerid)
{
    SetPVarString(playerid,"MarriedTo","No-one");
    return 1;
}
As we can tell from the pawn code, it sets the player's MarriedTo stat to "No-one"... Now, let's get to work.

pawn Код:
public OnPlayerConnect(playerid)
{
    new married[128];
    GetPVarString(playerid, "MarriedTo", married,128);
    printf("Player Married To: %s", married);// will print 'Player Married To: *MarriedTo*'
    return 1;
}
Floats
Well, already we know that Floats are usually found in Co-ordinates, Health, Armour and other things which carry a decimal point.

pawn Код:
SetPVarFloat(playerid, varname[], Float:float_value);
Down to the explaination... As with the other types of PVar's, the varname can be anything you want but for this example, we'll use PosX.

pawn Код:
public OnPlayerSpawn(playerid)
{
    new Float:PosX,Float:PosY,Float:PosZ;
    GetPlayerPos(playerid,PosX,PosY,PosZ);
    SetPVarFloat(playerid,"PosX",PosX);
    SetPVarFloat(playerid,"PosY",PosY);
    SetPVarFloat(playerid,"PosZ",PosZ);
    return 1;
}
We can now use this to detect the players position and show it.

pawn Код:
public OnPlayerLogin(playerid,password[])
{
    SetPlayerPos(playerid,GetPVarFloat(playerid,"PosX"),GetPVarFloat(playerid,"PosY"),GetPVarFloat(playerid,"PosZ"));
    return 1;
}
DeletePVar
Well... This basically says it all, so there's no need for an example... Mainly, this just deletes a variable we've used, so for this we'll say: PosX from the Float Example:

pawn Код:
DeletePVar(playerid,varname[]);
As I previously said, this is used to delete PVars, so let's use it shall we?

pawn Код:
public OnPlayerDisconnect(playerid)
{
    DeletePVar(playerid,"PosX");
    return 1;
}
GetPVarType
This basically gets the type of the PVar and returns it, so for example:

pawn Код:
#define PLAYER_VARTYPE_NONE         0
#define PLAYER_VARTYPE_INT          1
#define PLAYER_VARTYPE_STRING       2
#define PLAYER_VARTYPE_FLOAT        3
pawn Код:
GetPVarType(playerid,varname[]);
As I previously said, this is used to detect the type of PVar, so let's use it shall we?

pawn Код:
public OnPlayerUpdate(playerid)
{
    GetPVarType(playerid,"PosX"); // This would return: PLAYER_VARTYPE_FLOAT (Defined Above)
    return 1;
}
WHY PVAR'S?
Why, that's simple... Back in the day, we used to use things like PlayerInfo[playerid][pPosX] and so on, this is basically a new way of doing the same thing with a more stable approach, no doubt... There's probably more to it than that, such as speed and so on... But I'm not going to go into that.

EXAMPLE
Is it nessecary? Come on... I've just explained all of them with atleast 1 example, so just read the topic and mess about with PVars, eventually you'll learn and thank.

CREDITS
This Tutorial was created by myself with encouragement from the Global Vars topic which I might do a tutorial on later, but it depends how I feel... Please, by all means feel free to leave a reply with ideas on how I can improve it and if I have made a mistake somewhere.
Reply
#2

Hmmm... Pretty nice tutorial about PVars.
Well said.
Reply
#3

Thanks, much appreciated.
Reply
#4

Extremely unusual (in SA-MP forum context, very usual) way to start a tutorial. You say you just are learning about player variables and obviously haven't tested the code posted here, so why do you make a tutorial? You can't be entirely sure everything of it works and the usage of player variables is described very straightforward here and every function linked to the page has good examples and explanations there.

Quote:

As you probably already know, an Integer is a whole number which can be anything from 1-100000000.

Wrong, look up the integer range (protip: also goes below zero)

Also, SetPVarInt is a function, not a callback.

I wonder if anyone checks the tutorials to see if they're actually going to be helpful for new coders.
Reply
#5

I never have used PVar's , but I have question,

Here
pawn Код:
public OnPlayerConnect(playerid)
{
    SetPVarInt(playerid,"Tutorial",0);
    return 1;
}
This "Tutorial" must be defined at script start or isn't?
like new Tutorial[MAX_PLAYERS];
Reply
#6

Quote:
Originally Posted by ikey07
Посмотреть сообщение
I never have used PVar's , but I have question,

Here
pawn Код:
public OnPlayerConnect(playerid)
{
    SetPVarInt(playerid,"Tutorial",0);
    return 1;
}
This "Tutorial" must be defined at script start or isn't?
like new Tutorial[MAX_PLAYERS];
No it doesn't have to be defined.
A player variable which isn't set on a player returns 0 =D
Reply
#7

Quote:
Originally Posted by TransformerOwl
Посмотреть сообщение
Extremely unusual (in SA-MP forum context, very usual) way to start a tutorial. You say you just are learning about player variables and obviously haven't tested the code posted here, so why do you make a tutorial? You can't be entirely sure everything of it works and the usage of player variables is described very straightforward here and every function linked to the page has good examples and explanations there.

Wrong, look up the integer range (protip: also goes below zero)

Also, SetPVarInt is a function, not a callback.

I wonder if anyone checks the tutorials to see if they're actually going to be helpful for new coders.
Quote:
Originally Posted by ******
Define - integer:

• any of the natural numbers (positive or negative) or zero; "an integer is a number that is not a fraction".
So basically, when I said: "An integer is a whole number, 1-10000000", I could then say define Whole Number, which is:

Quote:
Originally Posted by ******
Define - Whole Number:

•A whole number doesn't contain a fraction. A whole number is an integer which has 1 or more units and can be positive or negative.
It was still correct.

Secondly, if you actually read the tutorial you'd notice that I say:

Quote:
Originally Posted by Shadow™
I've only started using PVars recently.
Suggesting that I'm not the smartest kid on the block, nor did I say I was... I'm just creating a tutorial for others to use and I did say:

Quote:
Originally Posted by Shadow™
by all means feel free to leave a reply with ideas on how I can improve it and if I have made a mistake somewhere.
So please, don't try to win one over on me... I never said I tested it, I never said an Integer couldn't go below 0.
Reply
#8

Sorry if you got me wrong, but here's something else I'd like to point out in that case...

pawn Код:
public OnPlayerDisconnect(playerid)
{
    DeletePVar(playerid,"PosX");
    return 1;
}
Deleting a per-player variable is not necessary under OnPlayerDisconnect.

And GetPVarType afaik does not return a string like you say. It returns the type, like this
pawn Код:
// PVar enumeration
#define PLAYER_VARTYPE_NONE         0
#define PLAYER_VARTYPE_INT          1
#define PLAYER_VARTYPE_STRING       2
#define PLAYER_VARTYPE_FLOAT        3
So
pawn Код:
GetPVarType(playerid, "PosX");
would actually return PLAYER_VARTYPE_FLOAT (3)

And your example of "MarriedTo" for string pvars isn't exactly the best, since the best method to assign someone married to another player would be using IDs and hence SetPVarInt and GetPVarInt.
Reply
#9

Forgot a lot of functions, and features of PVars like that PVars automatically reset when a player disconnects.

Other then that, not bad.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)