# Create Basic C++ clickable HUD button

**URL:** <https://forums.unrealengine.com/t/create-basic-c-clickable-hud-button/16781>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [December 9, 2014, 4:51am UTC](https://forums.unrealengine.com/t/create-basic-c-clickable-hud-button/16781 "2014-12-09T04:51:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_08d7a710](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_08d7a710](https://forums.unrealengine.com/u/anonymous_user_08d7a710)\
**Post date:** [December 9, 2014, 4:51am UTC](https://forums.unrealengine.com/t/create-basic-c-clickable-hud-button/16781/1 "2014-12-09T04:51:21Z")

</div>

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);

```

}

---

<div class="post-metadata">

**Author:** ![Sarr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sarr](https://forums.unrealengine.com/u/Sarr)\
**Post date:** [December 9, 2014, 10:04am UTC](https://forums.unrealengine.com/t/create-basic-c-clickable-hud-button/16781/2 "2014-12-09T10:04:59Z")

</div>

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  
}  
}

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [December 10, 2014, 12:34am UTC](https://forums.unrealengine.com/t/create-basic-c-clickable-hud-button/16781/3 "2014-12-10T00:34:47Z")

</div>

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](https://wiki.unrealengine.com/HUD,_Canvas,_Code_Sample_of_800%2B_Lines),_Create\_Buttons_%26\_Draw\_Materials

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

Rama

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_08d7a710](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_08d7a710](https://forums.unrealengine.com/u/anonymous_user_08d7a710)\
**Post date:** [December 10, 2014, 12:55am UTC](https://forums.unrealengine.com/t/create-basic-c-clickable-hud-button/16781/4 "2014-12-10T00:55:37Z")

</div>

Thanks guys I will try that

---

<div class="post-metadata">

**Author:** ![Wkavanagh](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/wkavanagh/32/88710_2.png) [@Wkavanagh](https://forums.unrealengine.com/u/Wkavanagh)\
**Post date:** [April 26, 2021, 4:19pm UTC](https://forums.unrealengine.com/t/create-basic-c-clickable-hud-button/16781/5 "2021-04-26T16:19:07Z")

</div>

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