# Adding Widgets

Now that DynamicHUD is aware of your mod, you can start adding widgets. Widgets are the components that will be displayed on your HUD.&#x20;

### Initializing Widgets

Widgets should be initialized in the `init` method of your `DynamicHudIntegration` implementation. This is where you set up widgets and their dynamic values using `DynamicValueRegistry`. Store widgets as fields for use in `configure`.

#### Example: Initializing and adding a TextWidget

```java
package your.mod.package;

import com.tanishisherewith.dynamichud.integration.DynamicHudIntegration;
import com.tanishisherewith.dynamichud.integration.DynamicHudConfigurator;
import com.tanishisherewith.dynamichud.utils.DynamicValueRegistry;
import com.tanishisherewith.dynamichud.widgets.TextWidget;

public class MyIntegration implements DynamicHudIntegration {
    TextWidget exampleWidget;
    DynamicValueRegistry registry;

    @Override
    public void init() {
        // Create a local registry
        registry = new DynamicValueRegistry("mymod");
        registry.registerLocal("greeting", () -> "Hello, " + DynamicHUD.MC.getSession().getUsername() + "!");

        // Initialize TextWidget
        exampleWidget = new TextWidget.Builder()
            .setX(300)
            .setY(100)
            .setDraggable(true)
            .rainbow(false)
            .registryKey("greeting")
            .registryID(registry.getId())
            .setModID("mymod")
            .shouldScale(false)
            .shadow(true)
            .textColor(new Color(0xFFFFFF))
            .build();
    }

    @Override
    public DynamicHudConfigurator configure(DynamicHudConfigurator configurator) {
        return configurator
            .addWidget(exampleWidget)
            .configureRenderer(renderer -> renderer.addScreen(TitleScreen.class))
            .withMoveableScreen(config -> new AbstractMoveableScreen(Text.literal("Editor Screen"), config.getRenderer()) {});
    }
}
```

**What’s Happening**:

* **Registry Setup**: Creates a local `DynamicValueRegistry` for mod-specific values and registers a `greeting` key to display the player’s username. You can learn more about it in the DynamicValueRegistry page
* **Widget Initialization**: Uses `TextWidget.Builder` to create a `TextWidget` with properties like position, draggability, and text color.
* **Configuration**: Adds the widget to the HUD via `DynamicHudConfigurator` in the `configure` method.

**Key Points**:

* **TextWidget.Builder**: Simplifies widget creation with a fluent API. Set properties like `setX`, `setY`, `registryKey`, and `registryID` for dynamic values.
* **DynamicValueRegistry**: Provides dynamic values for widgets. Use `registerLocal` for mod-specific values or `registerGlobal` with `modid:key` for shared values.
* **Storage**: Store widgets as fields to reuse in `configure`.

{% hint style="info" %}
Use `modid:key` for global registry keys (e.g., `mymod:greeting`) to avoid conflicts with other mods. It’s like labeling your shulker boxes—keeps things organized.
{% endhint %}

{% hint style="warning" %}
Always set `registryID` for local registry keys. Without it, the widget won’t find its dynamic value, displaying `null` or blank text.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tanishisherewith.gitbook.io/dynamic-hud/integrating-dynamichud/adding-widgets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
