# Is this an optimal way to access canvas function froms Chracter class

**URL:** <https://forums.unrealengine.com/t/is-this-an-optimal-way-to-access-canvas-function-froms-chracter-class/3957>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [May 7, 2014, 8:34pm UTC](https://forums.unrealengine.com/t/is-this-an-optimal-way-to-access-canvas-function-froms-chracter-class/3957 "2014-05-07T20:34:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [May 7, 2014, 8:34pm UTC](https://forums.unrealengine.com/t/is-this-an-optimal-way-to-access-canvas-function-froms-chracter-class/3957/1 "2014-05-07T20:34:51Z")

</div>

Hi…

As you know, it is not possible to access Canvas from any part from the HUD other than the DrawHUD function.

So I am planning to use this way. But I doubt it is the most optimal way to do it.

This is my character class

```auto

		APlayerController* MyPC = Cast<APlayerController>(Controller);
		AMyProjectHUD* MyHUD;
		FVector EndLocation;
		MyHUD = nullptr;
		if (MyPC)
		{
			MyHUD = Cast<AMyProjectHUD>(MyPC->GetHUD());
		}
		if (MyHUD)
		{
			MyHUD->bIsFiringCast = true;
		        MyHUD->DrawHUD();
		}

```

This my HUD class

```auto

void AMyProjectHUD::DrawHUD()
{
	if(bIsFiringCast == false)
	{
		Super::DrawHUD();

		// Draw very simple crosshair

		// find center of the Canvas
		const FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f);

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

		// draw the crosshair
		FCanvasTileItem TileItem(CrosshairDrawPosition, CrosshairTex->Resource, FLinearColor::White);
		TileItem.BlendMode = SE_BLEND_Translucent;
		Canvas->DrawItem(TileItem);
	}
	else
	{
		//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red,TEXT("WORKS"));
		//bIsFiringCast = false;
	}
}

```

But using this kind of lags my game. Although I am not 100% sure if it actually lags the game.

---

<div class="post-metadata">

**Author:** ![Agentleo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Agentleo](https://forums.unrealengine.com/u/Agentleo)\
**Post date:** [May 7, 2014, 8:49pm UTC](https://forums.unrealengine.com/t/is-this-an-optimal-way-to-access-canvas-function-froms-chracter-class/3957/2 "2014-05-07T20:49:56Z")

</div>

I think we would be able to help you more if you tells us what you are hoping to accomplish with your code. If you are just trying to display your hud you should be doing this from a GameMode class.

---

<div class="post-metadata">

**Author:** ![envenger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@envenger](https://forums.unrealengine.com/u/envenger)\
**Post date:** [May 7, 2014, 8:58pm UTC](https://forums.unrealengine.com/t/is-this-an-optimal-way-to-access-canvas-function-froms-chracter-class/3957/3 "2014-05-07T20:58:13Z")

</div>

> [@Agentleo;28609](#):
>
> I think we would be able to help you more if you tells us what you are hoping to accomplish with your code. If you are just trying to display your hud you should be doing this from a GameMode class.

I want to use a deproject function when the the character fires a bullet.

---

<div class="post-metadata">

**Author:** ![Agentleo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Agentleo](https://forums.unrealengine.com/u/Agentleo)\
**Post date:** [May 7, 2014, 10:34pm UTC](https://forums.unrealengine.com/t/is-this-an-optimal-way-to-access-canvas-function-froms-chracter-class/3957/4 "2014-05-07T22:34:34Z")

</div>

Sounds like what you need are trace functions. has an example Wiki article here that should be able to help.

> **[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 — https://unrealcommunity.wiki/! You will be able to find content from...

Beyond that Canvas has it own native Deproject function as well.

---

<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:** [May 8, 2014, 12:21am UTC](https://forums.unrealengine.com/t/is-this-an-optimal-way-to-access-canvas-function-froms-chracter-class/3957/5 "2014-05-08T00:21:20Z")

</div>

you are calling DrawHUD from your player controller class!!!

you dont need to do that 🙂

DrawHUD() will get called automatically, so just set your boolean to true in the Character class

also, you are not setting the boolean back to false ever, at least not in the code you show.

also, you should not conditionally call super on draw hud, you should always call it, and only do your additional custom logic inside your firecasting if statement.

summary

for starters, just dont call DrawHUD from your player controller class and see how that goes.

🙂
