GetHitResultUnderCursorForObjects Array

Hi,
I am trying to get the object that is under my mouse cursor and I am trying to use GetHitResultUnderCursorForObject

in the docs it says bool GetHitResultUnderCursorForObjects ( const TArray< TEnumAsByte< EObjectTypeQuery > > & ObjectTypes, bool bTraceComplex, FHitResult & HitResult )

but I cannot seem to understand how to build the array. Can someone provide an example?

Thanks in advance

Dear Gogata256,

You have to specify which Object types you are looking for when you call the function:

Signature:

bool GetHitResultUnderCursorForObjects ( const TArray< TEnumAsByte< EObjectTypeQuery > > & ObjectTypes, bool bTraceComplex, FHitResult & HitResult )

Implementation

//Init the Array
TArray< TEnumAsByte< EObjectTypeQuery > > ObjectTypes;
ObjectTypes.Add(EObjectTypeQuery::ObjectTypeQuery1);
ObjectTypes.Add(EObjectTypeQuery::ObjectTypeQuery2);
//... any other types you want

FHitResult Hit;
bool bHitOccurred = GetHitResultUnderCursorForObjects(ObjectTypes,true,Hit);
if(bHitOccurred)
{
    //utilize Hit.GetActor() to know who was hit 
}

The enum is not particularly C++ friendly, here’s the whole enum, probably better to use this particular function in BP or write your own version in C++.

EngineTypes.h

UENUM(BlueprintType)
enum EObjectTypeQuery
{
	ObjectTypeQuery1 UMETA(Hidden), 
	ObjectTypeQuery2 UMETA(Hidden), 
	ObjectTypeQuery3 UMETA(Hidden), 
	ObjectTypeQuery4 UMETA(Hidden), 
	ObjectTypeQuery5 UMETA(Hidden), 
	ObjectTypeQuery6 UMETA(Hidden), 
	ObjectTypeQuery7 UMETA(Hidden), 
	ObjectTypeQuery8 UMETA(Hidden), 
	ObjectTypeQuery9 UMETA(Hidden), 
	ObjectTypeQuery10 UMETA(Hidden), 
	ObjectTypeQuery11 UMETA(Hidden), 
	ObjectTypeQuery12 UMETA(Hidden), 
	ObjectTypeQuery13 UMETA(Hidden), 
	ObjectTypeQuery14 UMETA(Hidden), 
	ObjectTypeQuery15 UMETA(Hidden), 
	ObjectTypeQuery16 UMETA(Hidden), 
	ObjectTypeQuery17 UMETA(Hidden), 
	ObjectTypeQuery18 UMETA(Hidden), 
	ObjectTypeQuery19 UMETA(Hidden), 
	ObjectTypeQuery20 UMETA(Hidden), 
	ObjectTypeQuery21 UMETA(Hidden), 
	ObjectTypeQuery22 UMETA(Hidden), 
	ObjectTypeQuery23 UMETA(Hidden), 
	ObjectTypeQuery24 UMETA(Hidden), 
	ObjectTypeQuery25 UMETA(Hidden), 
	ObjectTypeQuery26 UMETA(Hidden), 
	ObjectTypeQuery27 UMETA(Hidden), 
	ObjectTypeQuery28 UMETA(Hidden), 
	ObjectTypeQuery29 UMETA(Hidden), 
	ObjectTypeQuery30 UMETA(Hidden), 
	ObjectTypeQuery31 UMETA(Hidden), 
	ObjectTypeQuery32 UMETA(Hidden),

	ObjectTypeQuery_MAX	UMETA(Hidden)
};
1 Like

Thank you for the explanation, I understood almost everything. The one thing that I did not understand is what these ObjectTypeQueries do and how are they used. are they implement in the objects that I am looking for in the hit test?

What are the ObjectTypeQueries?

Here is an example filling the array using both predefined and custom collision channels:

TArray<TEnumAsByte<EObjectTypeQuery> > objTypes;

// From "common" ECollisionChannel values
objTypes.Add(UEngineTypes::ConvertToObjectType(ECC_WorldDynamic));

// From "custom" collision channels.
ECollisionChannel CollisionChannel;
FCollisionResponseParams ResponseParams;
if (UCollisionProfile::GetChannelAndResponseParams(FName(TEXT("MyChannel")), CollisionChannel, ResponseParams))
{
    objTypes.Add(UEngineTypes::ConvertToObjectType(CollisionChannel));
}

See for example https://impetus-games.com/blog/Rotating-an-Actor-towards-the-Mouse-Cursor-in-UE4 for some extra details using BP.

1 Like