GSM
(Greg)
October 5, 2021, 9:44am
1
Hello,
I manage to add a WidgetComponent to an Actor in a blueprint to display a nameplate.
I’m trying to do the same in full C ++ with this code
TargetWidget = CreateDefaultSubobject<UWidgetComponent>("Widget");
static ConstructorHelpers::FClassFinder<UUserWidget> hudWidgetObj(TEXT("/Game/UI/UI_Nameplate"));
if (hudWidgetObj.Succeeded())
{
TargetWidget->SetWidgetClass(hudWidgetObj.Class);
TargetWidget->SetWidgetSpace(EWidgetSpace::Screen);
TargetWidget->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
TargetWidget->SetDrawSize(FVector2D(500.0f, 700.0f));
TargetWidget->SetVisibility(true);
TargetWidget->AttachToComponent(CollisionComponent, FAttachmentTransformRules::KeepRelativeTransform);
}
When I launch my game, the nameplate is not displayed on my actor.
But if i create a Blueprint deriving from my c ++ class (without modifying my c ++ code and without modifying the blueprint), the actor using this blueprint displays the nameplate.
Why ? How can i fix the problem in C++ ?
May be this will help:
.h
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = MyActorCat, meta = (AllowPrivateAccess = "true"))
UWidgetComponent* MessageComponent;
.cpp
MessageComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("MessageComponent"));
MessageComponent->SetupAttachment(RootComponent);
static ConstructorHelpers::FClassFinder<UUserWidget> Widget(TEXT("WidgetBlueprint'/Game/Widgets/Health/MessageWidget'"));
if (Widget.Succeeded())
{
MessageComponent->SetWidgetClass(Widget.Class);
static ConstructorHelpers::FObjectFinder<UMaterialInstance> MessageMaterial(TEXT("MaterialInstanceConstant'/Engine/EngineMaterials/Widget3DPassThrough_Masked_OneSided.Widget3DPassThrough_Masked_OneSided'"));
if (MessageMaterial.Succeeded())
{
MessageComponent->SetMaterial(0, MessageMaterial.Object);
}
}
There is a MessageWidget.uasset in the folder. Sadly I cannot open my project and view its parent class or setup. My build is broken at the moment.
GSM
(Greg)
October 7, 2021, 3:17pm
3
Thanks.
I modify my code follow your code. It works.
Maybe it was because of AttachToComponent instead of SetupAttachment.
Here i my new code:
// .h
UPROPERTY(VisibleAnywhere, Category = "Components")
class UWidgetComponent* TargetWidgetComponent;
// .cpp
TargetWidgetComponent = CreateDefaultSubobject<UWidgetComponent>("WidgetComponent");
TargetWidgetComponent->SetupAttachment(CollisionComponent);
static ConstructorHelpers::FClassFinder<UUserWidget> userWidget(TEXT("WidgetBlueprint'/Game/UI/UI_Nameplate'"));
if (userWidget.Succeeded())
{
TargetWidgetComponent->SetWidgetClass(userWidget.Class);
static ConstructorHelpers::FObjectFinder<UMaterialInstance> MessageMaterial(TEXT("MaterialInstanceConstant'/Engine/EngineMaterials/Widget3DPassThrough_Masked_OneSided.Widget3DPassThrough_Masked_OneSided'"));
if (MessageMaterial.Succeeded())
{
TargetWidgetComponent->SetMaterial(0, MessageMaterial.Object);
}
TargetWidgetComponent->SetWidgetSpace(EWidgetSpace::Screen);
TargetWidgetComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
TargetWidgetComponent->SetDrawSize(FVector2D(500.0f, 700.0f));
TargetWidgetComponent->SetVisibility(true);
}