FSceneView::DeprojectFVector2D is not const, so cannot use it from FEdMode DrawHUD

Dear Friends at Epic,

I discovered this while making my Vertex Snap Editor Plugin which is now available here for download:
http://forums.epicgames.com/threads/980157-UE4-Editor-Mode-Plugin-Features-Vertex-Snapping-amp-Instant-Translate-to-Cursor


I cant use FSceneView::DeprojectFVector2D SceneManagement.h

because it is not const

/** transforms 2D screen coordinates into a 3D world-space origin and direction 
	 * @param ScreenPos - screen coordinates in pixels
	 * @param out_WorldOrigin (out) - world-space origin vector
	 * @param out_WorldDirection (out) - world-space direction vector
	 */
	void DeprojectFVector2D(const FVector2D& ScreenPos, FVector& out_WorldOrigin, FVector& out_WorldDirection);

Attempting to use from DrawHUD in FEdMode

void FVictoryEdAlignMode::DrawHUD(FLevelEditorViewportClient* ViewportClient,FViewport* Viewport,const FSceneView* View,FCanvas* Canvas)

since View is a const ptr I cannot use the function!

I was thusly compelled to use this instead, which works cause all the static parameters are const

void FVictoryEdAlignMode::VictoryDeProject(const FSceneView* View,const FVector2D& ScreenPoint, FVector& out_WorldOrigin, FVector& out_WorldDirection)
{
	if(!View) return;
	//~~~~~~~~~
	
	//NEEDS TO BE MADE CONST TO BE ABLE TO USE THIS
	/** transforms 2D screen coordinates into a 3D world-space origin and direction 
	 * @param ScreenPos - screen coordinates in pixels
	 * @param out_WorldOrigin (out) - world-space origin vector
	 * @param out_WorldDirection (out) - world-space direction vector
	 */
	//View->DeprojectFVector2D(ScreenPoint, out_WorldOrigin, out_WorldDirection);

	
	/** transforms 2D screen coordinates into a 3D world-space origin and direction 
	 * @param ScreenPos - screen coordinates in pixels
	 * @param ViewRect - view rectangle
	 * @param InvViewMatrix - inverse view matrix
	 * @param InvProjMatrix - inverse projection matrix
	 * @param out_WorldOrigin (out) - world-space origin vector
	 * @param out_WorldDirection (out) - world-space direction vector
	 */
	FSceneView::DeprojectScreenToWorld(
		ScreenPoint, 
		View->ViewRect, 
		View->InvViewMatrix, 
		View->ViewMatrices.GetInvProjMatrix(), 
		out_WorldOrigin, 
		out_WorldDirection
	);

}

static void DeprojectScreenToWorld(
	const FVector2D & ScreenPos, 
	const FIntRect & ViewRect, 
	const FMatrix & InvViewMatrix, 
	const FMatrix & InvProjMatrix, 
	FVector & out_WorldOrigin, 
	FVector & out_WorldDirection
);

#Thanks!

Thanks for UE4!

#:heart:

Rama

Hi Rama,

Yep, that function should indeed be const. I’ve made this change and submitted it.

In the future, a workaround I’d suggest for situations like this would be to use const_cast<> to get a non-const pointer as needed.

Cheers!
Jeff

Ahh thanks for the very helpful tip about const_cast Jeff!

:slight_smile:

Rama