Announcement

Collapse
No announcement yet.

Code Structure Basics

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Code Structure Basics

    Original Post by Talow

    This tutorial will however cover some information every scripter here should really understand. The first thing we'll be covering is a bit of vocab.

    Object: Objects are also called instances, and they can be stored in either a named variable or in an array or collection. https://msdn.microsoft.com/en-us/library/ms173110.aspx

    Class: A class enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. classes support inheritance, a fundamental characteristic of object-oriented programming. https://msdn.microsoft.com/en-us/library/x9afc042.aspx

    Inheritance: Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. https://msdn.microsoft.com/en-us/library/ms173149.aspx

    Public: is an access modifier for types and type members. Public access is the most permissive access level. There are no restrictions on accessing public members. https://msdn.microsoft.com/en-us/library/yzh058ae.aspx

    Private: is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class in which they are declared

    These are all basic concepts and understanding of these will help everyone be a good programmer/scripter and communicate with the community. Since we all work with C#, Knowing what we are dealing with is a must. These are the very basic concepts you should have before you even change a value in any script IMO. It's about how the code works and if you want to change how the code works you should know the basics of the code.

    So I've touch a bit of object and class in the OOP post (read it if you need further understanding of that as we'll be talking about OOP design, but not directly pointing any of it out).

    So Inheritance is the next part to get to. Every class WE make is based off of another class, weather we tell it to be or not, it's just a function of C#. Object is the foundation we start with...

    public class Demo {}

    This class, Demo, is defined and is created off of the object class. So without doing anything our class already has things it's able to do, and these things are able to be done by all things that come from the object class.
    Equals
    Finalize
    GetHashCode
    GetType
    MemberwiseClone
    ReferenceEquals
    ToString
    These are methods that every object has and when you create your class you have access to them too from your class, because your class inherited everything from object at this point. This will make object the parent or base class, and your class a child of the object class.

    Knowing that we have the ability to have the code from a parent is nice. The true power is that you can tell your class what other class to use as it's base. Again this is something most of us do daily but don't understand it truly. Lets say we're going to create a item in game. The start of every class looks like what I've already shown above but continues a bit more before the first "{".

    public class Demo : Item {}

    So you can see here this is how we can use the power that the class Item (a part of our core servers) contains, and then extend it further. The ":" is how we tell C# that we want to base our class from the Item class, and create that inheritance that is all so powerful. Now that our class is a child of Item we have access, with no extra work, to all the public variables and methods that are defined in the Item class.

    This leads me to public and private. These are keywords that tell C# who's allowed to access the information. There are a few other keywords but these two are the basics.
    Public access is just that, anyone and everyone may see it and play with it. There are ways to change how they can play with it but I'm not going to cover that part here.
    Private access is also just that, you hold it near and dear to you so no one else may even look at it, much less change it. It's yours and yours alone.

    So continuing our example you can see what I said to the public stuff, because Item won't let you play with the private information it hold, but public, sure go for it, you can have it too.

    So in this world of parents and children we have stuff to change that normally would not be. For example items have a definition of if it will or will not handle the speech of a mobile. The "HandlesOnSpeech" is used for this reason but in the Item's class when we go and get it all we'll ever get is "false". So is the function pointless?

    public virtual bool HandlesOnSpeech { get { return false; } }

    Yes it's true this will do one thing and one thing only return false when we try and access it. But normally our items don't handle speech, I mean you can't talk to your katanas or the stool. But we can talk to keyword teleporters and the reason we can talk to some of them is because these items take advantage of the virtual keyword. The virtual keyword allows the item class to give a default reply as well as allow classes that inherit from it to have the same default if they chose to leave it be. If they want to change it however they can. By overriding the member HandlesOnSpeech you can create items that listen to mobiles, like the keyword teleporter. override is the keyword that you can use on a virtual member to do this.

    public override bool HandlesOnSpeech { get { return true; } }

    So we've learned now that code is built on top of other code to make things easier in the future with inheritance. This means that you can create base classes yourself too and base your classes on top of another. an example of this would be when you want to have 10 or so different monsters that have the same stats but just different body values, you could create a base script to apply all the stat stuff for you then make the classes for the creatures use the base, and simply apply the bodyvalue.

    This structure is the power of C# and we already use it. This is really just to help us all understand what we are already doing!
    RunUO - Ultima Online Emulator - www.runuo.net

  • #2
    check this Object Oriented Programming tutorial....

    http://net-informations.com/faq/oops/oops.htm

    Lee

    Comment


    • #3
      I'm new to coding and I got a little confused during this, but I want to thank thee for helping me know some vocab, before I start coding.
      I am the bone of my sword.
      Steel is my body, and fire is my blood.
      I have created over a thousand blades.
      Unknown to death, nor known to life.
      Have withstood pain to create many weapons.
      Yet, those hands will never hold anything.
      So as I pray, unlimited blade works.

      Comment

      Working...
      X