C# features

    C# is a statically typed language. Therefore, you can’t do the following:

    The method returns a Node instance. You must explicitly convert it to the desired derived type, Sprite in this case.

    For this, you have various options in C#.

    Casting and Type Checking

    Throws InvalidCastException if the returned node cannot be cast to Sprite. You would use it instead of the as operator if you are pretty sure it won’t fail.

    1. Sprite mySprite = (Sprite)GetNode("MySprite");
    2. mySprite.SetFrame(0);

    Using the AS operator

    The as operator returns null if the node cannot be cast to Sprite, and for that reason, it cannot be used with value types.

    1. Sprite mySprite = GetNode("MySprite") as Sprite;
    2. // Only call SetFrame() if mySprite is not null
    3. mySprite?.SetFrame(0);

    Using the generic methods

    Generic methods are also provided to make this type conversion transparent.

    GetNode<T>() casts the node before returning it. It will throw an InvalidCastException if the node cannot be cast to the desired type.

    1. Sprite mySprite = GetNode<Sprite>("MySprite");
    2. mySprite.SetFrame(0);

    GetNodeOrNull<T>() uses the as operator and will return null if the node cannot be cast to the desired type.

    To check if the node can be cast to Sprite, you can use the is operator. The is operator returns false if the node cannot be cast to Sprite, otherwise it returns true.

    1. if (GetNode("MySprite") is Sprite)
    2. {
    3. // Yup, it's a sprite!
    4. }

    For more advanced type checking, you can look into .

    For a complete C# example, see the Handling a signal section in the step by step tutorial.

    Declaring a signal in C# is done with the [Signal] attribute on a delegate.

    1. [Signal]
    2. delegate void MySignal();
    3. [Signal]
    4. delegate void MySignalWithArguments(string foo, int bar);

    These signals can then be connected either in the editor or from code with Connect. If you want to connect a signal in the editor, you need to (re)build the project assemblies to see the new signal. This build can be manually triggered by clicking the “Build” button at the top right corner of the editor window.

    1. public void MyCallback()
    2. {
    3. GD.Print("My callback!");
    4. }
    5. {
    6. GD.Print("My callback with: ", foo, " and ", bar, "!");
    7. }
    8. public void SomeFunction()
    9. {
    10. instance.Connect("MySignal", this, "MyCallback");
    11. instance.Connect(nameof(MySignalWithArguments), this, "MyCallbackWithArguments");
    12. }

    Emitting signals is done with the EmitSignal method.

    Notice that you can always reference a signal name with the nameof keyword (applied on the delegate itself).

    It is possible to bind values when establishing a connection by passing a Godot array.

    1. public int Value { get; private set; } = 0;
    2. private void ModifyValue(int modifier)
    3. {
    4. Value += modifier;
    5. }
    6. public void SomeFunction()
    7. {
    8. var plusButton = (Button)GetNode("PlusButton");
    9. var minusButton = (Button)GetNode("MinusButton");
    10. plusButton.Connect("pressed", this, "ModifyValue", new Godot.Collections.Array { 1 });
    11. minusButton.Connect("pressed", this, "ModifyValue", new Godot.Collections.Array { -1 });
    12. }

    Signals support parameters and bound values of all the built-in types and Classes derived from . Consequently, any Node or Reference will be compatible automatically, but custom data objects will need to extend from Godot.Object or one of its subclasses.

    1. {
    2. public string Field1 { get; set; }
    3. public string Field2 { get; set; }
    4. }

    Finally, signals can be created by calling AddUserSignal, but be aware that it should be executed before any use of said signals (with Connect or EmitSignal).

    1. {
    2. AddUserSignal("MyOtherSignal");
    3. EmitSignal("MyOtherSignal");
    4. }

    Godot has a set of defines that allow you to change your C# code depending on the environment you are compiling to.

    If you created your project before Godot 3.2, you have to modify or regenerate your csproj file to use this feature (compare <DefineConstants> with a new 3.2+ project).

    For example, you can change code based on the platform:

    Or you can detect which engine your code is in, useful for making cross-engine libraries:

    1. public void MyPlatformPrinter()
    2. {
    3. #if GODOT
    4. GD.Print("This is Godot.");
    5. #elif UNITY_5_3_OR_NEWER
    6. print("This is Unity.");
    7. #else
    8. throw new InvalidWorkflowException("Only Godot and Unity are supported.");
    9. #endif
    10. }

    Full list of defines

    • GODOT is always defined for Godot projects.

    • One of GODOT_64 or GODOT_32 is defined depending on if the architecture is 64-bit or 32-bit.

    • One of GODOT_X11, GODOT_WINDOWS, GODOT_OSX, GODOT_ANDROID, GODOT_IOS, GODOT_HTML5, or GODOT_SERVER depending on the OS. These names may change in the future. These are created from the get_name() method of the singleton, but not every possible OS the method returns is an OS that Godot with Mono runs on.

    When exporting, the following may also be defined depending on the export features:

    • One of GODOT_PC, GODOT_MOBILE, or GODOT_WEB depending on the platform type.

    • One of GODOT_ARM64_V8A or GODOT_ARMEABI_V7A on Android only depending on the architecture.

    • One of GODOT_ARM64 or GODOT_ARMV7 on iOS only depending on the architecture.

    To see an example project, see the OS testing demo: https://github.com/godotengine/godot-demo-projects/tree/master/misc/os_test