WidgetData
The WidgetData class is a Java record that encapsulates the data needed to create a widget. It uses Java’s Supplier functional interface to provide a factory method for creating widgets.
This class is mostly used for loading widgets. This provides an automatic way of producing widget (also known as widgetFactory).
Key Components
name: A
Stringthat represents the name of the widget.description: A
Stringthat provides a description of the widget.widgetFactory: A
Supplier<T>whereTextendsWidget. This is a factory method for creating instances of the widget.
Methods
name(): This method returns the name of the widget.
description(): This method returns the description of the widget.
createWidget(): This method uses the
widgetFactoryto create a new instance of the widget.
Usage
Here’s an example of how you might use the WidgetData class:
WidgetData<MyWidget> myWidgetData = new WidgetData<>(
"MyWidget", // Name of the widget
"This is my custom widget.", // Description of the widget
() -> new MyWidget() // Factory method for creating the widget
);
// Create a new instance of MyWidget (Used in WidgetManager to load from a save file)
Widget myWidget = myWidgetData.createWidget();Create an empty constructor in your Widget class providing garbage values to your instance variables, as they will be overwritten by saved data before any of the Widgets interact with the game, thus ensuring safety.
Last updated
Was this helpful?