SA-MP Forums Archive
Which is better? Enum or variables. - 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)
+--- Thread: Which is better? Enum or variables. (/showthread.php?tid=432556)



Which is better? Enum or variables. - newbienoob - 23.04.2013

enum?
pawn Код:
enum en
{
    Enum1,
    Enum2,
    Enum3,
    Enum4
}
new Enums[MAX_PLAYERS][en];

//So later I can use it like this;

Enums[playerid][Enum1]++;
Enums[playerid][Enum2]--;
Enums[playerid][Enum3] = 1;
Enums[playerid][Enum4] = 0;
Or variables?
pawn Код:
new
     Var1[MAX_PLAYERS],
     Var2[MAX_PLAYERS],
     Var3[MAX_PLAYERS],
     Var4[MAX_PLAYERS]
     ;

//So later I can use it like this;
Var1[playerid]++;
Var2[playerid]--;
Var3[playerid] = 1;
Var4[playerid] = 0;
So which one is better?


Re: Which is better? Enum or variables. - Vince - 23.04.2013

Either take up the same amount of space so it really doesn't matter.


Re: Which is better? Enum or variables. - newbienoob - 23.04.2013

Quote:
Originally Posted by Vince
Посмотреть сообщение
Either take up the same amount of space so it really doesn't matter.
So, both enums and variables are same?


Re: Which is better? Enum or variables. - DaTa[X] - 23.04.2013

yes they are same


Re: Which is better? Enum or variables. - Knappen - 23.04.2013

Enums are a bit easier to work with, and a bit more organized in my opinion.
If you use an enum like this
pawn Код:
enum pdata
{
    pLevel,
    pMoney,
    pKills,
    pDeaths
}
new pInfo[MAX_PLAYERS][pdata];
It's a bit easier to work with, using for example "pInfo[playerid][pDeaths]++;".
You know where all the player variables are, and they are easy to edit, and it's easy to add new ones. Enums are great, but doesn't do much use if you only need 1 or 2 variables, then it's easier to just make 2 normal variables.


Re: Which is better? Enum or variables. - Scenario - 23.04.2013

Whatever works best for you. As Vince said, there's no performance differences.

Personally, I've been trying to use more enums than standard variables lately.


Re: Which is better? Enum or variables. - Emmet_ - 23.04.2013

Per-player variables

But seriously, enums are way more neat and organized. Sometimes I only create an enum for 1 or 2 variables, haha.


Re: Which is better? Enum or variables. - newbienoob - 23.04.2013

Quote:
Originally Posted by ******
Посмотреть сообщение
Neither - they are different things and if one was better than the other all the time the other one wouldn't be in the language would it? Questions like this are completely stupid and show a profound lack of understanding of programming and the concept of different tools for different jobs. It also shows a complete lack of understanding about how to write code and the best way to go about optimising things! Stop asking these questions!
We won't learn anything if we don't ask


Also, I use enum because I have like 50 variables(or more). My script looks simple, clean, and like Knappen said
Quote:

they are easy to edit, and it's easy to add new ones.




Re: Which is better? Enum or variables. - iggy1 - 23.04.2013

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
But seriously, enums are way more neat and organized. Sometimes I only create an enum for 1 or 2 variables, haha.
Quote:
Originally Posted by ******
Посмотреть сообщение
Then you're doing it just as badly as newbienoob.
I sometimes use enums for few vars.

EG,
pawn Код:
enum E_SOME_ENUM
{
    e_SOME_VAR,
    e_SOME_OTHER_VAR,
    bool: e_SOME_BOOL_VAR
}
new SomeVar[E_SOME_ENUM]
This is a bad idea? If so why?

EDIT: I know this doesn't actually make the code more manageable, but for some reason i prefer using enums than several vars. If this is bad practice I'd love to know, so i can alter my code accordingly (if needed)


Re: Which is better? Enum or variables. - Emmet_ - 23.04.2013

Quote:
Originally Posted by ******
Посмотреть сообщение
Well arrays are slower than variables so there's that, but I was talking about Emmet_ saying he used it for single variables. There you have all the lack of speed of arrays combined with the verbosity of enums for zero gain over variables.
Actually, I don't use enums for single variables anymore (but I still do that in some scripts or I do it without realizing). I either declare single variables or I use per-player variables, despite the discouragement for them (I don't even know why people even hate them lol).