View Single Post
Old 08-15-2004, 01:58 AM   #2 (permalink)
David
Moderate
 
David's Avatar
 
Join Date: Nov 2002
Location: USA
Posts: 6,598
Default

If you have not taken the time to look at the Docs yet, you should do so now. In a default installation of RunUO 1.0 Release Candidate 0 the opening index can be found here C:\Program Files\RunUO Software Team\RunUO 1.0 RC0\docs\overview.html. What you are presented with is essentially an API or Application Programming Interface; this is a wealth of information for scripters. The Docs directory also contains lists of all commands, ingame objects, and UO keywords; but those documents are not the topic of this discussion.

Upon opening Overview.html, you will notice the api is first broken down by the Namespaces discussed in the previous lesson. Most often you will find what you need in the Server namespace. Clicking once on Server presents you with a list of Classes in the Server namespace. (Remember that a namespace is a collection of Classes.) Most often you will look for either the Mobile Class or the Item Class. If you click on either one, you will see first the Base Class and possibly any "interfaces" this class implements. You can recognize an interface by its name as it will always start with a capital "I". We will discuss interfaces in detail at a later time. Next you will see a list of Classes derived from this Class followed by any Nested Types or Classes embedded within this Class. Nested types are generally specific purpose things like a timer for a particular function or a special data type. Clicking on any of these items or types will take you to the details for that specific type.

Finally we get to the components exposed by the Class. First are the static properties and methods; anything marked (static) applies to all instances and child instances of the class equally and at the same time. Changing a static property in one instance will change it in every instance of the Class. Next are the constructors which are marked (ctor). An object will always have a default constructor which has no parameters, and anything derived from Item or Mobile will have a constructor that requires a Serial as a parameter. Sometimes there will be additional constructors each requiring different parameters, for example the Item Class has a third constructor that requires a parameter of type int which is named itemID; this is how we pass the graphic index number to the Item Class when we derive a new Class. As an example in the script above we declare the Class as derived from Item with this line public class EightBall : Item and then tell the base Item class which itemID to use with this line public EightBall() : base( 0xE2F ). The compiler knows to use the Item( int itemID ) constructor because that is the only Item constructor which accepts a single integer as a parameter.

Next are the Properties of the Class. Properties will have a return type such as int or bool followed by the name of the property, followed by its accessors. The properties return type specifies what value you can expect when you reference the property in a script. Most often you will see a return type of bool (true/false) or int (whole numbers) although the return type can be nearly anything including another Class; for example several properties return a Mobile. The accessors provided determine if the property is read only ( get; ) or read/write ( get; set; ) We will discuss accessors in more detail the next lesson.

After the properties we find the Methods exposed (made public) by the Class. You will notice in some cases that there are multiple methods with the same name. This is referred to as overloading the method, and each overload will require a different set of parameters. Each overloaded version of the method should perform essentially the same function in the same manner.

In most cases the first item in the method declaration will be the word "virtual." The virtual keyword indicates that this method may be changed in a Class derived from this Class. This is known as overriding the method. When you override a method, the declaration of the method must be identical to the original version including all parameters; with the exception that the word virtual is changed to override. An overridden method may be again overridden in a further derived Class and all overloads of the method do not have to be overridden at the same time. If the word virtual is not present in the declaration of the method, then the method was not intended to be overridden.

The next item in the declaration of the method will be the return type. If there is nothing returned by the method, void will be specified. A return type of some sort is required. If a return type other than void is specified, the body of the method must conclude with a return statement of the proper value type. For example consider the following code:
Code:
 		public bool Dye( Mobile from, DyeTub sender )
		{
			if ( Deleted )
				return false;

			Hue = sender.DyedHue;
		}
This method will generate a compiler error because "not all code paths return a value." The method is declared with a return type of bool, but only if the item in question is deleted will a boolean value actually be returned. To fix this method the scripter should add return true; just before the closing brace.

Finally we see the actual name of the method followed by a set of parentheses. Inside the parentheses will be a list of any (possibly none) required parameters. For example in the OnDoubleClick method used previously the only parameter is listed as ( Mobile from ). This is telling us that the method expects to receive a reference to a object of the Mobile Class which it will refer to as from. When we override this method we may not change the list of parameters, but we can call them whatever we want, for example from often becomes m. That is ok, as long at from or m are still of the type called Mobile.

I hope this discussion will help you to use the Docs as the fantastic source of scripting information that it really is.

enjoy!
__________________
David Forum Moderator
The RunUO.com Forum Moderator Team

Forum Rules and Guidelines
RunUO Forum Search Engine
Download RunUO 2.0 RC2
David is offline