How to select an actor in-game using GetHitProxy?

I need to select an actor in-game using a very efficient selection method. I think GetHitProxy is what the editor uses to select actors, I tried to call it this way, but it crashes when calling it:


void AMyProject2Character::OnFire()
{ 

    UWorld* world = GetWorld();
    
    
    if (world->GetGameViewport()->Viewport != NULL) {
        
        int32 HitX = world->GetGameViewport()->Viewport->GetMouseX();
        int32 HitY = world->GetGameViewport()->Viewport->GetMouseY();
        if (HitX > 0 && HitY > 0) {
            HHitProxy* HitProxy = world->GetGameViewport()->Viewport->GetHitProxy(HitX, HitY);
            
            if (HitProxy != NULL && HitProxy->IsA(HActor::StaticGetType()))
            {
                HActor* ActorHit = static_cast<HActor*>(HitProxy);
                if(ActorHit->Actor != NULL)
                {
                    AActor* Actor = ActorHit->Actor;
                    UE_LOG(LogTemp,Warning,TEXT("MyCharacter's Name is %s"), *Actor->GetName() );
                }
            }
        }
        
    }

}

Does anybody knows how to use this? Thank you

This is the stack trace of the crash on Visual Studio:


 	KernelBase.dll!00007ffc2bab77d2()	Unknown
>	UE4Editor-D3D11RHI.dll!FD3D11DynamicRHI::RHISetViewport(unsigned int MinX, unsigned int MinY, float MinZ, unsigned int MaxX, unsigned int MaxY, float MaxZ) Line 220	C++
 	UE4Editor-RHI.dll!SetViewport_Internal(unsigned int MinX, unsigned int MinY, float MinZ, unsigned int MaxX, unsigned int MaxY, float MaxZ) Line 1521	C++
 	UE4Editor-Engine.dll!`FCanvas::Flush_GameThread'::`18'::EURCMacro_CanvasFlushSetupCommand::DoTask(ENamedThreads::Type CurrentThread, const TRefCountPtr<FGraphEvent> & MyCompletionGraphEvent) Line 871	C++
 	UE4Editor-Engine.dll!TGraphTask<`FCanvas::Flush_GameThread'::`18'::EURCMacro_CanvasFlushSetupCommand>::ExecuteTask(TArray<FBaseGraphTask *,FDefaultAllocator> & NewTasks, ENamedThreads::Type CurrentThread) Line 669	C++
 	UE4Editor-Core.dll!FTaskThread::ProcessTasks(int QueueIndex, bool bAllowStall) Line 428	C++
 	UE4Editor-Core.dll!FTaskThread::ProcessTasksUntilQuit(int QueueIndex) Line 271	C++
 	UE4Editor-RenderCore.dll!RenderingThreadMain(FEvent * TaskGraphBoundSyncEvent) Line 282	C++
 	UE4Editor-RenderCore.dll!FRenderingThread::Run() Line 404	C++
 	UE4Editor-Core.dll!FRunnableThreadWin::Run() Line 73	C++
 	UE4Editor-Core.dll!FRunnableThreadWin::GuardedRun() Line 48	C++
 	[External Code]


Anyone from Epic can help me on this? Because I can’t find any code snippet about how to use GetHitProxy() in-game. Thank you.

I’ve built UE4 from source to get access to a more redeable stack trace (I’m testing on the last 4.8 Promoted branch). It’s failing on this assert:



