FindWidget Crashes Unreal Engine

I have a custom UserWidget called GameWidget

code is:

//GameWidget.h
#pragma once

#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
#include "Blueprint/UserWidget.h"
#include "GameWidget.generated.h"

/**
 * 
 */
UCLASS()
class SPACESHOOTER2_API UGameWidget : public UUserWidget
{
	GENERATED_BODY()
	
public:
	void Load();

	void SetScore(int score);
	void OnGameOver(int score);

	UPROPERTY()
	UTextBlock* ScoreText;
	
	
};

//GameWIdget.cpp
#include "SpaceShooter2.h"
#include "GameWidget.h"

void UGameWidget::Load()
{
	const FName TextBlockName = FName(TEXT("GameTextBlock"));

	if (ScoreText == nullptr) {
		//ScoreText = (UTextBlock*)(WidgetTree->FindWidget(TextBlockName));
		WidgetTree->FindWidget(TextBlockName);
	}
}

void UGameWidget::SetScore(int score)
{
	if (ScoreText != nullptr) {
		ScoreText->SetText(FText::FromString(FString(TEXT("Score: ")) + FString::FromInt(score)));
	}
}

void UGameWidget::OnGameOver(int score)
{
	if (ScoreText != nullptr)
	{
		ScoreText->SetText(FText::FromString(FString(TEXT("Press R To Restart\nScore: ")) + FString::FromInt(score) ));
	}
}

Here is what the blueprint made from the class GameWidget looks like:


Calling ‘Load’ causes the crash.
I narrowed it down specifically to calling ‘FindWidget’

Yeah, I also tried GetAllWidgets and THAT causes a crash as well. This is seriously frustrating, I’m just trying to get through a tutorial.

Hey Gochida-

Looking at the code provided, the likely issue is that WidgetTree is null when you reference it which causes the crash. Can you try adding if(WidgetTree) prior to the line where you call FindWidget(). If you are still getting a crash, please provide the callstack and log files from the crash for more information.

Cheers

Still crashes the editor when I run it, even with the if(WidgetTree)
link text

Where are you calling your Load function? Additionally, let me know if making your Load a UFUNCTION() has any affect on the behavior. When I mark the Load function (and other functions) as UFUNCTION() I’m able to call Load without any crash. Let me know if this helps or if you can provide steps to help me reproduce the issue locally.

I’m calling Load from a GameMode object in BeginPlay like so:

void ASpaceShooterGameMode::BeginPlay() {
	Super::BeginPlay();

	ChangeMenuWidget(StartingWidgetClass);

	((UGameWidget*)CurrentWidget)->Load();
}

ChangeMenuWidget sets CurrentWidget to the widget set in a edit anywhere UPROPERTY on the game mode. The GameMode used is a blueprint inheriting from this custom GameMode class.

Hey Gochida-

I am still unable to reproduce the crash on my end and the provided log files do not indicate a crash occurring. If you’re able to reproduce the crash, could you provide a sample project that illustrates the issue. Additionally, you mentioned working through a tutorial, can you provide the tutorial link and where in the tutorial this occurred?

The tutorial is from a site called Zenva, specifically Zenva Academy which sells programming courses. It was on sale for $9 so I figured I’d give it a shot. Unfortunately I don’t know of a way to link the course short of you purchasing it.

I can link my project via google drive, hope that works for you.

The supplied project is missing the .uproject file and cannot be opened. Can you upload the project again?

Try this one maybe?

Hey Gochida-

Thank you for providing the project again. This time I was able to open the project and investigate. I found that the issue was in fact a null pointer, the CurrentWidget variable was not set. If you make the pointer public and add EditAnywhere, Instanced, BlueprintReadWrite, Category = Test inside the UPROPERTY declaration, this will allow you to set any widget in your content browser to the pointer. After you’ve set the pointer in your game mode blueprint, you should be able to play without the crash occurring.

Cheers

I agree that this does work and should be the way to do it, though I think the original intent of the code was to have a Starting Widget that is first set to Current Widget, which is kind of redundant unless for some reason you needed to return to the starting widget.

That aside, it would be nice if the editor gave me an error report that a variable was null, rather than close out everything, shut the editor down and give me a “send crash report” window - that window is the whole reason I created a bug report in the first place!

Also makes trying to fix the null variable a bit of a pain. In the case of a even sneakier cause I can’t imagine how painful it would be to try and track it down and fix it. You aren’t even told what was null in the first place so it’s guesswork to narrow it down!