[C++] Null "Canvas" in the BeginPlay()

I’m trying to create a small UI and I’m trying to extract the Canvas->SizeX information from the Canvas member present in the base HUD class that my own class derives from.

In my test project, there’s absolutely no problem. However, in the real project, I keep getting a null pointer for the “Canvas” member.

Any idea what’s going on?

//…
#include “Engine/Canvas.h”
//…

AHumanHUD::AHumanHUD(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
//…;
}

void AHumanHUD::DrawHUD()
{
Super::DrawHUD();
}

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

float test;
if (Canvas) <== never other than 0.
{
	test = Canvas->SizeX;
}

}

Canvas will only be reliably valid inside of DrawHUD, so you must access its variables there :slight_smile:

So you should not be using Canvas in BeginPlay()

:slight_smile:

Rama

1 Like

Thanks! It does work in some weird cases, but it seems quite unpredictable. Probably a side effect or something.

Thank you.

1 Like