Unreal Engine crashes on MyWidgetInstance->Update();
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000000
I guess that I am not creating an instance of the widget properly.
// Fill out your copyright notice in the Description page of Project Settings.
#include “ShootVolume.h”
#include <Components/WidgetComponent.h>
#include “Components/TextBlock.h”
// Sets default values
AShootVolume::AShootVolume()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = false;
DefaultSceneRoot = CreateDefaultSubobject(FName(“DefaultSceneRoot”));
SetRootComponent(DefaultSceneRoot);
StaticMesh = CreateDefaultSubobject(FName(“StaticMesh”));
StaticMesh->SetupAttachment(DefaultSceneRoot);
Box = CreateDefaultSubobject(FName(“Box”));
Box->SetBoxExtent(FVector(74.0f, 125.0f, 100.0f));
Box->SetupAttachment(StaticMesh);
Box->SetRelativeLocation(FVector(0.0f, 0.0f, 100.0f));
}
void AShootVolume::BeginPlay()
{
Super::BeginPlay();
Box->OnComponentBeginOverlap.AddDynamic(this, &AShootVolume::OverlapBegin);
Box->OnComponentEndOverlap.AddDynamic(this, &AShootVolume::OverlapEnd);
Box->OnComponentHit.AddDynamic(this, &AShootVolume::HitMesh);
MyWidgetInstance = CreateWidget<UMyUserWidget>(GetWorld(), WidgetClass); //MyWidgetInstance->AddToViewport();
}
void AShootVolume::OverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Warning, TEXT("Some warning message"));
}
void AShootVolume::OverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
UE_LOG(LogTemp, Warning, TEXT(“Some warning message 2”));
OtherActor->Destroy();
MyWidgetInstance->Update();
}
void AShootVolume::HitMesh(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
FVector NormalImpulse, const FHitResult& Hit)
{
UE_LOG(LogTemp, Warning, TEXT(“bullet”));
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “MyUserWidget.h”
#include “Components/BoxComponent.h”
#include “CoreMinimal.h”
#include “GameFramework/Actor.h”
#include “ShootVolume.generated.h”
UCLASS()
class MYPROJECT_API AShootVolume : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor’s properties
AShootVolume();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditInstanceOnly, Category = "Changeables")
UStaticMeshComponent* StaticMesh;
UPROPERTY()
USceneComponent* DefaultSceneRoot;
UPROPERTY()
UBoxComponent* Box;
UPROPERTY(EditAnywhere, Category = "Widgets")
TSubclassOf<UMyUserWidget> WidgetClass;
UPROPERTY()
UMyUserWidget* MyWidgetInstance;
UFUNCTION()
void OverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
UFUNCTION()
void HitMesh(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};
// Fill out your copyright notice in the Description page of Project Settings.
#include “Components/TextBlock.h”
#include “MyUserWidget.h”
void UMyUserWidget::NativeConstruct()
{
Super::NativeConstruct();
// ItemTitle can be nullptr if we haven't created it in the
// Blueprint subclass
if (ItemTitle)
{
ItemTitle->SetText(FText::FromString(TEXT("Hello world!")));
}
}
void UMyUserWidget::Update()
{
// ItemTitle can be nullptr if we haven't created it in the
// Blueprint subclass
ItemTitle->SetText(FText::FromString(TEXT("Hello!")));
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “Blueprint/UserWidget.h”
#include “MyUserWidget.generated.h”
/**
*
*/
UCLASS()
class MYPROJECT_API UMyUserWidget : public UUserWidget
{
GENERATED_BODY()
public:
virtual void Update();
protected:
virtual void NativeConstruct() override;
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
class UTextBlock* ItemTitle;
};