SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 19/10/2012, 07:14 PM   #1
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,488
Reputation: 256
Default Data structures in Pawn

If you want to save some data that is related to something (for example a vehicle) there are 2 ways to do it:

1)

Code:
new VehicleCreated[MAX_VEHICLES];
new VehicleID[MAX_VEHICLES];
new VehicleModel[MAX_VEHICLES];
new Float:VehiclePos[MAX_VEHICLES][4];
new VehicleColor[MAX_VEHICLES][2];
new VehicleInterior[MAX_VEHICLES];
new VehicleWorld[MAX_VEHICLES];
new VehicleOwner[MAX_VEHICLES][MAX_PLAYER_NAME];
new VehicleNumberPlate[MAX_VEHICLES][16];
new VehicleValue[MAX_VEHICLES];
new VehicleLock[MAX_VEHICLES];
new VehicleMods[MAX_VEHICLES][14];
new VehiclePaintjob[MAX_VEHICLES] = {255, ...};
Usage:

Code:
VehicleModel[vehicleid] = 450;
2)

Code:
enum eVehicle
{
	vCreated,
	vID,
	vModel,
	Float:vPos[4],
	vColor[2],
	vInterior,
	vWorld,
	vOwner[MAX_PLAYER_NAME],
	vNumberPlate[16],
	vValue,
	vLock,
	vMods[14],
	vPaintjob
}
new Vehicle[MAX_VEHICLES][eVehicle];
Usage:

Code:
Vehicle[vehicleid][vModel] = 450;


Question: Which one do you use and WHY?


(Question: Which one is better and WHY?)

Last edited by MadeMan; 20/10/2012 at 11:09 AM. Reason: Changed the question
MadeMan is offline   Reply With Quote
Old 19/10/2012, 07:36 PM   #2
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,488
Reputation: 256
Default Re: Data structures in Pawn

Quote:
Originally Posted by ****** View Post
Some times you may find one style is good, others the other, others a mix.
Any examples?
MadeMan is offline   Reply With Quote
Old 19/10/2012, 10:15 PM   #3
lorizz
Big Clucker
 
lorizz's Avatar
 
Join Date: Oct 2012
Posts: 70
Reputation: 4
Default Re: Data structures in Pawn

For a save data, I prefer using enums, more simply instead of variables
__________________
I don't read [+REP] post and [OMGHELP]

5/6,5$ for make you an ENITRE admin system (with slap, splat, banip, unbanip, freeze ecc.) pm me for more infos
10/14$ for make you an ENITRE GM! (Every GM)
lorizz is offline   Reply With Quote
Old 20/10/2012, 08:34 AM   #4
Steven82
High-roller
 
Join Date: Nov 2009
Posts: 2,431
Reputation: 186
Default Re: Data structures in Pawn

Using a bunch of different variables, instead of an enum for something, that's going to save multiple things into a file/database doesn't seem that appealing to me. But it is your opinion.
__________________
If you read my old posts; I apologize in advance.
Steven82 is offline   Reply With Quote
Old 20/10/2012, 11:09 AM   #5
playbox12
High-roller
 
playbox12's Avatar
 
Join Date: Feb 2010
Location: Netherlands
Posts: 1,318
Reputation: 216
Default Re: Data structures in Pawn

It doesn't matter (much) performance wise AFAIK. In a system you described the only reason I would step away from the enum is when I need to have multi-dimensional arrays inside the enum/array.

SA-MP does not allow for more than 3 dimensions per array, and when you use an enum like that, your actually making a two-dimensional array.
playbox12 is offline   Reply With Quote
Old 20/10/2012, 11:20 AM   #6
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,488
Reputation: 256
Default Re: Data structures in Pawn

I changed the question, maybe it's better now.

I used regular arrays before (the first option), but now I prefer enums (the second option).

Pros and cons of enums:

Pros:

1) You know where the variable belongs

If you use an id on vModel, you can be sure that you can use the same id on vLock too

pawn Code:
Vehicle[id][vModel] = 450;
Vehicle[id][vLock] = 1;

When using regular arrays, you can't be so sure, because the arrays can have different lengths

pawn Code:
new VehicleModel[200];
new VehicleLock[50];

pawn Code:
id = 150;
VehicleModel[id] = 450;
VehicleLock[id] = 1;

2) You can do this

pawn Code:
Vehicle[oneid] = Vehicle[otherid];

to copy the contents of one vehicle to another.

Cons:

1) You can't use sizeof on array elements

Example:

pawn Code:
format(Vehicle[vehicleid][vNumberPlate], sizeof(Vehicle[][vNumberPlate]), "sometext");

But you can define the size as a constant and use it

pawn Code:
#define MAX_NUMBER_PLATE 16

pawn Code:
vNumberPlate[MAX_NUMBER_PLATE],

pawn Code:
format(Vehicle[vehicleid][vNumberPlate], MAX_NUMBER_PLATE, "sometext");

2) You can't use more than 1 dimension on array elements

Doing

pawn Code:
vTrunk[5][2],

in enum declaration is not valid.



Can't think of anything else at the moment.

I'm hoping to put together a full list of pros and cons of using an enum as a data structure. So if you can add something, would be great.
MadeMan is offline   Reply With Quote
Old 20/10/2012, 04:00 PM   #7
SuperViper
High-roller
 
