Discussion in   Coding Corner   started     11 years ago   July 12, 2013, 11:33:06 PM   by   Kyn

Your First Script (C#)

Kyn
Offline
336 Posts
Topic :   Your First Script (C#)
11 years ago  July 12, 2013, 11:33:06 PM


I've decided it might be fun to create a basic script with you guys from start to finish. If you guys enjoy my mini-tutorial please let me know and I'll keep doing them from time to time.


Lets say for argument sake I want to create an object that players will double click to spawn a Paladin's Horse. Lets call it a "Paladin Stone"


Alright so to start I must first define what extra/additional name spaces I will be calling from in my script. To do this I must create my "use" assembly reference like this so that I may call from them as needed in my script.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


Alright, so now I've defined what I will be calling in my script I can now define the "item" namespace and create my opening bracket. As seen below.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{


Now I am ready to rock. Next I will define my items public class. Please note that a public class can be accessed by any other code in the same assembly that references it, that is why I am using "public" as opposed to "private" which may only be accessed by code in the same class or struct.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{
    public class PaladinStone : Item
    {


So as you can see above, I have created my "item" essentially and it will be using the reference : Item - Obviously because it is an item as opposed to a mobile/creature etc. Next I will give my item some properties. There are many ways to assign properties to an item for this tutorial I'll be using the simplest method.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{
    public class PaladinStone : Item
    {
        [Constructable]
        public PaladinStone() : base( 6249 )
        {
            Name = "Paladin's Stone";
            this.Weight = 1.0;
            this.Hue = 1153;
        }


So, you can see I've assigned multiple properties to my item. I've given it a base itemID which is shown as "base( 6249 )" I have given it a name "Paladin's Stone" and I have assigned it a weight and hue color. "this.weight = 1.0;" and "this.Hue = 1153;" Now I check to make sure I have encased my assigned variables in a starting and ending bracket underneath my public class stone. I've also made sure that my item is labeled as a [Constructable]


Next I will be creating a public override "void" this will set the limits of how I would like my item to be used.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{
    public class PaladinStone : Item
    {
        [Constructable]
        public PaladinStone() : base( 6249 )
        {
            Name = "Paladin's Stone";
            this.Weight = 1.0;
            this.Hue = 1153;
        }
       
        public override void OnDoubleClick( Mobile from )
  {


For the purposes of this item I've assigned it as: "Void OnDoubleClick" as you can see I want the double click to be from my mobile A.K.A Player. "( Mobile from ) I've also assigned my opening bracket underneath my override.


Now I will tell my item what I want it to do under certain circumstances and how I want it to be used.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{
    public class PaladinStone : Item
    {
        [Constructable]
        public PaladinStone() : base( 6249 )
        {
            Name = "Paladin's Stone";
            this.Weight = 1.0;
            this.Hue = 1153;
        }
       
        public override void OnDoubleClick( Mobile from )
  {
   
   if( IsChildOf( from.Backpack ))
   {


I only want my stone to be usable while in the players (mobiles) backpack, so I've added an if statement below my override "if( IsChildOf( from.Backpack ))" This tells my script that I only want it to be used inside a players backpack. Now before I define what I want my stone to do if it is NOT in a players backpack I must first assign a few more instructions of what to do if it IS in a players backpack, as seen below.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{
    public class PaladinStone : Item
    {
        [Constructable]
        public PaladinStone() : base( 6249 )
        {
            Name = "Paladin's Stone";
            this.Weight = 1.0;
            this.Hue = 1153;
        }
       
        public override void OnDoubleClick( Mobile from )
  {
   
   if( IsChildOf( from.Backpack ))
   {
     
    Mobile spawn;
    {
     spawn = new PaladinsHorse();
     spawn.MoveToWorld(new Point3D(from.X, from.Y, from.Z), from.Map);
     from.SendMessage ("As you grasp the stone in your hands a glimmer of light flashes out of the stone and onto the ground in front of you - A horse has appeared!");
    this.Delete();
    }


I have assigned some more instructions for my script to follow, I've instructed my script to spawn a "new PaladinHorse();" and I've specified where the horse will spawn in relation to the player. "spawn.MovetoWorld(new Point3d(from.x, from.y, from.z), from.Map;" This allows my horse to spawn regardless of the location I am in. I've also added a fun RP message to be displayed on use. "from.SendMessage ("As you grasp the stone in your hands a glimmer of light flashes out of the stone and onto the ground in front of you - A horse has appeared!"


Now that I've instructed my script on what it should do if it is in a players backpack I will next create an else statement that will tell my script what to do if it is not in a players backpack. This will also be the end of my script so I will create a closing bracket as well.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{
    public class PaladinStone : Item
    {
        [Constructable]
        public PaladinStone() : base( 6249 )
        {
            Name = "Paladin's Stone";
            this.Weight = 1.0;
            this.Hue = 1153;
        }
       
        public override void OnDoubleClick( Mobile from )
  {
   
   if( IsChildOf( from.Backpack ))
   {
     
    Mobile spawn;
    {
     spawn = new PaladinsHorse();
     spawn.MoveToWorld(new Point3D(from.X, from.Y, from.Z), from.Map);
     from.SendMessage ("As you grasp the stone in your hands a glimmer of light flashes out of the stone and onto the ground in front of you - A horse has appeared!");
    this.Delete();
    }
 
   }
   else
   {
    from.SendLocalizedMessage( 1042001 ); // That must be in your pack for you to use it.
   
   }
  }


Now you can see that I have selected a message to be displayed to the player pending that the stone is not in their backpack when double clicked. My localized message is "1042001" which will state: "That must be in your pack for you use it" I've also added my closing brackets and now all thats left to do is serialize my script so that it can be saved and stored properly within the client/server.


Code: [Select]
using System;
using Server;
using Server.Mobiles;


namespace Server.Items
{
    public class PaladinStone : Item
    {
        [Constructable]
        public PaladinStone() : base( 6249 )
        {
            Name = "Paladin's Stone";
            this.Weight = 1.0;
            this.Hue = 1153;
        }
       
        public override void OnDoubleClick( Mobile from )
  {
   
   if( IsChildOf( from.Backpack ))
   {
     
    Mobile spawn;
    {
     spawn = new PaladinsHorse();
     spawn.MoveToWorld(new Point3D(from.X, from.Y, from.Z), from.Map);
     from.SendMessage ("As you grasp the stone in your hands a glimmer of light flashes out of the stone and onto the ground in front of you - A horse has appeared!");
    this.Delete();
    }
 
   }
   else
   {
    from.SendLocalizedMessage( 1042001 ); // That must be in your pack for you to use it.
   
   }
  }
       
        public PaladinStone( Serial serial ) : base( serial )
        {
        }


        public override void Serialize( GenericWriter writer )
        {
            base.Serialize( writer );
            writer.Write( (int) 0 ); // version
        }


        public override void Deserialize( GenericReader reader )
        {
            base.Deserialize( reader );
            int version = reader.ReadInt();
        }
    }
}


I've now added serialization to my script and it is complete and ready for testing. Serialization is generally the same for all items and is simply a generic writer calling to write/assign its version "(int) 0 );" and what to do during deserialization. "int version = reader.ReadInt();"
Hopefully you guys enjoyed this mini tutorial. Please let me know if you'd like to learn more!

Grumpelstiltskin
Offline
15 Posts
#1 Re :   Your First Script (C#)
11 years ago  July 12, 2013, 11:45:06 PM

Thank you for this. It gives a very detailed description of what you do and what some of us want to do.

Edeltraud
Offline
17 Posts
#2 Re :   Your First Script (C#)
11 years ago  July 13, 2013, 12:05:03 AM

Wow brought back memories of learning about programing!  I highly respect your job and thank you for showing us so we can challenge ourselves to know more about the games we play!

pibb100k
Offline
49 Posts
#3 Re :   Your First Script (C#)
11 years ago  July 13, 2013, 10:48:50 AM

Marry me<3