Things I wish I would've known earlier
Posted: 09 Oct 2013, 03:40
Regions
These guys will help you out in organizing.
You can put these in effect by using "Ctrl + M, O" in Visual Studio.
Parameters
By parameters, I mainly mean the "??", "?:", "out", and "@".
[*]??
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.
[*]?:
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.
[*]out
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.
[*]@
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).
[*]\
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
Actions are similar to voids, but they store the values that are applied. setTurtleName() does not require Player p to be transferred.
Extensions
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!
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!