ID:132685
 

Yeah...

Basically, curious as to why there isn't an enumerated type to use, unless there is...

Reason:

Example: C++

enum ECharacters
{
ECharacters_RAWR = 0,
ECharacters_PIE,
ECharacters_Cheese,
ECharacters_MAX
};

CCharacters m_Character[ ECharacters_MAX ];

for( int i = 0; i < ECharacters_MAX; ++i )
{
m_Character[ i ].name = NULL;
}

So, arrays and loops can be easier, and easier to read instead of say.

CCharacters m_Character[ 3 ];

for( int i = 0; i < 3; ++i )
{
m_Character[ i ].name = NULL;
}

So, if I were to remove a character, I'd have to decrement where ever a 3 is declared for characters.
This is one of the uses of the #define directive.
#define POISONED 1
#define PARALYZED 2
#define SLEEPING 4
#define CONFUSED 8

mob/character/var/state

mob/character/proc/Cant_Move()
return src.state & (PARALYZED | SLEEPING)
I'm not sure that's a particularly good use of enumerations - I was under the impression that they're better used when you've got a variable that can be a number of different things with no real interpretation. So say you've got a communications link that can be down, up and simplex, or up and duplex, then you'd use an enumeration.

I'd suggest using const vars if you need something like that in DM:

var
const
CHARACTER_SOLDIER = 1
CHARACTER_ARCHER = 2
CHARACTER_MAGE = 3
CHARACTER_THIEF = 4
CHARACTER_MAX = 4


If you want the compiler to catch a failure to change CHARACTER_MAX when removing characters, you could consider adding noops referencing them:

CHARACTER_MAX = 4 + (CHARACTER_SOLDIER+CHARACTER_ARCHER+CHARACTER_MAGE_CHARACTER_THIEF)*0
In response to Jp

Well, this method seems more appealing, however it is more redundant when it comes to adding a new member of an enum.


In response to HolyDoomKnight
Adding new members to the enum is something you do rarely, by the nature of what an enumeration is. Adding a state is pretty non-trivial in terms of the operation of your code. The issues with the method Jp puts forward is bounds checking, and it's not actually enumerable. But then, enumeration types in a lot of languages aren't enumerable. An enumeration type could solve that, but you could happily wrap those constants in a datum do give some type-safety and enumerability.
In response to Stephen001
Stephen001 wrote:
Adding new members to the enum is something you do rarely, by the nature of what an enumeration is. Adding a state is pretty non-trivial in terms of the operation of your code. The issues with the method Jp puts forward is bounds checking, and it's not actually enumerable. But then, enumeration types in a lot of languages aren't enumerable. An enumeration type could solve that, but you could happily wrap those constants in a datum do give some type-safety and enumerability.

Another simple way to do this is with a list:

var/list/class = list("soldier" = 1, "archer" = 2, "mage" = 3, etc.)


You can reference a member of the enumeration as class["soldier"], you can check if a string is a member of the enumeration, and you can enumerate the members.

you could happily wrap those constants in a datum do give some type-safety and enumerability.

I'm not sure what you mean by "type-safety" since this code is perfectly valid even though /Class is not a descendant of /obj:

mob
var
Class/class
Login()
class = new /obj()


But you do get the benefit that you can't assign an invalid value by accident (provided you always use the datum for assignments), for example:

Class
var
soldier = 1
archer = 2
mage = 3

var
Class/Class = new /Class()

mob
var
class
Login()
class = Class.soldier


If you accidentally misspell "soldier" you will get a compile-time error, but with using the list method from above you would get no error.

Using a datum, you can also use the datum's vars list to enumerate the classes. To add another member you just need to add another variable. To keep the code simpler I didn't include it in this post, but the datum's constructor could assign values to each member variable to ensure that each has a distinct value.
In response to Forum_account
Yeah, when I say type-safety in the context of DM, I mean from a programmer's point of view, seen as the compiler couldn't give two hoots what you do with types. Small benefit, but you work with what you've got.