0

Basically, i want a door to open in unreal engine 5 with c++. I downloaded a door mesh from Sketchfab but the pivot point was at the senter of the mesh, so i created a actor class and attached a Scene component to it and to the scene component i attached a mesh then i kinda moved the mesh so the pivot point of the scene component would be at the buttom left corner of the door mesh. i already setup a function in the character that will be executed when he is looking at the door and press on the e key I also setup a function in the scene component (OnInteract) that should rotate the door 90 degrees. the problem is that i need to call this function from the character class.

here is the code for the OnInteract function in the SceneComponent:

void UDoorSceneComponent::OnInteract()
{
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("The function is called"));
    FRotator NewRotation = GetRelativeRotation();
    NewRotation.Yaw -= 90;
    SetRelativeRotation(NewRotation);
}

code for the Interact function in the character class:

void APlayerCharacter::Interact()
{
    FHitResult HitResult;

    //Makes an invisible line between the Start and End point
    FVector Start = FollowCamera->GetComponentLocation();
    FVector End = Start + FollowCamera->GetForwardVector() * InteractLengh;
    
    // if something is between the line then it stores the information in HitResult
    GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECollisionChannel::ECC_Visibility);

    // Cast to Door. will return a nullptr if the object between Start and End wasnt the door
    ADoor1* DoorCast = Cast<ADoor1>(HitResult.GetActor());

    // if Door exists (not a nullptr) run the open door funcion
    if(DoorCast)
    {
        // Got the actor refrence
        AActor* Door1Act = UGameplayStatics::GetActorOfClass(GetWorld(), ADoor1::StaticClass());
        ADoor1* Door1Cast = Cast<ADoor1>(Door1Act);
        if(Door1Cast == nullptr)
        {
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("nullptr"));
        }
        else
        {
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("not nullptr"));
        }
    }


}

as you can see, i have tried to get a refrence to the door class but i dont know how can i execute the OnInteract function from here.. I realy tried looking this up on google and didnt really found something. I use manjaro linux as the operating system. Im also new to Unreal Engine so please take it easy :) thanks

edit: I added some debug messages and added a bool named test in the door class (set to true by default) then in the Interact function in the character class i added an if statement if(Door1Cast->test) that if its true ill get a screen debug message and i runed the code and i did get the debug message. I think that if i can get a refrence to the DoorSceneComponent in the Door1 class. Any ideas how?

rdex999
  • 3
  • 4
  • OnInteract seems like a callback function that needs to be called when Interact event will happen on the door. I am not familiar with UE but you somehow have to trigger this event on the door. – kiner_shah Apr 06 '23 at 09:22
  • i do trigger the event the Interact function works, i just need to find a way to call the OnInteract function – rdex999 Apr 06 '23 at 09:25
  • Where do you trigger Interact? In the code you posted I don't see anything which points to Interact call. – kiner_shah Apr 06 '23 at 09:28
  • this is the triger for the function PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &APlayerCharacter::Interact); but that wont call it, i did set up the call in unreal engines input settings – rdex999 Apr 06 '23 at 09:32
  • I don't know the class hierarchy here, but maybe you should start logging debug messages in Interact functions of different components. Since OnInteract is in UDoorSceneComponent, I thought Interact event should also be triggered in UDoorSceneComponent. – kiner_shah Apr 06 '23 at 09:36
  • 1
    i did as you said and i added some debug messages, now i figured out that i need a refrence to the DoorSceneComponent in the Door1 class so then i can call the OnInteract function. thank you for the help! – rdex999 Apr 06 '23 at 10:18

1 Answers1

0

As you can see, you are able to use test field in Door object.

Now you want to be able to use SceneComponent as well. So, you need to make it field.

In your Door class declare private:

UPROPERTY(VisibleAnywhere)
USceneComponent* MySceneComponent

In constructor:

MySceneComponent = CreateDefaultSubobject<USceneComponent>("SceneComponent");

And create method GetMySceneComponent(), which returns the pointer to MySceneComponent.

After it, you can cast to your Door class, and call GetMySceneComponent() method to get scene component you need.

apokrif
  • 21
  • 3
  • are you sure thats a UFUNCTION ? i think thats a UPROPERTY – rdex999 Apr 06 '23 at 12:06
  • Yes, ofc it should be UPROPERTY. My bad :) – apokrif Apr 06 '23 at 12:43
  • oh and how to i create the GetMySceneComponent() method? something like: USceneComponent GetDoorScene(); and like USceneComponent ADoor1::GetDoorScene() { return *DoorScene; } because i get a compile error "Calling a private constructor of class 'USceneComponent'" – rdex999 Apr 06 '23 at 12:51
  • Yeah, your method could look like: `USceneComponent* GetDoorScene();` `ADoor1::GetDoorScene() { return DoorScene; }` without * in c++ code, cuz your property is already a pointer – apokrif Apr 07 '23 at 13:05