Things I wish I would've known earlier

Things I wish I would've known earlier

Postby ismellike » 09 Oct 2013, 03:40

Regions
These guys will help you out in organizing.
Code: Select all
       
        #region playerRegion
        public override void Use(Player p, string message)
        {
        }
        public override void Help(Player p)
        {
        }
        #endregion

You can put these in effect by using "Ctrl + M, O" in Visual Studio.

Parameters
By parameters, I mainly mean the "??", "?:", "out", and "@".
[*]??
Code: Select all
       
        public override void Use(Player p, string message)
        {
            p.realName = (string)p.ExtraData["turtleName"] ?? "I have no turtle name ):";
        }

The ?? parameter will try to use the object on the left, but if it is null/empty, it will use the object on the right.

[*]?:
Code: Select all
        public override void Use(Player p, string message)
        {
            p.realName = p.realName != (string)p.ExtraData["turtleName"] ? (string)p.ExtraData["turtleName"] : p.realName ;
        }

The ?: is somewhat similar to ??, but what the ?: does is that it is like a little if/else statement in itself.
Study the code above and compare it to what I have below; they do the same, but one is shorter.
Code: Select all
           
           if (p.realName != (string)p.ExtraData["turtleName"])  // left of ?
                p.realName = (string)p.ExtraData["turtleName"]; //left of :
            else
                p.realName = p.realName; //right of :


[*]out
Code: Select all
       
       public override void Use(Player p, string message)
        {
            string myTurtleName;
            int money;
            getATurtleName(p.money, out myTurtleName, out money);
            p.money = money;
            p.ExtraData.Add("turtleName", myTurtleName);
        }
        public void getATurtleName(int money, out string turtleName, out int balance)
        {
            if (money <= 0)
                turtleName = "Poor Noob Turtle";
            else if (money >= 1000)
                turtleName = "Pro Turtle";
            else
                turtleName = "Normal Turtle";
            balance = money / 2;
            System.IO.File.AppendAllText("extra\\TurtleLog.txt", turtleName);
        }

The out parameter allows you to get information from an action. This allows for a less cluttered code outside of the voids; You won't have to add a bunch of "int"s and "string"s to be able to use the information from a different void.
[*]@
Code: Select all
       
       public override void Init()
        {
            string @string = "TurtleLog";
            if (System.IO.File.Exists(@"extra\" + @string + ".txt"))
                System.IO.File.Create(@"extra\" + @string + ".txt");
        }

Take note how I am able to use a type as a type name by applying the @ before the type name. @ also gets the literal representation of a string; Normally, "extra\" would give you an error, but with @ it won't; \ allows escape sequences. @ also allows you to extend a string (showed in the example below).
Code: Select all
@"hi
this
is
an
extended
string with spaces"


[*]\
Code: Select all
       
       public override void Use(Player p, string message)
        {
            System.IO.File.WriteAllText(@"extra\" + p.name + ".txt",
                "Hi, I love turtles and ducks \r\n\tThey are so cool \r\n\t\tI love them so much\r\n\t\t\ttabs are awesome too");
        }

In the example above, I show you 3 escape sequences: \t , which makes tabs; \r, which brings you to the right hand side; and \n, which makes a new line. There are many more escape sequences

Delegates/Actions
Code: Select all
       
        System.Action action;
        public override void Use(Player p, string message)
        {
            action = new System.Action(delegate
            {
                p.realName = (string)p.ExtraData["turtleName"];
            });
            setTurtleName();
        }
        private void setTurtleName()
        {
            action.Invoke();
        }

Actions are similar to voids, but they store the values that are applied. setTurtleName() does not require Player p to be transferred.

Extensions

Code: Select all
public static class StaticClass
{
   public static void AddVars(this Player p)
   {
    p.ExtraData.Add("turtleName","Ricardo");
   }
}
public class CmdCommand : Command
{
   public override void Use(Player p, string message)
   {
     p.AddVars();
   }
}

The code speaks for itself, but extensions must always be in a static class and static themselves.
If anyone has anything they would like to add, be sure to edit this post and improve!
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Things I wish I would've known earlier

Postby joppiesaus » 09 Oct 2013, 13:08

Nice! By the way, you can use "System.Environment.NewLine" instead of "\n\r".
Example:
Code: Select all
"Hi \n\r there!"
"Hi" + System.Environment.NewLine + "there!"
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Things I wish I would've known earlier

Postby Leeizazombie » 23 Jan 2014, 10:59

I believe you use System.Environment.NewLine because it's using Framework 3 .NET
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Things I wish I would've known earlier

Postby joppiesaus » 25 Jan 2014, 14:54

What does "?"?
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Things I wish I would've known earlier

Postby Leeizazombie » 25 Jan 2014, 15:05

I think the target framework for MCDzienny is .NET Framework 3.0 or 3.5
Isn't that why Enivoriment.NewLine should be used? I had a problem trying to make new lines and that was why
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Things I wish I would've known earlier

Postby dzienny » 30 Jan 2014, 01:50

joppiesaus wrote:What does "?"?

It's a part of the conditional (ternary) operator:
Code: Select all
condition ? firstExpression : secondExpression;
User avatar
dzienny
Administrator
 
Posts: 1181
Joined: 23 Jan 2011, 14:27


Return to Knowledge Base

Who is online

Users browsing this forum: No registered users and 2 guests

cron