Hi, I’m newbie here and I had some research before posting this topic. So I am not just asking like pasting codes.
So
I got ThirdPersonMPCharacter.h/cpp which following the multiplayer tutorial in UE4 Document.
And I create a userwidget by myself
UCLASS()
class THIRDPERSONMP_API UMyUserWidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, Category=CPP)
float HP;
void setHP(float currentHP);
and function setHP is like below :
void UMyUserWidget::setHP(float currentHP)
{
HP = currentHP;
}
So What I want to do is…
When my character from ThirdPersonMPCharacter.cpp change variable CurrentHealth. Then I am trying to call void setHP from UMyUserWidget and set value of HP inside UMyUserWidget. Where I can handle value HP sync with ThirdPersonCharacter’s CurrentHealth. And finally using HP variable at UE4 Editor’s BP to showing(Add viewport) changing HP value.
void AThirdPersonMPCharacter::SetCurrentHealth(float healthValue)
{
UE_LOG(LogTemp, Log, TEXT("SET_CURRENT_HEALTH , PARAM = %f"), healthValue);
if (GetLocalRole() == ROLE_Authority)
{
UE_LOG(LogTemp, Log, TEXT("CURRENT_HEALTH IS CHANGING"));
CurrentHealth = FMath::Clamp(healthValue, 0.f, MaxHealth);
// Here is what I want... But really don't know how and this code is error.
UMyUserWidget* MyUserWidget = Cast<UMyUserWidget>(GetClass());
MyUserWidget->setHP(CurrentHealth);
}
}
That unhandled exception means that you tried to do something with a NULL object. That basically never happens when going from Blueprint, because Blueprint verifies object references before calling functions, so this is likely a bug somewhere in your C++ code.
Run the code in the debugger and see where the exception happens, that should show you on the stack why the object pointer is null.
I tried not running HP = currentHP; in setHP function and just UE_LOG parameter currentHP. It works fine. But when I trying to assign value to HP I got Unhandled Exception
Note that it’s perfectly possible to run a method inside a class when the object pointer is NULL, as long as you don’t reference any members (explicitly or implicitly.)
I have checked it return nullptr. But I don’t know why my function is running.
So additional question is How to cast UserWidget ? don’t know how to get Object of my custom UUserWidget Class.
casting would be needed if lets say you created your widgets in a new AHUD class then you would need to get the HUD from player Controller and cast it to yourHUD class. Easy way to remember is casting filters your datatype and returns if the datatype is Of type or not.
my personal preference is i do all the Widget related stuff in Blueprints and if i need to update anything i simply do a Multicast Delegate broadcast()