25.07.2012, 19:14
PVar Arrays
This include is based on PVars and can be used as per-player arrays with dynamic size.
Regular per-player arrays, such as
pawn Code:
new HouseKeys[MAX_PLAYERS][5];
PVar Arrays however, use memory only for slots that are used and don't need a max size limit.
Doing something like
pawn Code:
HouseKeys[playerid][2] = houseid;
pawn Code:
SetPVarArrayInt(playerid, "HouseKeys", 2, houseid);
You can also add data to the dynamic array, without knowing the index
pawn Code:
new slot = AddToPVarArrayInt(playerid, "HouseKeys", houseid);
Looping all slots:
pawn Code:
PVA_Loop(playerid, "HouseKeys", i)
{
if(GetPVarArrayInt(playerid, "HouseKeys", i) == houseid)
{
SendClientMessage(playerid, -1, "You have the keys for this house");
}
}
PVar Arrays can be used to save house keys, vehicle IDs, phone numbers, notes etc.
Functions
Functions and examples of usage
SetPVarArrayInt(playerid, varname[], index, int_value)
pawn Code:
SetPVarArrayInt(playerid, "HouseKeys", 2, houseid);
pawn Code:
new houseid = GetPVarArrayInt(playerid, "HouseKeys", 2);
pawn Code:
SetPVarArrayString(playerid, "Notes", 1, "this a note");
pawn Code:
new notetext[128];
GetPVarArrayString(playerid, "Notes", 1, notetext, sizeof(notetext));
pawn Code:
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
SetPVarArrayFloat(playerid, "Pos", 0, x);
SetPVarArrayFloat(playerid, "Pos", 1, y);
SetPVarArrayFloat(playerid, "Pos", 2, z);
pawn Code:
new Float:x, Float:y, Float:z;
x = GetPVarArrayFloat(playerid, "Pos", 0);
y = GetPVarArrayFloat(playerid, "Pos", 1);
z = GetPVarArrayFloat(playerid, "Pos", 2);
pawn Code:
if(!IsPVarArraySlotUsed(playerid, "HouseKeys", 2))
{
SendClientMessage(playerid, -1, "You don't have a house key on slot 2");
}
pawn Code:
new housekeys = GetPVarArraySize(playerid, "HouseKeys"); // returns the number of elements in "HouseKeys" array
printf("Player ID %d has %d house keys", playerid, housekeys);
pawn Code:
new slot = AddToPVarArrayInt(playerid, "HouseKeys", houseid);
pawn Code:
new slot = AddToPVarArrayString(playerid, "Notes", "another note");
pawn Code:
new slot = AddToPVarArrayFloat(playerid, "Offsets", 123.456);
pawn Code:
DeletePVarArray(playerid, "HouseKeys"); // deletes all house keys from player
pawn Code:
DeletePVarArray(playerid, "HouseKeys", 2); // deletes house key from slot 2
Variable names (varname[]) should not be longer than 50 characters.
Like regular arrays, PVar Array indexes start from 0 (0,1,2,3,...).
Download
http://pastebin.com/AshKExzY
1. Download the script from Pastebin
2. Rename it to "pvararray.inc"
3. Place it to ..\pawno\include\ folder
4. Include it in your script
pawn Code:
#include <pvararray>