Data structures in Pawn
#1

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?)
Reply
#2

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?
Reply
#3

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

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.
Reply
#5

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.
Reply
#6

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.
Reply
#7

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];
Reply
#8

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);
Reply
#9

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.
Reply
#10

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
Reply
#11

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

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?
Reply
#13

@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..
Reply
#14

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.
Reply
#15

@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.
Reply
#16

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

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


Forum Jump:


Users browsing this thread: 6 Guest(s)