SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 25/07/2012, 08:14 PM   #1
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,488
Reputation: 256
Default PVar Arrays - dynamic per-player memory

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];
must have a fixed size (5). It will always use MAX_PLAYERS * 5 cells of memory, even for slots that your script doesn't use.

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;
is equivalent to

pawn Code:
SetPVarArrayInt(playerid, "HouseKeys", 2, houseid);
when using PVar Arrays.

You can also add data to the dynamic array, without knowing the index

pawn Code:
new slot = AddToPVarArrayInt(playerid, "HouseKeys", houseid);
The amount of elements you can have in the dynamic array is basically unlimited.

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 have all the properties that PVars have (shared across scripts, automatically deleted when player leaves the server etc).

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);
GetPVarArrayInt(playerid, varname[], index)
pawn Code:
new houseid = GetPVarArrayInt(playerid, "HouseKeys", 2);
SetPVarArrayString(playerid, varname[], index, string_value[])
pawn Code:
SetPVarArrayString(playerid, "Notes", 1, "this a note");
GetPVarArrayString(playerid, varname[], index, string_return[], len)
pawn Code:
new notetext[128];
GetPVarArrayString(playerid, "Notes", 1, notetext, sizeof(notetext));
SetPVarArrayFloat(playerid, varname[], index, Float:float_value)
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);
Float:GetPVarArrayFloat(playerid, varname[], index)
pawn Code:
new Float:x, Float:y, Float:z;
x = GetPVarArrayFloat(playerid, "Pos", 0);
y = GetPVarArrayFloat(playerid, "Pos", 1);
z = GetPVarArrayFloat(playerid, "Pos", 2);
IsPVarArraySlotUsed(playerid, varname[], index)
pawn Code:
if(!IsPVarArraySlotUsed(playerid, "HouseKeys", 2))
{
    SendClientMessage(playerid, -1, "You don't have a house key on slot 2");
}
GetPVarArraySize(playerid, varname[])
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);
AddToPVarArrayInt(playerid, varname[], int_value)
pawn Code:
new slot = AddToPVarArrayInt(playerid, "HouseKeys", houseid);
AddToPVarArrayString(playerid, varname[], string_value[])
pawn Code:
new slot = AddToPVarArrayString(playerid, "Notes", "another note");
AddToPVarArrayFloat(playerid, varname[], Float:float_value)
pawn Code:
new slot = AddToPVarArrayFloat(playerid, "Offsets", 123.456);
DeletePVarArray(playerid, varname[], index = -1)
pawn Code:
DeletePVarArray(playerid, "HouseKeys"); // deletes all house keys from player
pawn Code:
DeletePVarArray(playerid, "HouseKeys", 2); // deletes house key from slot 2

Notes

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>
MadeMan is offline   Reply With Quote
Old 25/07/2012, 08:26 PM   #2
FireCat
High-roller
 
FireCat's Avatar
 
Join Date: Jul 2010
Posts: 2,323
Reputation: 609
Default Re: PVar Arrays - dynamic per-player memory

Nice idea! Never thought of it
FireCat is offline   Reply With Quote
Old 25/07/2012, 08:27 PM   #3
IstuntmanI
High-roller
 
IstuntmanI's Avatar
 
Join Date: Mar 2009
Location: Romania
Posts: 1,802
Reputation: 375
Default Re: PVar Arrays - dynamic per-player memory

Awesome !
__________________


IstuntmanI is online now   Reply With Quote
Old 25/07/2012, 10:20 PM   #4
Maxips2
Huge Clucker
 
Join Date: Oct 2008
Posts: 410
Reputation: 20
Default Re: PVar Arrays - dynamic per-player memory

Nice work!
Btw question: is it also limited to 800 variables per player?

EDIT: after viewing source i realized ur using regular pvar functions so it is limited.. :/
Maxips2 is offline   Reply With Quote
Old 25/07/2012, 11:32 PM   #5
Kar
Banned
 
Join Date: May 2010
Location: Black Asylum
Posts: 2,940
Reputation: 538
Default Re: PVar Arrays - dynamic per-player memory

Quote:
Originally Posted by Maxips2 View Post
Nice work!
Btw question: is it also limited to 800 variables per player?

EDIT: after viewing source i realized ur using regular pvar functions so it is limited.. :/
PVars have a limit? anyway don't you think thats alot -.^? That saves alot of memory..
Kar is offline   Reply With Quote
Old 25/07/2012, 11:36 PM   #6
Maxips2
Huge Clucker
 
Join Date: Oct 2008
Posts: 410
Reputation: 20
Default Re: PVar Arrays - dynamic per-player memory

I don't think thats enough lol.
But I've recently come up with a simple solution which would "increase" the limit.
I'll let you know if you are interested.
Maxips2 is offline   Reply With Quote
Old 26/07/2012, 01:05 AM   #7
[DOG]irinel1996
High-roller
 
[DOG]irinel1996's Avatar
 
Join Date: Jan 2010
Posts: 1,470
Reputation: 288
Default Respuesta: PVar Arrays - dynamic per-player memory

Nice, looks quite simple to use. And original idea, I'll give a try.
_________________________
Quote:
Originally Posted by Maxips2 View Post
I don't think thats enough lol.
But I've recently come up with a simple solution which would "increase" the limit.
I'll let you know if you are interested.
I think nobody will reach the max value, would be strange, 800 variables.
[DOG]irinel1996 is offline   Reply With Quote
Old 26/07/2012, 01:20 AM   #8
Maxips2
Huge Clucker
 
Join Date: Oct 2008
Posts: 410
Reputation: 20
Default Re: PVar Arrays - dynamic per-player memory

800 is not so much when its being used in a RP gamemode.
Maxips2 is offline   Reply With Quote
Old 24/02/2013, 06:52 PM   #9
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,526
Reputation: 554
Default Re: PVar Arrays - dynamic per-player memory

From thread name I was expecting some magic #emit based (or better - native plugin) mambo jumbo, and surprise, normal include by normal author. Me likey much, rep+
Misiur is offline   Reply With Quote
Old 24/02/2013, 11:44 PM   #10
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,833
Reputation: 1296
Default Re: PVar Arrays - dynamic per-player memory

PVars perform really poorly, I learned this early on when learning the ins and outs of SAMP scripting I can see this causing a lot of problems with looping functions. If anyone is thinking about using this I would stay away and keep with regular array definitions.
Pottus 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
[Plugin] Dynamic memory v1.2.1 Zeex Релизы/Releases 29 23/12/2012 09:31 AM
Saving memory with const arrays MP2 Scripting Help 1 18/05/2012 03:51 PM
Dynamic Object Arrays Scenario Help Archive 20 21/07/2010 08:08 PM


All times are GMT. The time now is 11:00 PM.


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