What I am trying to accomplish is a widget above an item I am trying to interact with. I want to do this in C++.
I have been searching and trying different things based on my search. Found and tried the following:
- C++ implementation for the HealthBar - Show - GameDev.tv
- How to create WidgetComponent with a UUserWidget reference in C++ code?
- And others.
What I currently have is a InteractionWidget class
.h
UCLASS()
class LOCALMULTIPLAYER_API UInteractionWidget : public UUserWidget
{
GENERATED_BODY()
// virtual void NativeConstruct() override;
public:
FORCEINLINE UBorder* GetInteractionBorder() const { return InteractionBorder; }
FORCEINLINE UTextBlock* GetInteractionText() const { return InteractionText; }
private:
UPROPERTY(meta = (BindWidget))
UBorder* InteractionBorder;
UPROPERTY(meta = (BindWidget))
UTextBlock* InteractionText;
};
nothing in the cpp
On the item to be interacted with
.h
// Interaction Widget
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Interactable")
TSubclassOf<UInteractionWidget> InteractionWidgetClass;
UPROPERTY(VisibleAnywhere, Category = "Interactable")
UWidgetComponent* InteractionWidget;
.cpp
In Constructor
// Setup the widget component
InteractionWidget = CreateDefaultSubobject<UWidgetComponent>(FName("Interaction Widget"));
if (InteractionWidget)
{
InteractionWidget->SetupAttachment(RootComponent);
InteractionWidget->SetWidgetClass(InteractionWidgetClass);
InteractionWidget->SetRelativeLocation(FVector(0.f, 0.f, 100.f));
InteractionWidget->SetWidgetSpace(EWidgetSpace::Screen);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No Interaction Widget Found"));
}
In beginplay
if (InteractionWidget)
{
// Do widget setup here
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Interaction Widget Not Found"));
}
I never get it saying “No Interaction Widget Found”. So the creation seems ok. But I do get “Interaction Widget Not Found”.
Setting breakpoints in the constructor, InteractionWidgetClass is null even though I set it in the Blueprint implementation of the item.
I am not even sure this is the best way to go about it. I am trying to do as much in C++ as I can for this project.
How can I fix this, or is there a better way I can get a widget to show on an interactable item?