How do I communicate between classes?

Hi Everyone!

I have three separate classes. DoorBase, ButtonBase, and Oliver (Character class).
I would like to connect the door and button to each other so when the button is pressed, by my character (Oliver) class, the door opens.

So far, I have overlap functionality on my Character class:

Oliver.cpp

void AOliver::OnButtonVolumeBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	AButtonBase* Button = Cast<AButtonBase>(OtherActor);
	UBoxComponent* ButtonVolume = Cast<UBoxComponent>(OtherComp);

	if (OtherActor == Button && ButtonVolume)
	{
		bCanPressButton = true;
	}
}

void AOliver::OnButtonVolumeEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	//AButtonBase* Button = Cast<AButtonBase>(OtherActor);
	//UBoxComponent* ButtonVolume = Cast<UBoxComponent>(OtherComp);

	//if (OtherActor == Button && ButtonVolume)
	//{
		bCanPressButton = false;
	//}
}

void AOliver::ButtonPress()
{
	if (bCanPressButton)
	{
		if (ButtonPressAnimMontage)
		{
			UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
			AnimInstance->Montage_Play(ButtonPressAnimMontage);
			bCanPressButton = false;
		}
	}
}

Right now, I’m able to enter the ButtonBase class box volume and that enables my Character class to play an animation. ButtonBase and DoorBase are empty classes at the moment.

My question is how do I connect all 3 classes so that once my Character class interacts ButtonBase, DoorBase plays the open animation?

Thank you in advance!

1 Like

Hello there :slight_smile:
So, one way to connect this 3 classes is to create some bool variables that will be set by accessing functions of the class. For example, the DoorBase will have a bool called bOpenDoor and the function SetOpen that will set the bOpenDoor value.
So, your code will be something like this:
DoorBase.h

public:
    bool bOpenDoor;

    UFUNCTION()
    FORCEINLINE void SetOpen(bool bOpen) { bOpenDoor = bOpen; };

    UFUNCTION()
    void OpenDoor();

DoorBase.cpp

void ADoorBase::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
    if(bOpenDoor)
    {
        OpenDoor();
    }
}

void ADoorBase::OpenDoor()
{
    // Open door logic here
}

ButtonBase.h

#include "DoorBase.h"
public:
    UPROPERTY(EditInstanceOnly, Category="Door")
    ADoorBase* Door;

    bool bIsPressed;

    UFUNCTION()
	FORCEINLINE void SetPresed(bool bPressed) { bIsPressed = bPressed; };

ButtonBase.cpp

void AButtonBase::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

    if(bIsPressed == true)
    {
        Door->SetOpen(true);
    }
    else
    {
        Door->SetOpen(false);
    }
    if(Door)
    {
        if(bIsPressed == true)
        {
            Door->SetOpen(true);
        }
        else
        {
            Door->SetOpen(false);
        }
    }
}

Oliver.cpp

void AOliver::OnButtonVolumeBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	AButtonBase* Button = Cast<AButtonBase>(OtherActor);
	UBoxComponent* ButtonVolume = Cast<UBoxComponent>(OtherComp);

	if (OtherActor == Button && ButtonVolume)
	{
		bCanPressButton = true;
        AButtonBase->SetPresed(true)
	}
}

void AOliver::OnButtonVolumeEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	AButtonBase* Button = Cast<AButtonBase>(OtherActor);
	UBoxComponent* ButtonVolume = Cast<UBoxComponent>(OtherComp);

	if (OtherActor == Button && ButtonVolume)
	{
		bCanPressButton = false;
        AButtonBase->SetPresed(false)
	}
}

Compile and put a button and a door in the level, select the button, and in its details should appear a Door dropdown and you should click on the button “Pick Actor from scene” and there you select the door that the selected button should open.

Please keep in mind that this is just a sketch, you can make the necessary improvements.

If you have any questions, please let me know.

I apologize for the late reply, I’ve been extremely busy in my personal life.

Thank you for taking time to give me some insight! I’ll give it shot and I’ll definitely reach out if I have questions!

1 Like