SuperViper's Avatar
 
Join Date: Sep 2011
Posts: 1,178
Reputation: 315
Default Re: Data structures in Pawn

Quote:
Originally Posted by MadeMan View Post
2) You can't use more than 1 dimension on array elements

Doing

pawn Code:
vTrunk[5][2],

in enum declaration is not valid.
An alternative for that is using multiple enums, ex:

pawn Code:
enum e_FactionRanks
{
    factionRank[32]
}

new FactionRanks[FACTION_AMOUNT][RANK_AMOUNT][e_FactionRanks];
__________________
Release NameUse
FairPlayKeep the cheaters out of your server
Object Model ArrayArray of every single object model in the game
RCON PanelControl any server as you like with RCON controls
SlopeClimb up a steep hill against other players with cars falling down

I do not play on any SAMP server. Anyone with the name Ricky Phelps in-game is an imposter.
SuperViper is offline   Reply With Quote
Old 03/11/2012, 06:39 PM   #8
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,488
Reputation: 256
Default Re: Data structures in Pawn

More good things about enums

3) You can have variables with same name, as long as they belong to different enums

Example:

pawn Code:
enum eBoxing
{
    bTime
};
new Boxing[5][eBoxing];
pawn Code:
enum eBomb
{
    bTime
};
new Bomb[10][eBomb];

Usage:

pawn Code:
Boxing[boxid][bTime] = 30;
Bomb[bombid][bTime] = 20;

4) You can pass an object attribute as a parameter to a function

For example if you want to make a function that only gives the value of a member variable, but doesn't allow to set it

pawn Code:
GetVehicleValue(id, eVehicle:item)
{
    return Vehicle[id][item];
}

Usage:

pawn Code:
new lockstatus = GetVehicleValue(id, vLock);

5) You can do this

pawn Code:
enum eMenuItem
{
    fPrice,
    fName[16]
};
new BurgerMenu[][eMenuItem] = {
    {40,"Cheese Burger"},
    {50,"Huge Burger"},
    {30,"Fries"}
};
new PizzaMenu[][eMenuItem] = {
    {80,"Large Pizza"},
    {60,"Medium Pizza"},
    {40,"Small Pizza"}
};
pawn Code:
new MenuItem[eMenuItem];
if(IsPlayerInBurgerShot(playerid))
    MenuItem = BurgerMenu[listitem];
else
    MenuItem = PizzaMenu[listitem];
if(GetPlayerMoney(playerid) < MenuItem[fPrice])
{
    SendClientMessage(playerid, COLOR_RED, "You don't have enough money!");
    return 1;
}
GivePlayerMoney(playerid, -MenuItem[fPrice]);
new msg[128];
format(msg, sizeof(msg), "You have bought %s for $%d", MenuItem[fName], MenuItem[fPrice]);
SendClientMessage(playerid, COLOR_WHITE, msg);
MadeMan is offline   Reply With Quote
Old 03/11/2012, 06:54 PM   #9
Finn
High-roller
 
Join Date: Jan 2008
Posts: 1,465
Reputation: 382
Default Re: Data structures in Pawn

Quote:
Originally Posted by MadeMan View Post
3) You can have variables with same name, as long as they belong to different enums

Example:

pawn Code:
enum eBoxing
{
    bTime
};
new Boxing[5][eBoxing];
pawn Code:
enum eBomb
{
    bTime
};
new Bomb[10][eBomb];

Usage:

pawn Code:
Boxing[boxid][bTime] = 30;
Bomb[bombid][bTime] = 20;
Those arrays do not have the same names, other one is Boxing and other one is Bomb.

If you did it the other way, it'd be:
pawn Code:
new BoxingTime[5];
new BombTime[10];

...And it would work.
Finn is offline   Reply With Quote
Old 03/11/2012, 07:39 PM   #10
MadeMan
High-roller
 
MadeMan's Avatar
 
Join Date: Jun 2007
Posts: 3,488
Reputation: 256
Default Re: Data structures in Pawn

Quote:
Originally Posted by Finn View Post
Those arrays do not have the same names, other one is Boxing and other one is Bomb.

If you did it the other way, it'd be:
pawn Code:
new BoxingTime[5];
new BombTime[10];

...And it would work.
Well yes, this is something you can do with regular arrays as well.

It was just an example anyway. I just wanted to show that you can have 2 variables (actually constants) with same names.

pawn Code:
enum eBoxing
{
    bTime = 2
};
enum eBomb
{
    bTime = 3
};

Doing

pawn Code:
printf("%d", bTime);

will give error

Code:
error 091: ambiguous constant; tag override is required
But doing

pawn Code:
printf("%d", eBoxing:bTime);
printf("%d", eBomb:bTime);

will print

Code:
2
3
MadeMan 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] Vectoral Pawn - STL Data containers for pawn Rancho Plugin Development 69 26/03/2019 12:50 AM
[HELP]Regarding mySQL structures. Alberto_DR Server Support 1 09/11/2011 02:57 PM
[Include] [INC] some useful data structures Zeex Includes 7 10/05/2010 07:17 PM
[Help] Control structures Coicatak Help Archive 7 12/03/2009 04:09 PM


All times are GMT. The time now is 03:14 PM.


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