Download

Thought I could use Factory Method for my widgets... but...

In my 2D Final Fantasy ish project, there are many widget classes which are derived from a parent class (I called it Base Widget class)

I thought “Hmm… so many different widgets, but sharing one parent… I think I need to use Factory Method”

and here I come… cannot remember which one I used, but it seems fine for a moment.

borrowing from https://forums.unrealengine.com/show...legate-example

I thought I could do CreateWidget inside the parent widget class, if I get parameter of current controller and class type.

And… I got stuck in pointer and structure…

Is there anyway that works similar like Factory Method? like… one object (factory?) create various classes based on a parent class?

here is code

the parent class



UCLASS()
class NOCOUNTRYFORTHEM_API UBase_Widget : public UUserWidget
{
template <typename theType>
	FORCEINLINE void CreateWidgetItself(class APlayerController* currentController, theType dataType)
	{
		TAssetSubclassOf<theType> theClass;
		dataType = CreateWidget<theType>(currentController, theClass.LoadSynchronous()); // Create Widget
		if (this == NULL)
			return;
		this->AddToViewport();// Add it to the viewport so the Construct() method in the UUserWidget:: is run.
		this->SetVisibility(ESlateVisibility::Hidden);// Set it to hidden so its not open on spawn
	}
}


Base Factory class



UCLASS()
class NOCOUNTRYFORTHEM_API ABase_WidgetHUD : public AHUD
{
	GENERATED_BODY()
public:
	virtual void CreateCustomWidget(ENonBattleWidgetTypesEnum theWidgetType, class APlayerController* currentController) PURE_VIRTUAL(UDefault_Widget::CreateCustomWidget, ;);
	
	
	
};


Child Factory.h



UCLASS()
class NOCOUNTRYFORTHEM_API ANonBattleWidgetsHUD : public ABase_WidgetHUD
{
	GENERATED_BODY()
public:
	void CreateCustomWidget(ENonBattleWidgetTypesEnum theWidgetType, class APlayerController* currentController);
	
	
};




Child Factory.cpp



void ANonBattleWidgetsHUD::CreateCustomWidget(ENonBattleWidgetTypesEnum theWidgetType, class APlayerController* currentController)
{
	switch (theWidgetType)
	{
	case ENonBattleWidgetTypesEnum::NBWT_NONBATTLEMENU:
		UNonBattleMenu_Widget* theWidget;
		theWidget->CreateWidgetItself(currentController, theWidget);
		break;
	case ENonBattleWidgetTypesEnum::NBWT_CHARACTERINFO:
		break;
	case ENonBattleWidgetTypesEnum::NBWT_ITEM:
		break;
	case ENonBattleWidgetTypesEnum::NBWT_CRAFT:
		break;
	case ENonBattleWidgetTypesEnum::NBWT_OPTIONEXIT:
		break;
	}
}



i know my code is a mess… but I am clueless too.