Download

Undefined type UCanvas

When I try to dry my HUD, I’m getting an undefined type UCanvas. Here is my code:

void AFPSHUD::DrawHUD()
{
Super::DrawHUD();

if (CrosshairTexture)
{
    // Find the center of our canvas.
    FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f);

    // Offset by half of the texture's dimensions so that the center of the texture aligns with the center of the Canvas.
    FVector2D CrossHairDrawPosition(Center.X - (CrosshairTexture->GetSurfaceWidth() * 0.5f), Center.Y - (CrosshairTexture->GetSurfaceHeight() * 0.5f));

    // Draw the crosshair at the centerpoint.
    FCanvasTileItem TileItem(CrossHairDrawPosition, CrosshairTexture->Resource, FLinearColor::White);
    TileItem.BlendMode = SE_BLEND_Translucent;
    Canvas->DrawItem(TileItem);
}

}

Canvas is defined in HUD.h:

class ENGINE_API AHUD : public AActor
{

/** Canvas to Draw HUD on. Only valid during PostRender() event. /
UPROPERTY()
UCanvas
Canvas;

}

It doesn’t know what a UCanvas is, all it knows is that you have a member variable of that type. You need to include ‘Canvas.h’ in your CPP file, and you probably need to put:



class UCanvas;


In the header for a forward-declaration. Somewhere above UCLASS() will do.

I found it. I needed to add the following include files to my FPSHUD.cpp file:

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