C# language features (original) (raw)

This page provides an overview of the commonly used features of both C# and Godot and how they are used together.

Type conversion and casting

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

var mySprite = GetNode("MySprite"); mySprite.SetFrame(0);

The method GetNode() returns a Node instance. You must explicitly convert it to the desired derived type, Sprite2D 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 Sprite2D. You would use it instead of the as operator if you are pretty sure it won't fail.

Sprite2D mySprite = (Sprite2D)GetNode("MySprite"); mySprite.SetFrame(0);

Using the AS operator

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

Sprite2D mySprite = GetNode("MySprite") as Sprite2D; // Only call SetFrame() if mySprite is not null 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.

Sprite2D mySprite = GetNode("MySprite"); mySprite.SetFrame(0);

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

Sprite2D mySprite = GetNodeOrNull("MySprite"); // Only call SetFrame() if mySprite is not null mySprite?.SetFrame(0);

Type checking using the IS operator

To check if the node can be cast to Sprite2D, you can use the is operator. The is operator returns false if the node cannot be cast to Sprite2D, otherwise it returns true. Note that when the is operator is used against nullthe result is always going to be false.

if (GetNode("MySprite") is Sprite2D) { // Yup, it's a Sprite2D! }

if (null is Sprite2D) { // This block can never happen. }

You can also declare a new variable to conditionally store the result of the cast if the is operator returns true.

if (GetNode("MySprite") is Sprite2D mySprite) { // The mySprite variable only exists inside this block, and it's never null. mySprite.SetFrame(0); }

For more advanced type checking, you can look into Pattern Matching.

Preprocessor defines

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

Examples

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

public override void _Ready()
{

#if (GODOT_MOBILE || GODOT_WEB) // Use simple objects when running on less powerful systems. SpawnSimpleObjects(); #else SpawnComplexObjects(); #endif }

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

public void MyPlatformPrinter()
{

#if GODOT GD.Print("This is Godot."); #elif UNITY_5_3_OR_NEWER print("This is Unity."); #else throw new NotSupportedException("Only Godot and Unity are supported."); #endif }

Or you can write scripts that target multiple Godot versions and take advantage of features that are only available on some of those versions:

public void UseCoolFeature()
{

#if GODOT4_3_OR_GREATER || GODOT4_2_2_OR_GREATER // Use CoolFeature, that was added to Godot in 4.3 and cherry-picked into 4.2.2, here. #else // Use a workaround for the absence of CoolFeature here. #endif }

Full list of defines

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

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