RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Enums and why you love them.

Talow

Sorceror
Enums are a wonderful way of referencing integers without having to remember the number this can help with a few different things.

There are other instances where Enums are called but that is the easiest to show.

Enums are in essence ints that you can reference with words and that's why you love them!

To create an enum is simple in this example we'll use days of the week:

Code:
enum Days {
    Sat,
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri
};

Lets look at some important things with this.
Firstly: we didn't identify what values the words would be relating to, and so it starts at a value of 0.
meaning that Days.Sat would equal 0 and then Days.Sun would equal 1 ext.

You can declare the value for your words simply too...

Code:
enum Days {
    Sat = 1,
    Sun = 2,
...
}
and so forth however if your values are going in order you only have to specify the first value.

now that you have the enum set up you can declare a variable for it pretty easily too...

Code:
Days dayofweek = Days.sat;

Further more you can use it in if statements or in a switch statement...

Code:
if(dayofweek ==Days.sun) {
console.write("the variable 'dayofweek' equals SUNDAY!");
}

Code:
switch (dayofweek)
{
     case Days.sat: {
        console.write("saturday");
        break;
    }
     default: {
        console.write("It's not saturday");
        break;
    }
}

if later on you need to use the integer value of your Enum you can do so simply with conversion...

Code:
    int x = (int)Days.Sun;
    int y = (int)dayofweek;
I hope that gives you guys ideas for furthering your scripts to new heights.
 

Vorspire

Knight
Great intro to Enumerations, Talow :)

Just one thing, when you access a Map as you stated at the start of your post, it doesn't actually use an Enum, the Map names are actually hard-coded static Map variables within the Map class itself;
Code:
		public static Map Felucca { get { return m_Maps[0]; } }
		public static Map Trammel { get { return m_Maps[1]; } }
		public static Map Ilshenar { get { return m_Maps[2]; } }
		public static Map Malas { get { return m_Maps[3]; } }
		public static Map Tokuno { get { return m_Maps[4]; } }
		public static Map TerMur { get { return m_Maps[5]; } }
		public static Map Internal { get { return m_Maps[0x7F]; } }

A better example would be the MapRules enum;
Code:
	[Flags]
	public enum MapRules
	{
		None					= 0x0000,
		Internal				= 0x0001, // Internal map (used for dragging, commodity deeds, etc)
		FreeMovement			= 0x0002, // Anyone can move over anyone else without taking stamina loss
		BeneficialRestrictions	= 0x0004, // Disallow performing beneficial actions on criminals/murderers
		HarmfulRestrictions		= 0x0008, // Disallow performing harmful actions on innocents
		TrammelRules			= FreeMovement | BeneficialRestrictions | HarmfulRestrictions,
		FeluccaRules			= None
	}

The MapRules Enumeration is special though, as it represents a set of Flags which can be bit-wise combined or split when used.

Each value (IE 0x0000) in this particular Enumeration represents a 16-bit (2-byte) bit-mask. (a signed "Short" or "WORD").

MapRules.TrammelRules is instantiated as ((0x0002 OR 0x0004) OR 0x0008) which results in the full bit-mask of 0x000E.

Do not confuse the word "OR" above with meaning "this or that", it's actually a representation of the bitwise operator '|'


The most common usages of Enumerations in RunUO for the average developer will be in Gumps, when assigning Button ID's for use with OnResponse :)
 

Talow

Sorceror
I never realized that, I'll look at it again and update it to reflect something that works much better and thanks for the info.

After Vorspire pointed out that maps are not set as Enums I removed the reference to that in the post, the rest still stands, and Enums are still very useful.
 
Really interesting, thank you!

Do you accept requests? I'd like to read to a "Delegates & Interfaces and why you love them"... for me interfaces are nothing much different than a whatever class now :/
 
Top