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.

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

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.

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.

Last updated

Was this helpful?