Accessing a function from another class

Hi Guys iam fairly new to UE4 cpp, basically iam trying to call a function from a class Named as DoorActor to the Interact actor class what have done so far.

ADoorActor.cpp

the Function i need to call

void ADoorActor::OpenDoor()
{

	const FVector CurrentLoc = GetActorLocation();
	FVector NewLoc = FVector(0.0f, 0.0f, -200.0f) + CurrentLoc;
	SetActorLocation(NewLoc);
}

Interactable.cpp

void AInteractable::NotifyActorBeginOverlap(AActor* OtherActor)
{

	Super::NotifyActorBeginOverlap(OtherActor);
	AMainCharacter* MainPlayer = Cast<AMainCharacter>(OtherActor);
	if (MainPlayer)
	{
		UE_LOG(LogTemp, Warning, TEXT("Interacting Box"));
		MainPlayer->SetInteractActor(this);
	}
}

// This Function is what i need answer for 
void AInteractable::Use_Implementation()
{
	//fIND THE OBJECT IN THE wORLD
	for (TObjectIterator<ADoorActor> Itr; Itr; ++Itr)
	{
		if (Itr->IsA(ADoorActor::StaticClass()))    
		{
			ADoorActor* Door = *Itr;
			Door->GetOpenDoor();
			UE_LOG(LogTemp, Warning, TEXT("Door Interacted"))
		}
	}
}

this is my Charcter class which is calling Interactabe on button press

void AMainCharacter::OnEquip()
{

if (CurrentWeapon)
{
	CurrentWeapon->EquipWeapon(this);
	UE_LOG(LogTemp, Warning, TEXT("Has Weapon"));
	bHasWeapon = true;
}

if (InteractActor == nullptr)
{
	return;
}

if (InteractActor->GetClass()->ImplementsInterface(UUseInterface::StaticClass()))
{
	UE_LOG(LogTemp, Warning, TEXT("Interacted"));
	IUseInterface::Execute_Use(InteractActor);
}

}

My Question is,
Is there any other way to call the ADoorActor function OpenDoor() without using iteration also my Door is placed in the world, your help is mush appreciated.

You need to work on cpp tutorials first to understand OOP programming basics.

Secondly your design should be like to use LineTraceSingleByChannel

What this does is creates a lazer point to the amount of range and checks the objects hit by the lazer.
If your door has been hit by the lazer, your character will interact the door.

Pseudocode is :

  • E is your interact button.

  • When you click e, create LineTraceSingleByChannel

  • Get the hit result

  • If your hit result has the door. You will do casting here.

  • Open the door.

     FVector CameraLocation;
     FRotator CameraRotation;
    
     GetController()->GetPlayerViewPoint(CameraLocation, CameraRotation);
    
     FVector TraceStart = CameraLocation;
     FVector TraceEnd = (CameraRotation.Vector() * 1000) + TraceStart;
     FHitResult TraceHit;
    
     FCollisionQueryParams QueryParams;
     QueryParams.AddIgnoredActor(this);
    
     if (GetWorld()->LineTraceSingleByChannel(TraceHit, TraceStart, TraceEnd, ECC_Visibility, QueryParams))
     {
         if (TraceHit.GetActor())
         {
              // do logic casting etc.
         }
     }
    

Additionally as an advice, create a interaction component for your all classes that its interactable for your character and search for interaction components.

@CtnDev thank you for the reply i appreciate your answer but I know a bit about cpp and i agree i have alot to go. But the thing is my call function goes like this → character call the interaction class which implement the interface and interatable class call the door class which has the function use the OpenDoor(), although I managed to make the function work without casting but my question was is there any way to get the object in the world without iteration like I have been uing below
for (TObjectIterator Itr; Itr; ++Itr) { if (Itr->IsA(ADoorActor::StaticClass())) { ADoorActor* Door = *Itr; Door->GetOpenDoor(); UE_LOG(LogTemp, Warning, TEXT("Door Interacted")) } }

Can you lookup this page

It may help you. Actually you dont need any iteration at all.