Getting child component from actor

I’m trying to teleport to a location when using a door using the following code:


FHitResult* HitResult = new FHitResult();
	FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();
	FVector ForwardVector = FirstPersonCameraComponent->GetForwardVector();
	FVector EndTrace = ((ForwardVector*250.0f) + StartTrace);
	FCollisionQueryParams* TraceParams = new FCollisionQueryParams();

	if (GetWorld()->LineTraceSingleByChannel(*HitResult,StartTrace,EndTrace,ECC_Visibility,*TraceParams))
	{
		DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0),true);
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("You hit: %s"),*HitResult->Actor->GetName()));
		if (HitResult->Actor->GetName() == "Door1")
		{
			SetActorLocation() //I want to put the location of a child compononent of my HitResult->Actor
			
		}

This is the Hitresult->Actor , I want to get the location of the destination component, how do I get it through code
6dd502d81b91755ce76a8cd3dd50969b.png



ADoorTrigger *HitDoorTrigger = Cast<ADoorTrigger>(HitResult->Actor);
if (HitDoorTrigger){
    HitDoorTrigger->Destination->GetComponentLocation();
    //etc...
}


The Cast<>() will convert the hit result pointer into your custom door trigger class if you hit a door trigger, letting you access its members like the Destination component. If it’s not a door trigger the Cast will return nullptr, so that allows you to check it and only do certain code if the hit actor is a certain type. Cast<>() is very important for a lot of things in UE4, so it’s worth learning how to use it.

It says ADoorTrigger is undifined. And I can’t make that class public for some reason?

6b05a08bbccd64257b3e908e22a600d3.png

Ok so, it’s not a matter of public/private (C++ doesn’t have public/private classes), it’s the fact that ADoorTrigger isn’t defined in the .cpp you’re trying to use it in, what you need to do is #include “DoorTrigger.h” in the .cpp file that is using the Cast<>() so that it knows what an ADoorTrigger class is. Each .cpp file is compiled separately and only knows about the classes that you #include (and basic classes that MSVC and UE4 always include). This is a basic C++ thing, so you should probably go through some tutorials to get a better understanding, otherwise you’ll run into a lot more problems down the road.

Yea, I’ve always used c# so alot is new for me.

Now it works by the way!
I used:
ADoorTrigger *HitDoorTrigger = Cast<ADoorTrigger>(HitResult);

Thanks alot for your help!

Ok nevermind, my code didn’t compile.

I’ve tried what you said and got a syntax error:


	if (GetWorld()->LineTraceSingleByChannel(*HitResult,StartTrace,EndTrace,ECC_Visibility,*TraceParams))
	{
		DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0),true);
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("You hit: %s"),*HitResult->Actor->GetName()));
		if (HitResult->Actor->GetName() == "Door1")
		{
			ADoorTrigger *HitDoorTrigger = Cast<ADoorTrigger>(HitResult->Actor);
			if (HitDoorTrigger) {
				SetActorLocation(HitDoorTrigger->Destination->GetComponentLocation());
			}
			
		}
	}

error: no instance of overloaded function “Cast” matches the argument list

Oh, I forgot that HitResult->Actor is actually a TWeakObjectPtr<AActor>, not a raw pointer, so the argument type is wrong and the compiler is telling you about it. You should use Cast<ADoorTrigger>(HitResult->Actor->Get()). UE4 sometimes uses wrappers around raw pointers, like TSharedPtr or TWeakObjectPtr for safer handling. These pointer wrappers have accessor methods like Get(), or * to give you the raw pointer. You could have used the code suggestions to look at the types of FHitResult::Actor and the arguments that Cast expects and figured this out for yourself though.

Just want to add to make sure you’re including “DoorTrigger.h” at the top of your class, so the class knows what an ADoorTrigger is.

Thanks for the replies. HitResult->GetActor() worked too :slight_smile: