> 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/gum-tool/code-tab.md).

# Code Tab

## Introduction

The Code tab provides generated code for your current Gum objects. The code tab is available if a Screen or Component is selected.

If you have not yet set up code generation, you may see buttons to help you with code generation.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-b8273d657a866e68d8c7877f4b4956331841379a%2F09_06%2034%2044.png?alt=media" alt=""><figcaption></figcaption></figure>

If you have already set up code generation, or if your Gum project does not have an associated .csproj file then you will see options for code generation.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-c4e2df6c1964dc38bea818359ec0f70a1fc0380a%2F09_06%2029%2020.png?alt=media" alt=""><figcaption><p>Code tab displaying generated code for the selected component</p></figcaption></figure>

This tab provides the following functionality:

* Immediate display of generated code for the selected object
* Ability to see generated code for individual instances in an object or for an entire component/screen
* Ability to generate code automatically for a variety of target platforms

If you are working with Gum in a C# environment then the Code tab can help you write Gum code.

## Viewing Generated Code

The Code tab automatically displays the selected Screen, Component, or instance. If you are using code generation just for previwing code, you should select the Manually Setu Code Generation button. This enables the modification of properties without generating any code.

## Previewing Instances

If you have a single instance selected, the preview window displays the code for creating the instance and assigning its variables. This is especially useful if you are unsure how to reproduce a particular layout in code. For example, the following image shows the generated code for a Text named TextInstance.

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-32ae00eb5806707575e51fe2b83afc159c89b7ec%2F09_06%2039%2027.png?alt=media" alt=""><figcaption><p>Generated code for a Text named TextInstance</p></figcaption></figure>

The generated code shows all of the assignments necessary to reproduce the current instance's layout. Keep in mind that only explicitly-set variables are displayed. Any default variables (variables which appear with a green background in the Variables tab) are not assigned in generated code.

## Previewing Entire Screens and Components

If a Screen or Component is selected, then an entire class for the component is displayed in the preview window. This generated code includes:

* `using` statements
* A `partial` class with the suggested name. The name may append the word "Runtime" to the Screen or Component name depending on the target platform.
* Inheritance depending on the current platform and base type.
* `enum` declaration for all categories
* Properties for each category including switch statements assigning all properties for each state
* A property for each instance in the Screen or Component
* Initialization of all variables including variables on the instances if using full code generation

## Automatic Saving of Generated Code

The Code tab supports the automatic copying of files to disk. By using this feature, C# projects can automatically stay in sync with Gum projects, eliminating the need to write custom Runtime objects.

{% hint style="info" %}
Projects should be backed up or committed to source control before enabling automatic code generation to make it easy to undo changes.
{% endhint %}

To set up code generation, either click the **Auto Setup Code Generation** button, or modify the values in the Code tab for code generation.

* For MonoGame/KNI/FNA projects, see the [tutorial on setting up code generation](/gum/code/getting-started/tutorials/gum-project-forms-tutorial/gum-screens.md).
* For FlatRedBall projects, code generation is automatically handled by the FlatRedBall Editor.

See the sections below for information about each code generation option.

### Code Project Root

The location of the folder containing the .csproj file. This path is used to determine where to generate the code files. Gum generates two folders at this location:

* Components
* Screens

Gum also reads the .csproj in this folder to detect which version of the Gum runtime your game references, so keep this pointed at the .csproj folder. To put generated code somewhere else, use Generated Code Folder.

If this folder has several .csproj files, Gum reads `Assembly-CSharp.csproj` first, then the one with the shortest name. To use a different .csproj, set `CsprojPath` in `ProjectCodeSettings.codsj` to its path relative to the project file (.gumj or .gumx), for example `"CsprojPath": "../MyGame.Ui.csproj"`.

{% hint style="info" %}
The `CsprojPath` setting is available in October 2026, or now if building Gum from source.
{% endhint %}

### Generated Code Folder

An optional folder, relative to the Code Project Root, that Gum writes the Components and Screens folders into. For example, `Gum/Generated` writes a component named Button to `<Code Project Root>/Gum/Generated/Components/Button.Generated.cs`. Leave it empty to generate directly in the Code Project Root.

This setting only moves files. Namespaces are still built from the Root Namespace and the element's folder in the Gum project.

If an absolute path is entered, it is saved to a relative path so that generation works for all users working on a project regardless of where a project is cloned even though it appears absolute in Gum. For example: `C:\Users\Owner\Documents\GitHub\Gum\Samples\MonoGameGumCodeGeneration\`

Since the path is saved as relative to your project file location, this path will break if you move your Gum project to a new location. Be sure to update this if you are moving your project file.

### Generated Code Folder Prefix

Text put in front of the Components and Screens folder names. Leave it empty to use `Components` and `Screens`.

* A prefix ending in a slash, such as `GumCodeGen/`, puts both folders inside a folder with that name: `GumCodeGen/Components/Button.Generated.cs` and `GumCodeGen/Screens/MainMenu.Generated.cs`.
* A prefix without a slash, such as `Gum`, renames the folders: `GumComponents/Button.Generated.cs` and `GumScreens/MainMenu.Generated.cs`.

The prefix is applied inside the Generated Code Folder (or the Code Project Root if that is empty). Changing it offers to move your existing generated and custom code files to the new location, and a copy of every file is kept so the move can be undone with **Content > Restore Last Code File Migration**. Folders left empty by the move are removed. `StandardElements.Generated.cs` stays directly in the output folder.

Like Generated Code Folder, this only moves files. Namespaces are not affected; use Root Namespace and Append Folder to Namespace to control them.

### Output Library

New projects use **Gum Forms (recommended)** regardless of the runtime. Change the Output Library only if you need one of the deprecated options below.

* **Gum Forms (recommended)** is the preferred code generation. It works with MonoGame, KNI, FNA, Skia, Raylib, Silk.NET, Stride, and Unity. This code generation generates code with classes containing properties which inherit from FrameworkElement such as Button and Textbox wherever possible. If a non-forms instance (such as a Sprite or Text instance) is added to a screen or component, then code will *fall back* to generating non-forms properties (such as SpriteRuntime or TextRuntime).
* **MonoGame (deprecated)** generates code without creating forms controls. Use this if your game does not use Forms, or if your game predates Forms support in MonoGame.
* **SkiaSharp (deprecated)** generates code without creating forms controls, for runtimes which use SkiaSharp for graphics. Prefer **Gum Forms (recommended)**, which also works on Skia.
* **Raylib (deprecated)** generates code for projects using the Raylib runtime. Raylib code generation currently supports only the **Reference Loaded Gum Project** instantiation type (see below); the **Fully in Code** type is not yet supported and Gum displays a warning if you select it.
* **Silk.NET (deprecated)** generates code for projects using the Gum.SilkNet runtime. Like Raylib, it currently supports only the **Reference Loaded Gum Project** instantiation type; the **Fully in Code** type is not yet supported and Gum displays a warning if you select it.

{% hint style="info" %}
The **Raylib** Output Library is available in the Gum July 2026 release and newer. The **Silk.NET** Output Library is available in the Gum August 2026 release and newer.
{% endhint %}

Additional libraries may be added in the future. If your project needs support for code generation and you are using a library that is not supported, please contact the Gum team on Discord or GitHub.

### Object Instantiation Type

This option controls how much code is generated by Gum. The following options are available:

#### Reference Loaded Gum Project

This generates minimal code for access to objects. Specifically this generates:

* Instantiation of Screens and Components using a strongly-typed class
* Access to instances through strongly typed property names
* Setting of states through enums

This approach allows for the customization of Gum files without requiring full code regeneration. Games which use this type of code generation can still support modding, so long as the modified files do not remove instances or change their names. This type of code generation still requires the loading of the Gum project (.gumj or .gumx and associated files).

#### Fully in Code

This option enables working in Gum to create layouts which will work fully in code without loading a .gumj file. This is especially important if you are working on a platform with limited IO access. Generated code can run faster than loading a .gumj file since it does not require file IO, XML parsing, and reflection.

For more details see the [Runtime Generation Details](/gum/gum-tool/code-tab/runtime-generation-details.md) page.

### Project-wide Using Statements

Add the following using statement at the end of the Project-wide Using Statement box so that references to standard runtime types are found.

```csharp
using Gum.GueDeriving;
```

If you plan on creating Screens, you should also add using statements for your component runtimes

```csharp
using {YourProjectNamespace}.Components;
```

### Root Namespace

Enter the project's Root Namespace, such as `MyGame`. Gum prefixes the generated code namespace with this entered namespace.

### Append Folder to Namespace

This option controls whether the folder names of screens and components should be added to a namespace.

For example, consider a component named ButtonStandard inside the Components/Controls folder. If this option is checked (recommended), then the class is generated with the `Contrlos` folder included in the namespace:

```csharp
namespace MyProject.Components.Controls;
```

If this option is unchecked, then folders are not included in the namespace, so the generated code would include the following namespace:

```csharp
namespace MyProject.Components;
```

### Default Screen Base

Enter the type that you would like all screen runtimes to inherit from. If you're not sure what to enter, then use `Gum.Wireframe.GraphicalUiElement`. If your game uses a custom class that you have written for all screens, then use the name of that class. You can switch to custom classes later as your project grows.

Note that Gum may choose to override this base class in certain conditions including:

* If your project is using Forms
* If your screen inherits from another screen

### Generation Behavior

This controls when Gum generates code for a component.

* NeverGenerate - generated code is not saved for this component
* GenerateManually - generated code is only saved for this element if the **Generate Code** button is clicked, or if it is a requirement for another component or screen which is generated.
* GenerateAutomaticallyOnPropertyChanged - this is generated whenever a change is made to this element in Gum.


---

# 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/gum-tool/code-tab.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.