void FOpenGLDynamicRHI::RHIClearMRT(bool bClearColor,int32 NumClearColors,const FLinearColor* ClearColorArray,bool bClearDepth,float Depth,bool bClearStencil,uint32 Stencil, FIntRect ExcludeRect)
{
	VERIFY_GL_SCOPE();

HERE---->	check((GMaxRHIFeatureLevel >= ERHIFeatureLevel::SM5) || !PendingState.bFramebufferSetupInvalid); <------

	if (bClearColor)
	{
		// This is copied from DirectX11 code - apparently there's a silent assumption that there can be no valid render target set at index higher than an invalid one.
		int32 NumActiveRenderTargets = 0;
		for (int32 TargetIndex = 0; TargetIndex < MaxSimultaneousRenderTargets; TargetIndex++)
		{
			if (PendingState.RenderTargets[TargetIndex] != 0)
			{
				NumActiveRenderTargets++;
			}
			else
			{
				break;
			}
		}
		
		// Must specify enough clear colors for all active RTs
		check(NumClearColors >= NumActiveRenderTargets);
	}


This is the stack trace on Xcode (Mac build):

#5 0x0000000100889a10 in FDebug::AssertFailed(char const*, char const*, int, wchar_t const*, …) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Private/Misc/OutputDevice.cpp:360
#6 0x0000000137c7e223 in FOpenGLDynamicRHI::RHIClearMRT(bool, int, FLinearColor const*, bool, float, bool, unsigned int, FIntRect) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/OpenGLDrv/Private/OpenGLCommands.cpp:3194
#7 0x0000000137c7dfa9 in FOpenGLDynamicRHI::RHIClear(bool, FLinearColor const&, bool, float, bool, unsigned int, FIntRect) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/OpenGLDrv/Private/OpenGLCommands.cpp:3087
#8 0x0000000137c7ef26 in non-virtual thunk to FOpenGLDynamicRHI::RHIClear(bool, FLinearColor const&, bool, float, bool, unsigned int, FIntRect) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/OpenGLDrv/Private/OpenGLCommands.cpp:3088
#9 0x0000000104e89b69 in FRHICommandList::Clear(bool, FLinearColor const&, bool, float, bool, unsigned int, FIntRect) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/RHI/Public/RHICommandList.h:1758
#10 0x0000000104ff968c in FViewport::GetRawHitProxyData(FIntRect)::EURCMacro_BeginDrawingCommandHitProxy:: DoTask(ENamedThreads::Type, TRefCountPtr<FGraphEvent> const&) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Engine/Private/UnrealClient.cpp:1083
#11 0x00000001051d2837 in TGraphTask<FViewport::GetRawHitProxyData(FIntRect)::EURCMacro_BeginDrawingCommandHitProxy>::ExecuteTask(TArray<FBaseGraphTask*, FDefaultAllocator>&, ENamedThreads::Type) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Public/Async/TaskGraphInterfaces.h:733
#12 0x000000010040ae80 in FBaseGraphTask::Execute(TArray<FBaseGraphTask*, FDefaultAllocator>&, ENamedThreads::Type) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Public/Async/TaskGraphInterfaces.h:308
#13 0x00000001004010b9 in FTaskThread::ProcessTasks(int, bool) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Private/Async/TaskGraph.cpp:428
#14 0x00000001003fabe9 in FTaskThread::ProcessTasksUntilQuit(int) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Private/Async/TaskGraph.cpp:271
#15 0x00000001003eba7d in FTaskGraphImplementation::ProcessThreadUntilRequestReturn(ENamedThreads::Type) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Private/Async/TaskGraph.cpp:946
#16 0x000000010c2cdc4f in RenderingThreadMain(FEvent*) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/RenderCore/Private/RenderingThread.cpp:278
#17 0x000000010c3b134d in FRenderingThread::Run() at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/RenderCore/Private/RenderingThread.cpp:399
#18 0x00000001004164ea in FRunnableThreadPThread::Run() at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Private/HAL/PThreadRunnableThread.cpp:25
#19 0x00000001003d6c99 in FRunnableThreadPThread::_ThreadProc(void*) at /Users/…/UnrealEnginePromoted/Engine/Source/Runtime/Core/Private/GenericPlatform/…/HAL/PThreadRunnableThread.h:167

You need inherits from UGameViewportClient class.
Then


virtual bool RequiresHitProxyStorage() override { return true; }

Did you make it work?

Do you fix it?

I’m on 4.25.1 and I get a crash when I try to call GetHitProxy at runtime slightly different error, fails on Check(ColorRT) in a method inside RHIResources.h.

you should overwrite this function


virtual bool RequiresHitProxyStorage() override { return true; }

Ok this fixed the crash but HitProxy never returns anything.

you can follow this https://vhope.cf/zh/posts/ue/ue-hitproxy/

hi brother, the link can not open already , can u show the method ?

hi man, can u show me how to resolve this problem 3Q