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 agreeting
key to display the player’s username. You can learn more about it in the DynamicValueRegistry pageWidget Initialization: Uses
TextWidget.Builder
to create aTextWidget
with properties like position, draggability, and text color.Configuration: Adds the widget to the HUD via
DynamicHudConfigurator
in theconfigure
method.
Key Points:
TextWidget.Builder: Simplifies widget creation with a fluent API. Set properties like
setX
,setY
,registryKey
, andregistryID
for dynamic values.DynamicValueRegistry: Provides dynamic values for widgets. Use
registerLocal
for mod-specific values orregisterGlobal
withmodid:key
for shared values.Storage: Store widgets as fields to reuse in
configure
.
Always set registryID
for local registry keys. Without it, the widget won’t find its dynamic value, displaying null
or blank text.
Last updated
Was this helpful?