SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 26/07/2010, 11:27 AM   #1
Shadow™
Big Clucker
 
Join Date: Jul 2010
Location: In a cardboard box ):
Posts: 186
Reputation: 1
Default Using PVars

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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
DeletePVar(playerid,varname[]);
As I previously said, this is used to delete PVars, so let's use it shall we?

pawn Code:
public OnPlayerDisconnect(playerid)
{
    DeletePVar(playerid,"PosX");
    return 1;
}

GetPVarType
This basically gets the type of the PVar and returns it, so for example:

pawn Code:
#define PLAYER_VARTYPE_NONE         0
#define PLAYER_VARTYPE_INT          1
#define PLAYER_VARTYPE_STRING       2
#define PLAYER_VARTYPE_FLOAT        3

pawn Code:
GetPVarType(playerid,varname[]);
As I previously said, this is used to detect the type of PVar, so let's use it shall we?

pawn Code:
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.
__________________



Last edited by Shadow™; 26/07/2010 at 04:17 PM.
Shadow™ is offline   Reply With Quote
Old 26/07/2010, 01:07 PM   #2
ViruZZzZ_ChiLLL
High-roller
 
ViruZZzZ_ChiLLL's Avatar
 
Join Date: Aug 2009
Location: I don't know
Posts: 1,646
Reputation: 8
Default Re: Using PVars

Hmmm... Pretty nice tutorial about PVars.
Well said.
__________________


ViruZZzZ_ChiLLL is offline   Reply With Quote
Old 26/07/2010, 01:16 PM   #3
Shadow™
Big Clucker
 
Join Date: Jul 2010
Location: In a cardboard box ):
Posts: 186
Reputation: 1
Default Re: Using PVars

Thanks, much appreciated.
__________________


Shadow™ is offline   Reply With Quote
Old 26/07/2010, 02:39 PM   #4
TransformerOwl
Little Clucker
 
Join Date: Jul 2010
Posts: 16
Reputation: 0
Default Re: Using PVars

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.
TransformerOwl is offline   Reply With Quote
Old 26/07/2010, 02:58 PM   #5
ikey07
High-roller
 
ikey07's Avatar
 
Join Date: Jul 2010
Location: Latvia,Riga
Posts: 1,169
Reputation: 180
Default Re: Using PVars

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

Here
pawn Code:
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];
__________________
ikey07 is offline   Reply With Quote
Old 26/07/2010, 03:00 PM   #6
Last_Stand_Guardian
Big Clucker
 
Join Date: Mar 2010
Posts: 126
Reputation: 0
Default Re: Using PVars

Quote:
Originally Posted by ikey07 View Post
I never have used PVar's , but I have question,

Here
pawn Code:
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

Last edited by Last_Stand_Guardian; 26/07/2010 at 03:15 PM.
Last_Stand_Guardian is offline   Reply With Quote
Old 26/07/2010, 03:16 PM   #7
Shadow™
Big Clucker
 
Join Date: Jul 2010
Location: In a cardboard box ):
Posts: 186
Reputation: 1
Default Re: Using PVars

Quote:
Originally Posted by TransformerOwl View Post
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.
__________________


Shadow™ is offline   Reply With Quote
Old 26/07/2010, 03:37 PM   #8
TransformerOwl
Little Clucker
 
Join Date: Jul 2010
Posts: 16
Reputation: 0
Default Re: Using PVars

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

pawn Code:
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 Code:
// PVar enumeration
#define PLAYER_VARTYPE_NONE         0
#define PLAYER_VARTYPE_INT          1
#define PLAYER_VARTYPE_STRING       2
#define PLAYER_VARTYPE_FLOAT        3
So
pawn Code:
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.
TransformerOwl is offline   Reply With Quote
Old 27/07/2010, 04:08 PM   #9
Hiddos
High-roller
 
Hiddos's Avatar
 
Join Date: Sep 2008
Posts: 3,307
Reputation: 2184
Default Re: Using PVars

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

Other then that, not bad.
__________________
Quote:
Originally Posted by DeWolf View Post
For fuck's sake people, this whole thread is fucked up now.
Hiddos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pvars Steven82 Help Archive 0 26/05/2010 08:52 PM
Looping Pvars Dark_Kostas Help Archive 4 06/04/2010 07:18 AM
PVars Question(s) XGh0stz Help Archive 0 02/04/2010 08:08 PM
Pvars question? Torran Help Archive 4 26/03/2010 05:53 PM
Pvars... biltong Help Archive 8 26/03/2010 01:31 PM


All times are GMT. The time now is 01:01 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.