26.07.2010, 11:27
(
Последний раз редактировалось Shadow™; 26.07.2010 в 16:17.
)
Using PVar's by Shadow™
INTRODUCTIONWell... 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
- Different PVar's
- SetPVarInt
- SetPVarString
- SetPVarFloat
- DeletePVar
- Why PVar's?
- Example
- Credits?
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.
IntegersWHY PVAR'S?
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.Strings
So, now:
Basically, this sets the Variable "Tutorial" to 0, so we can refer back to it at a later date:pawn Код:public OnPlayerConnect(playerid)
{
SetPVarInt(playerid,"Tutorial",0);
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 OnPlayerSpawn(playerid)
{
if(GetPVarInt(playerid,"Tutorial") == 0)
{
SendClientMessage(playerid,WHITE,"Sorry, you have to take the tutorial.");
}
return 1;
}
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;
}
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.Floats
So, now:
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)
{
SetPVarString(playerid,"MarriedTo","No-one");
return 1;
}
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;
}
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.DeletePVar
We can now use this to detect the players position and show it.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;
}
pawn Код:public OnPlayerLogin(playerid,password[])
{
SetPlayerPos(playerid,GetPVarFloat(playerid,"PosX"),GetPVarFloat(playerid,"PosY"),GetPVarFloat(playerid,"PosZ"));
return 1;
}
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?GetPVarType
pawn Код:public OnPlayerDisconnect(playerid)
{
DeletePVar(playerid,"PosX");
return 1;
}
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 3pawn Код: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, 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.