What is EObjectTypeQuery and how to use it

So i have decided to use coding more and i have a question regarding EObjectTypeQuery

In CapsuleTraceSingleForObjects there is

TArray > & ObjectTypes

so i have figured out that TEnumAsByte is some dark magic that should be used for enums in unreal engine (altough a word of explanation here would be nice :>)

But i am completly clueless about the EObjectTypeQuery because the documentation page explains preety much nothing

TEnumAsByte is simply the variable type you use when declaring a variable that holds a reference to an enum value, e.g. suppose you’ve declared your enum as follows:

UENUM(BlueprintType)
namespace EFruitEnum
{
	enum EFruit
	{
                FRUIT_Apple                UMETA(DisplayName="Apple"),
                FRUIT_Pear                  UMETA(DisplayName="Pear"),
                FRUIT_Peach                UMETA(DisplayName="Peach")
        }
}

if you’d then like to have a variable that holds one of these values, you’d declare it as follows:

UPROPERTY()
TEnumAsByte<EFruitEnum::EFruit> MyFruit;

As for what exactly EObjectTypeQuery does, I have no idea whatsoever.
It simply lists 32 ObjectTypeQueries.
Looks disapprovingly at UE4 devs

So i cant store enum values in good old ints in unreal engine but i have to use TEnumAsByte? Do i get it right? :slight_smile:

TEnumAsByte is a class, and its constructor can take both int32 annd uint8.

There is a function that is part of the UEngineTypes class that you might find helpful:

static EObjectTypeQuery ConvertToObjectType
(
    ECollisionChannel CollisionChannel
)
1 Like

The 32 ObjectTypeQueries actually correspond to the options in the drop down list in the editor.
I discovered this by trial and error.

For Example, calling the following for a URadialForceComponent:
RemoveObjectTypeToAffect(EObjectTypeQuery::ObjectTypeQuery4);
would remove Physics Body.

RemoveObjectTypeToAffect(EObjectTypeQuery::ObjectTypeQuery5);
would remove Vehicle.

2 Likes

it’s bewildering that there’s no macros for this… are we missing something here?

Oh, it’s because it’s KismetSystemLibrary and intended only for blueprints. In code it actually converts these into collision channels and uses World->SweepMultiByObjectType(OutHits, Start, End, FQuat::Identity, ObjectParams, FCollisionShape::MakeSphere(Radius), Params)

1 Like

I finally got it to work
(Raytracing fro custom World Object Type Channels)

For example in header file of the actor component add something like this

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "My Category")
	TArray<TEnumAsByte<EObjectTypeQuery>> HitObjectTypes;

then in cpp file

FCollisionObjectQueryParams CollisionObjectParams;
for (auto Iter = HitObjectTypes.CreateConstIterator(); Iter; ++Iter)
{
	const ECollisionChannel& Channel = UCollisionProfile::Get()->ConvertToCollisionChannel(false, *Iter);
	CollisionObjectParams.AddObjectTypesToQuery(Channel);
}

And then you can raycast

FHitResult Hit;	
float TraceDist = 300;
FVector Start = GetOwner()->GetActorLocation();

//For detecting hits below
FVector End = Start + (GetOwner()->GetActorUpVector() * -1 * TraceDist);
FCollisionQueryParams TraceParams;

bool bHit = GetWorld()->LineTraceSingleByObjectType(Hit, Start, End, CollisionObjectParams, TraceParams);
1 Like