> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/getting-started/tutorials/gum-project-forms-tutorial/gum-screens.md). Agents can also search and read this documentation through its [MCP server](https://docs.flatredball.com/gum/~gitbook/mcp).

# Gum Screens and Code Generation

## Introduction

Gum screens are *top level* items which can contain instances of Gum objects. We'll be creating our first Gum screen in this tutorial. We'll also load this screen in code and work with gum objects.

## Creating a Screen in the Gum Tool

To add a new Screen:

1. Open the project in the Gum tool
2. Right-click on the Screens folder and select Add Screen

   <figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-273d125e010b97435b57eb8fda7ddce6edcf4c7f%2F10_05%2020%2015.png?alt=media" alt=""><figcaption><p>Add Screen right-click item</p></figcaption></figure>
3. Name the screen TitleScreen and click OK

   <figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-15cb00b408b91396a6b44bb1dfdd6abc16ce8cf1%2Fimage.png?alt=media" alt=""><figcaption><p>Enter the new screen name and click OK</p></figcaption></figure>

The newly created TitleScreen is now in the Screens folder.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-c5096b507a64f187d3e4678a948bb9ca09e11c2d%2F10_05%2021%2055.png?alt=media" alt=""><figcaption><p>TitleScreen in Gum</p></figcaption></figure>

## Adding Instances

We can add new instances to Gum Screen by drag+dropping the files onto the Game screen.

Add a ButtonStandard to TitleScreen.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-8d1fe98e15eaf11ed65e3aef0ee4e809a7145d7b%2F10_05%2026%2049.gif?alt=media" alt=""><figcaption><p>Drag+drop ButtonStandard onto TitleScreen to create a Text instance</p></figcaption></figure>

Instances can also be created by selecting the TitleScreen, then drag+dropping the item in the editor window.

Add a second ButtonStandard instance by dropping Components/Controls/ButtonStandard onto TitleScreen.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-7da61109270436f1b8612c92f7914be7d7ec4f80%2F10_05%2027%2050.gif?alt=media" alt=""><figcaption><p>Drag+drop a component onto the Editor tab to add it to TitleScreen</p></figcaption></figure>

Be sure to select the TitleScreen first, then drag+drop. If you click the component instead, then it will be selected, so you must re-select the TitleScreen.

The Gum tool includes lots of functionality for creating and customizing UI. For a more complete tutorial covering the Gum tool, see [Working with Gum](https://docs.flatredball.com/gum/gum-tool/tutorials-and-examples/working-with-gum.md). Feel free to spend some time creating your TitleScreen.

## Setting Up Code Generation

Code generation reduces the amount of code you have to write to work with Gum. It results in strongly-typed screens and components, reducing the chances of typos and catching errors at runtime.

To set up code generation for your project:

1. Select a Screen such as TitleScreen - the Code tab does not appear unless a Screen or Component is selected
2. Click on the Code tab
3. Click the **Auto Setup Code Generation** button\\

   <figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-77ad4becb49a172fc0bb71834cd6176c1ebe3d99%2F10_05%2034%2013.png?alt=media" alt=""><figcaption></figcaption></figure>
4. Gum locates your .csproj file and sets up default values. To review or change them, click **Edit project settings...** to open the **Code generation** page of **Edit > Settings...**.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-dbb91107c8cd9e9629edf6bcd74107befcd6ced2%2F29_15%2000%2030.png?alt=media" alt=""><figcaption><p>Code tab in Gum</p></figcaption></figure>

{% hint style="warning" %}
If you do not see the **Auto** button, or Gum says it found no project file, your Gum project is saved in a location that is not inside the folder containing your game's .csproj. Save Gum projects in a subfolder of where your .csproj is located, usually in a folder inside the Content folder.
{% endhint %}

Once you have set this up, you can click the **Generate** button to generate code for your TitleScreen.

Note that Gum also generates code for all components referenced by your project, so it also generates ButtonStandard. If asked, click **Yes** to generate all necessary components.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-13767f34a8b7845b464eab78036c1a7d35671876%2F29_15%2002%2023.png?alt=media" alt=""><figcaption><p>Generate Code and click Yes to generate all components referenced by the TitleScreen</p></figcaption></figure>

You can verify that your code has been properly generated by looking at your project in Visual Studio or your IDE of choice.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-a9f358c667f22e9b92216c44e20a1191cff733d4%2F29_15%2004%2015.png?alt=media" alt=""><figcaption><p>ButtonStandard and TitleScreen classes</p></figcaption></figure>

Note that each screen and component generates two .cs files:

1. YourComponentOrScreenName.cs - this is called *custom code* and is where you can write code safely
2. YourComponentOrScreenName.Generated.cs - this is called *generated code* and is written by Gum. This code will be re-generated when you make changes in Gum, so if you write any code here it will be lost on re-generation.

By default both files sit side by side. To keep them in separate folders, set the Generated Code Folder and Custom Code Folder, as described in the [Code tab](https://docs.flatredball.com/gum/gum-tool/code-tab.md#folder-layout) page.

### Troubleshooting Code Generation

If code generation seems to not be working correctly, this section can provide some troubleshooting tips.

#### Verifying Output Library

Verify that the Output Library value is set to Gum Forms (recommended). If you are not using this Output Library, code generation may not work properly.

You can also verify that your screens and components classes match the names in Gum without any suffix (such as Runtime).

#### Resetting Code Generation

If Gum has generated your code incorrectly, you can take the following steps to reset code generation:

1. Locate the Screens and Components folders in your code project (inside the Generated Code Folder and Custom Code Folder if you set them) and delete them. This will delete all generated and custom code. Be careful - you may want to create a backup if you have any custom code that you would like to preserve.
2. Look for `ProjectCodeSettings.codsj` next to your Gum project file. Delete this file.
3. Close and re-open Gum
4. Select one of your screens or components
5. Click on the Code tab
6. Click the **Auto** button
7. Click the **All** button next to **Generate**
8. Click the **Generate** button

## Showing a Gum Screen in Your Game

To show the TitleScreen in game, we can use the newly-generated TitleScreen class as shown in the following snippet:

{% tabs %}
{% tab title="Full Code" %}

```csharp
// Initialize
var screen = new TitleScreen();
screen.AddToRoot();
```

{% endtab %}

{% tab title="Diff" %}

```diff
// Initialize
+var screen = new TitleScreen();
+screen.AddToRoot();
```

{% endtab %}
{% endtabs %}

The game now displays the TitleScreen.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-72b5e2df38944df66ac3642bf5c20a66c89b3e52%2F29_15%2008%2040.png?alt=media" alt=""><figcaption><p>TitleScreen displayed in a MonoGame project</p></figcaption></figure>

The new code has 2 calls:

* new TitleScreen() - this instantiates the TitleScreen using the generated code. As mentioned above, this is a type-safe way to access the Gum screen. Note that it is possible to access the screen using strings as well, but doing so can be more error prone.
* screen.AddToRoot() - this call adds the screen to the root Gum object so that it is drawn and so it receives events. For example, we can mouse over and click on our buttons and they respond visually.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-09f0100a7bb045bc1856fe5a7d9b36c9e8df4d9c%2F29_15%2010%2049.gif?alt=media" alt=""><figcaption><p>Buttons responding to cursor hover and click</p></figcaption></figure>

## Accessing Instances in Code

We can access the buttons in our TitleScreen using the generated code. We can access the buttons in our game code, or we can access them in the TitleScreen.cs file. Both are perfectly fine, and which you do depends on whether you intend to have the Gum screen contained in a higher-level object that is specific to your game (such as a game scene), or if you want to keep all Gum code within the screen's custom code file. Either way, the code to interact with items is similar.

For example, the following code could be used to set the time when a button was clicked in your game code. Notice that we access the button using the same name as is given in the Gum screen. If you changed the name of your button to something other than ButtonStandardInstance, then you should use that new name in code too:

{% tabs %}
{% tab title="Full Code" %}

```csharp
// Initialize
var screen = new TitleScreen();
screen.ButtonStandardInstance.Click += (_, _) =>
    screen.ButtonStandardInstance.Text = DateTime.Now.ToString();
screen.AddToRoot();
```

{% endtab %}

{% tab title="Diff" %}

<pre class="language-diff"><code class="lang-diff">// Initialize
var screen = new TitleScreen();
<strong>+screen.ButtonStandardInstance.Click += (_, _) =>
</strong>+    screen.ButtonStandardInstance.Text = DateTime.Now.ToString();
screen.AddToRoot();
</code></pre>

{% endtab %}
{% endtabs %}

The button now responds to clicks by updating its text.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-2dfad26e8426ff4eafde1e2e8a7ed727b0fd0eb0%2F29_15%2015%2048.gif?alt=media" alt=""><figcaption><p>Button updating its Text to the current time</p></figcaption></figure>

Alternatively, you can write code directly in the TitleScreen. Remember, TitleScreen.cs exists so you can write your own code. It contains a CustomInitialize method which is called when the Screen is first created. We can assign events in CustomInitialize as shown in the following code snippet:

{% tabs %}
{% tab title="Full Code" %}

<pre class="language-csharp"><code class="lang-csharp">partial class TitleScreen
{
    partial void CustomInitialize()
    {
<strong>        ButtonStandardInstance1.Click += (_, _) =>
</strong>            ButtonStandardInstance1.Text = DateTime.Now.ToString();
    }
}
</code></pre>

{% endtab %}

{% tab title="Diff" %}

<pre class="language-diff"><code class="lang-diff">partial class TitleScreen
{
    partial void CustomInitialize()
    {
<strong>+       ButtonStandardInstance1.Click += (_, _) =>
</strong>+           ButtonStandardInstance1.Text = DateTime.Now.ToString();
    }
}
</code></pre>

{% endtab %}
{% endtabs %}

Remember, add your code to CustomInitialize, not the constructor.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-bb45801f3a5f90872113876dcbde5d30fc69469e%2F29_15%2019%2018.gif?alt=media" alt=""><figcaption><p>Button click event handled in the TitleScreen class</p></figcaption></figure>

## Conclusion

This tutorial showed how to load a Gum screen in code and how to interact with objects. The next tutorial covers how to work with some of the more common component types.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/gum/code/getting-started/tutorials/gum-project-forms-tutorial/gum-screens.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
