SA-MP Forums Archive
Data structures in Pawn - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Discussion (https://sampforum.blast.hk/forumdisplay.php?fid=84)
+---- Thread: Data structures in Pawn (/showthread.php?tid=386211)



Data structures in Pawn - MadeMan - 19.10.2012

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?)


Re: Data structures in Pawn - MadeMan - 19.10.2012

Quote:
Originally Posted by Y_Less
View Post
Some times you may find one style is good, others the other, others a mix.
Any examples?


Re: Data structures in Pawn - lorizz - 19.10.2012

For a save data, I prefer using enums, more simply instead of variables


Re: Data structures in Pawn - Steven82 - 20.10.2012

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.


Re: Data structures in Pawn - playbox12 - 20.10.2012

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.


Re: Data structures in Pawn - MadeMan - 20.10.2012

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.


Re: Data structures in Pawn - SuperViper - 20.10.2012

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];



Re: Data structures in Pawn - MadeMan - 03.11.2012

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);



Re: Data structures in Pawn - Finn - 03.11.2012

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.


Re: Data structures in Pawn - MadeMan - 03.11.2012

Quote:
Originally Posted by Finn
Посмотреть сообщение
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 Код:
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 Код:
enum eBoxing
{
    bTime = 2
};
enum eBomb
{
    bTime = 3
};
Doing

pawn Код:
printf("%d", bTime);
will give error

Код:
error 091: ambiguous constant; tag override is required
But doing

pawn Код:
printf("%d", eBoxing:bTime);
printf("%d", eBomb:bTime);
will print

Код:
2
3



Re: Data structures in Pawn - Michael@Belgium - 03.11.2012

I use enums more lately. It's way easier to work with.


Re: Data structures in Pawn - Finn - 03.11.2012

Quote:
Originally Posted by MadeMan
Посмотреть сообщение
pawn Код:
enum eBoxing
{
    bTime = 2
};
enum eBomb
{
    bTime = 3
};
pawn Код:
printf("%d", eBoxing:bTime);
printf("%d", eBomb:bTime);
Where exactly is that needed?


Re: Data structures in Pawn - Niko_boy - 04.11.2012

@MadeMan

that always been a question of mine :X e.g.
pawn Код:
enum tempinfo{
   variable1,variable2,
}
new Array0[2][tempinfo];
AND

pawn Код:
enum pinfo{
   variable1,variable2,
}
new Array1[2][pinfo];
and ..
i use like :
pawn Код:
Array1[0][variable1] = somevalue;//variable 1 belong to enum 'tempinfo'
Array2[0][variable1] = somevalue;//variable 1 belong to enum 'pinfo'
that would work i guess ?

and if it is in the following way it wouldn't, for sure right? :
pawn Код:
enum info{
   variable1,variable2,
}
new Array0[2][info];
//and
new Array1[2][info];
and use like:
pawn Код:
Array0[2][variable1] = somevalue;//variable 1 belong to enum 'info'
Array1[2][variable1] = somevalue;//this variable also belong to enum 'info'
^ or not ?

i am lately very confused regarding them..


Re: Data structures in Pawn - [ABK]Antonio - 04.11.2012

Quote:
Originally Posted by Niko_boy
Посмотреть сообщение
@MadeMan

that always been a question of mine :X e.g.
pawn Код:
enum tempinfo{
   variable1,variable2,
}
new Array0[2][tempinfo];
AND

pawn Код:
enum pinfo{
   variable1,variable2,
}
new Array1[2][pinfo];
and ..
i use like :
pawn Код:
Array1[0][variable1] = somevalue;//variable 1 belong to enum 'tempinfo'
Array2[0][variable1] = somevalue;//variable 1 belong to enum 'pinfo'
that would work i guess ?

and if it is in the following way it wouldn't, for sure right? :
pawn Код:
enum info{
   variable1,variable2,
}
new Array0[2][info];
//and
new Array1[2][info];
and use like:
pawn Код:
Array0[2][variable1] = somevalue;//variable 1 belong to enum 'info'
Array1[2][variable1] = somevalue;//this variable also belong to enum 'info'
^ or not ?

i am lately very confused regarding them..
That will work (the second thing). The way I look at enums (at least for basic uses) is as though you're doing...

pawn Код:
#define variable1 (0)
#define variable2 (1)
All you're doing really is putting a placeholder for the number.

Usually that's the way I'd set up a team system if it requires multiple arrays.


Re: Data structures in Pawn - MadeMan - 04.11.2012

@Niko_boy

They will both work.

In the first example, the arrays have different types (like eBoxing and eBomb).

In the second example, the arrays have same type (like BurgerMenu and PizzaMenu are both eMenuItem).

In the second example, you can also do this

pawn Код:
Array0[0] = Array1[1];
because they have same type.


Re: Data structures in Pawn - Y_Less - 04.11.2012

Not ONE of your pros/cons are specific to enums!


Re: Data structures in Pawn - MadeMan - 04.11.2012

Quote:
Originally Posted by Finn
Посмотреть сообщение
Where exactly is that needed?
I don't know, but maybe somebody else does. I'm just showing a possibility of which others might not be aware of.

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
Not ONE of your pros/cons are specific to enums!
Care to explain?