My aim is it to access a function of the "door actor object" from the code of my "player character actor object", for instance to open or close the door.

Would be great if someone could fix my attempt or know a similar or better solution.

This is code from the "door":

#include "DefaultDoorRotator.h"
#include "FPSCharacter.h"

void ADefaultDoorRotator::OnOverlapBegin(UPrimitiveComponent* OverlapComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, int32 OtherBodyIndex, bool bFromSweepm, const FHitResult& SweepResult)
{
    if (player_controled_door)
    {
        if (OtherActor != nullptr)
        {
            if ((OtherActor->GetClass()->GetDefaultObject<AFPSCharacter>()!=0))
            {
                OtherActor->GetClass()->GetDefaultObject<AFPSCharacter>()->set_interaction_object(ActionComponent);
                GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Registered Door on Player!"));
            }
        }


    }

}

Here from the player:

#include "FPSCharacter.h"
#include "DefaultDoorRotator.h"

USceneComponent* object_for_interaction;

    UFUNCTION()
    void set_interaction_object(USceneComponent* obj)
    {
        if (obj != 0)
        {
            object_for_interaction = obj;
        }

    }

void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    PlayerInputComponent->BindAction("CharacterInteract", IE_Pressed, this, &AFPSCharacter::Interaction_start);
}

void AFPSCharacter::Interaction_start()
{
    want_to_interact = true;
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Want to interact!"));
    if (object_for_interaction != 0)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Begin to interact!"));
        object_for_interaction->GetClass()->GetDefaultObject<ADefaultDoorRotator>()->ToggleDoor();

    }


}

When this would work, the function of the door actor object should run.

0 Answers