How to use feature flags in GODOT Engine
Tags: godot, featuremanagement, featureflags, gaming
Published: 2023-06-24 16:17:19

According to an [article](https://www.cnet.com/tech/gaming/why-video-games-are-primed-for-a-big-comeback-in-2023/) published by CNET, the growth of the gaming industry is expected to increase. Due to this, new game titles are on the rise as greater demands are placed on gaming companies to remain competitive by keeping their users engaged with new features and updates. With the proper feature flagging mechanism, new features and updates can be effectively managed and released to users.

<!--truncate-->

## What is a Feature Flag?

Today, feature flags are known as the driving force behind feature rollouts. A feature flag is a boolean value linked to a feature. When the flag is false, the feature it controls is hidden. This brilliant yet simple mechanism can be integrated and used within [many programming languages and technologies](https://configcat.com/docs/sdk-reference/overview/) you may already be using. One of those technologies is an open source Gaming Engine called [GODOT](https://godotengine.org/).

## Pre-requisites

- [Visual Studio Code](https://code.visualstudio.com/download)
- Have [GODOT](https://godotengine.org/download/windows/) installed
- Have [.NET 7.0](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) installed

## Let's talk about GODOT

**GODOT** is an open-source gaming engine for developing 2D and 3D games. Compared to similar technologies like [Unity](https://unity.com/) and [Unreal Engine](https://www.unrealengine.com/), GODOT is lighter and less taxing on your hardware resources. It is the ideal choice for building simple games.

To demonstrate the usage of feature flags in this technology, I've created a [sample project](https://github.com/configcat-labs/using-feature-flags-in-godot-sample/tree/starter-code) to help you follow along.

## Setup and installation

To use the GODOT Engine, you'll need to install and set it up on your computer.

For this demo, I'll use the latest version of the GODOT Engine, `version 4.0.1 - .NET`.

**1.** [Download and install GODOT](https://godotengine.org/download/windows/). Your download will contain a zipped folder. Unzip the folder.

**2.** You can launch the GODOT Engine by running the application file from the extracted folder.

## Using the sample app

With GODOT installed, let's look at running the sample GODOT project.

**1.** Clone the [starter-code](https://github.com/configcat-labs/using-feature-flags-in-godot-sample/tree/starter-code) branch.

**2.** Launch the GODOT Engine, browse to the folder, and open the project.

![Opening the starter code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rx7iq7p32dmo4id91a8c.png)

**3.** If you'd like to use VS Code as your editor, you can configure it as an external editor, as shown below:


![Setting VS Code as GODOT external editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z0mrr4lrl6i3ot2u95b.png)

## Using feature flags in GODOT

One way to integrate and use feature flags in GODOT is to use a script. You can attach the script to one of your existing nodes, and when the node loads into your scene the script will be executed. Let's look at how to do this.

**1.** With the project open and the **Scene** tab selected, create a new node by clicking the **+ Other Node** button in the left pane.

**2.** Create a **Sprite 2D** Node. You can also create just about any node with an attached script.

![Creating a Sprite node](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1m48n2dhr6l5idh3tz6.png)

**3.** Right-click on the node to open the context menu, and select attach script as shown below:

![Attaching a node script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4r56ppdbx4ydtg8z3v8q.png)

The above will create a new file with the name `Sprite.cs` in your project folder. Let's look at how we can print a message to the console based on the value of a feature flag when the node is loaded.

**4.** Save the scene. **Scene** -> **Save Scene**.

### Setting up a feature flag

[ConfigCat](https://configcat.com/) offers a cloud-based solution for creating and managing feature flags. Let's use it to add a feature flag to use in GODOT.

**1.** To sign up for a ConfigCat account, click [here](https://app.configcat.com/auth/signup).

**2.** In the [Dashboard](https://app.configcat.com), create a feature flag with the following details:

![Adding a feature flag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2t74yhskh7k17wi44yv.png)

### Connecting GODOT to ConfigCat

To connect the GODOT script to ConfigCat for querying the status of the flag, we'll need to download a client SDK. Because our GODOT app uses C#, we can download and integrate [ConfigCat's .NET client SDK](https://configcat.com/docs/sdk-reference/dotnet/).

**1.** To install the SDK into our project, we'll first need to download and install Microsoft's [.NET 7.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/7.0).

**2.** After the installation completes, double-check that `dotnet` is installed by running this command:

```sh
dotnet --info
```

**3.** Launch a terminal window in the project folder, and run the following command to install the [ConfigCat .NET client SDK](https://configcat.com/docs/sdk-reference/dotnet/):

```sh
dotnet add package ConfigCat.Client
```

**4.** Open the `Sprite.cs` file in the project folder. At the top of the file, add the following line:

```c#
using ConfigCat.Client;
```

**5.** Let's create a `Ready()` method to execute when the node enters the scene. In the body of the class, add the following code:

```c#
public partial class Sprite : Sprite2D
{
    // Initialize ConfigCat client
    private IConfigCatClient configCatClient = ConfigCatClient.Get("YOUR_CONFIGCAT_SDK_KEY");

    private bool isMyGodotFeatureFlag;

    public override void _Ready()
    {
      
      isMyGodotFeatureFlag = configCatClient.GetValue("mygodotfeatureflag", false);

      if(isMyGodotFeatureFlag) {
        GD.Print("Your feature flag is enabled!!");
      } else {
        GD.Print("Your feature flag is disabled!!");
      }

    }
}

```

If you build and run the project by clicking the "*Play*" icon button at the top right, you should see the sprite node icon displayed in the window and the following message logged to your console:

![Console output from GODOT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxdl40vxpg8g0jd50qh5.png)

Let's take it a step further and make the note rotate when the feature flag is enabled. To do this, modify the `Sprite` class as follows:

```c#
public partial class Sprite : Sprite2D
{
  private int speed = 400;
  private int AngularSpeed = (int)Mathf.Pi;

  // Initialize ConfigCat client
  private IConfigCatClient configCatClient = ConfigCatClient.Get("YOUR_CONFIGCAT_SDK_KEY");

  private bool isRotateSpriteEnabled;

  public override void _Ready()
  {
    isRotateSpriteEnabled = configCatClient.GetValue("mygodotfeatureflag", false);

    if (isRotateSpriteEnabled)
    {
      GD.Print("Your feature flag is enabled!!");
    }
    else
    {
      GD.Print("Your feature flag is disabled!!");
    }


  }

  public override void _Process(double delta)
  {
    if (isRotateSpriteEnabled)
    {
      Rotation += AngularSpeed * (float)delta;
    }
  }
}
```

You can view the complete `Sprite.cs` file [here](https://github.com/configcat-labs/using-feature-flags-in-godot-sample/blob/master/Sprite.cs).

**6.** If you run the project again, and the feature flag is enabled, the sprite node will rotate:

![Rotating Sprite](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n84813cqllq2ud6yc2s7.png)

## Code

Feel free to check out the completed GODOT project [here](https://github.com/configcat-labs/using-feature-flags-in-godot-sample).

## Final thoughts

The flexible nature of feature flagging lends itself to many software technologies, and game development engines are no exception. You've seen how simple it is to add a feature flag to GODOT, and we've only scratched the surface of what is possible with feature flagging. With a feature flagging solution, you can enhance canary deployments, A/B testing, and software updates. One of the most sought-after benefits of flagging features is that you can switch them on and off remotely without touching your code.

ConfigCat also supports many other frameworks and languages. Check out the entire list of supported SDKs [here](https://configcat.com/docs/sdk-reference/overview/).

For more awesome content, keep up with ConfigCat on [Twitter](https://twitter.com/configcat), [Facebook](https://www.facebook.com/configcat), [LinkedIn](https://www.linkedin.com/company/configcat/), and [GitHub](https://github.com/configcat).