Download

Create Basic C++ clickable HUD button

Hey Guys, I’ve been trying to make a clickable button with a texture on it in C++. So far all I have is the ability to make a basic texture in hud. How would I make a clickable
Here’s what I have so far on my HUD class

THE .h FILE/////////////////////////////

#pragma once

#include “GameFramework/HUD.h”
#include “MyHUD.generated.h”

/**
*
*/
UCLASS()
class SIGNTUTORIAL_API AMyHUD : public AHUD
{
GENERATED_UCLASS_BODY()

virtual void DrawHUD();

private:
UTexture2D* CrosshairTexture;
void DrawCrosshair();

};

THE CPP FILE/////////////////////////////////////////

#include “SignTutorial.h”
#include “MyHUD.h”

AMyHUD::AMyHUD(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
ConstructorHelpers::FObjectFinder<UTexture2D> CrosshairTextureObject(TEXT(“Texture2D’/Game/crosshair.crosshair’”));
if (CrosshairTextureObject.Object)
{
CrosshairTexture = CrosshairTextureObject.Object;
}
}

void AMyHUD::DrawHUD()
{
Super::DrawHUD();
if (CrosshairTexture)
{
DrawCrosshair();
}
}

void AMyHUD::DrawCrosshair()
{
float TextureWidth = CrosshairTexture->GetSurfaceWidth();
float TextureHeight = CrosshairTexture->GetSurfaceHeight();

FVector2D const CanvasCenter(Canvas-&gt;ClipX * 0.5f, Canvas-&gt;ClipY * 0.5f);
FVector2D const DrawPosition(CanvasCenter.X - TextureWidth * 0.5f, CanvasCenter.Y - TextureHeight * 0.5f);

FCanvasTileItem TileItem(DrawPosition, CrosshairTexture-&gt;Resource, FLinearColor::White);
TileItem.BlendMode = ESimpleElementBlendMode::SE_BLEND_Translucent;

Canvas-&gt;DrawItem(TileItem);

}

the way I did that was to add a hitbox in the location where I need mouseclick to be detected (command is literally AddHitBox(parameters)) and override and implement
“void ReceiveHitBoxClick(const FName Hitbox) override;”
in the *.h file, then in the cpp file you can just do :
AMyHUD::ReceiveHitBoxClick(FName Hitbox)
{
if (Hitbox == whatever the name of your hitbox is)
{
do something when people click the hitbox
}
}

Several highly relevant UE4 wiki tutorials, including my own tutorial where I show how to do click detection yourself

A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums,Create_Buttons%26_Draw_Materials

Rama

Thanks guys I will try that

NotifyHitBoxClick() not Receive… which will only work in BPs. Otherwise this has solved a similar issue for me.