Skip to content

Documentation

Grant Shotwell edited this page Jul 22, 2020 · 10 revisions

Syntax

The syntax for MCSharp has been designed to be as close as possible to C# syntax.

Classes and Structs

Structs represent a set of variables, while classes represent an entity that holds a set of variables. (Note: classes have not been implemented yet.)

Defining a Struct

Struct can be defined and used the same way as C# types.

public struct MyStruct {
    ...
}

Members

Members (fields, properties, methods) can be defined and used in the same way as C# members.

Fields

Fields are the values that are saved within an object. Everything else within an object definition are only ways of manipulating its fields.

It is generally good practice to keep your fields private, and use properties to modify fields.

private int timeOfCreation = World.GetTime("gametime");

Properties

A property is code that changes the value of and/or gives the value of something the declaring object represents. You can define 'get' and 'set' methods for a property, but you don't need to define both. Like normal methods, the code within a property is in its own scope.

It is generally good practice to use properties that access fields, instead of accessing your fields directly.

public static PlayerCount {
    get {
        Objective dummy = new Objective("dummy");
        Scoreboard.SetScore("@a", 1);
        return Scoreboard.GetScore("@a", dummy);
    }
}

Properties are generally used to access private fields, and create representations of values that have no direct field to match.

private int radius;
public Radius {
    get { return radius; }
    set { radius = value; }
}
public Diameter {
    get { return radius * 2; }
    set { radius = value / 2; }
}

Methods

A method is code that does something; usually to the given arguments or the object itself. Methods can also return a value. The code within a method is within its own scope.

public int OnePlus(int n) {
    int m = n + 1;
    return m;
}

Built-in Library

Many types are built-in to run the various commands Minecraft has.

Primitives

Static Objects

Clone this wiki locally