Based on the contents below, I created a blueprint class and am working on moving it to c++ class.
C++ class’s logic is same with blutprint class.
I design widget blueprint class and reparent to c++ class.
here is c++ code
CompassBar.h
#pragma once
#include “CoreMinimal.h”
#include “Blueprint/UserWidget.h”
#include “CompassBar.generated.h”
class UMaterialInstanceDynamic;
UCLASS()
class STRUGGLELIFE_API UCompassBar : public UUserWidget
{
GENERATED_BODY()
protected:
virtual void NativeConstruct() override;
virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;
UPROPERTY()
UMaterialInstanceDynamic* CompassMaterial;
};
CompassBar.cpp
#include “CompassBar.h”
#include “Components/Image.h”
#include “Kismet/GameplayStatics.h”
void UCompassBar::NativeConstruct()
{
Super::NativeConstruct();
UImage* CompassBar = Cast<UImage>(GetWidgetFromName("DSN_CompassBar"));
if(CompassBar)
{
CompassMaterial = CompassBar->GetDynamicMaterial();
}
}
void UCompassBar::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
Super::NativeTick(MyGeometry, InDeltaTime);
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
const FRotator ControllerRotation = PlayerController->GetControlRotation();
const float CompassRatio = ControllerRotation.Yaw / 360.f;
CompassMaterial->SetScalarParameterValue("YawRatio", CompassRatio);
}
It works, but if i reopen editor widget blueprint class’s parent is set to none
I don’t know why, and would you please give me some advice?