For any class, it's possible to create a class constructor, or a method that is called when an object of the class is created. An example of a constructor can be found in many of RunUO's scripts and goes something along these lines
Code:
public class Cl
{
// This is the constructor, its name has to be the same as the class and also may not contain a return type
// (such as int in public int f()) as it implicitly returns the constructed Cl object
public Cl()
{
//Any statements put inside of the constructor are automatically executed when an object is created.
}
}
In that example the class name was Cl as was the constructor's name, though the name's can be whatever you'd like as long as they are always the exact same.
Now, lets take one of RunUO's scripts, ProspectorsTool and add a constructor to it that sets the amount of uses when passed an integer (don't worry too much about base(...) in the example, all it does is calls the constructor of the class the object is derived from with specified parameters, in this case, we're calling the constructor of BaseBashing with the ItemID of the ProspectorsTool.) For simplicity, we'll omit all but the class definition at the top and our constructor.
Code:
public class ProspectorsTool : BaseBashing, IUsesRemaining
{
/*
Some of the class would probably be here
*/
// This states that this constructor can be called in-game, ones without [Constructable] can only be called from out-of-game,
// such as from another script.
[Constructable]
// This line defines our constructor. It has the same name as our class, has a single parameter 'uses' that is an int,
// and passes the ItemID of the ProspectorsTool (0xFB4) to the constructor of its parent class, BaseBashing.
public ProspectorsTool(int uses) : base(0xFB4)
{
Weight = 9.0;
UsesRemaining = uses; //This sets the uses that are remaining to the amount passed to the constructor.
}
/*
More of the class would probably be here
*/
Now, with that new constructor defined, you can create a ProspectorsTool object with a custom-defined number of uses such as,
Code:
ProspectorsTool prospectorstool = new ProspectorsTool(200);
to create a new ProspectorsTool with 200 uses.
Now, lets say that you wanted a constructor that only the class it's defined in can use, that is, nothing outside of our class can create an object using that constructor. This can be accomplished by simply changing the Access-Specifier.
Code:
//We're using the same class as before
public class ProspectorsTool : BaseBashing, IUsesRemaining
{
/*
Some of the class would probably be here
*/
// This makes the ProspectorsTool available to be created in-game
[Constructable]
//This is the constructor we want other classes to use, that's why its 'public'
public ProspectorsTool(int uses) : base(0xFB4)
{
Weight = 9.0;
UsesRemaining = uses;
}
// We don't want to let other classes set the weight of a ProspectorsTool, we want it to always be 9.0 like the other constructor
// makes it but we want to use this as a quick way of setting out weight on a new ProspectorsTool in this class, so we make this
// constructor 'private'
private ProspectorsTool(int weight) : base(0xFB4)
{
Weight = weight;
UsesRemaining = 50;
}
/*
More of the class would probably be here
*/
In this case, the first constructor can be called from any other class to create an object, but our second constructor can only be called from within our class. (If you want to make a constructor than can be called from in a single class and any classes derived from it, make it 'protected' instead of 'public' or 'private')
I hope you have a better understand of constructors after reading this, they are an important part of almost any class that you may create or use.