Deproject() is crashing the editor.

Using Deproject function is crashing my editor when testing it.

Not sure why it is happening.

.h file

UCLASS()
class AMyProjectHUD : public AHUD
{
	GENERATED_UCLASS_BODY()

public:

virtual FVector GetFiringLocation();
}

.cpp

FVector AMyProjectHUD::GetFiringLocation()
{
	FVector Origin;
	FVector WorldDir;
	const FVector2D Center((Canvas->ClipX * 0.5f), (Canvas->ClipY * 0.5f));
	Canvas->Deproject(Center, Origin, WorldDir);
}

The function is called in a character class

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

The editor just closes. I tried to remove the deproject part and it works.

Hi . I attempted to duplicate the issue you were experiencing, but was unable to do so with the code snippets that you provided. Could you provide some more information about what is happening? Please look here to get a better idea of what information would be helpful. In particular, can you reproduce this issue in a brand new project made from one of the available templates? If so, please let me know exactly what steps you take when creating the project, what code you add, and how you add it. Make sure to include as much information as possible, since even seemingly minor details can potentially cause drastically different outcomes if left out.

Thanks

#Never Call Canvas Outside of Draw HUD

Dear ,

Canvas->Deproject(Center, Origin, WorldDir);

You are using Canvas outisde of Draw HUD! in a character class!

This is guaranteed crash!

You can’t use canvas->project or canvas->deproject outside of the DrawHUD loop

void YourHUD::DrawHUD()
{
   if(!Canvas) return;
   Canvas->Deproject / Project
}

You might ask, how can I get do stuff outside of drawhud related to project and deproject

I’d recommend you just route stuff to your HUD class and do it in the DrawHUD loop

:slight_smile:

The crash takes place the moment deproject is called. I am trying to replicate it in other ways and see.

I found the reason of crash

const FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f);

Is crashing it.

I am doing it in my HUD class but not inside the DrawHUD function.

I couldn’t use canvas in character as it was protected.

Thanks for the reply I will find something out.

Can Drawcall be called manually from the character?

At what time interval does draw call takes place?

you have to only access Canvas from your DrawHUD loop :slight_smile:

What is your current status?

It was easy finished it and it works

Did you verify that your Canvas object isn’t null? You should exercise safer coding standards to prevent potential crashes. For example to protect yourself from crashing on a bad canvas object you could do the following
checkf(Canvas, “Canvas is invalid! Deproject will not be called.”);
if (Canvas)
{
const FVector2D Center((Canvas->ClipX * 0.5f), (Canvas->ClipY * 0.5f));
Canvas->Deproject(Center, Origin, WorldDir);
}

#Resolved

Congratulations! Yay!