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


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

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

This my HUD class

void AMyProjectHUD::DrawHUD()
	if(bIsFiringCast == false)

		// 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;
		//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.

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.

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

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

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

you dont need to do that :slight_smile:

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.


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