Sharing my version of the door control code

Good evening from Finland! I loved the BuildingEscape course and I wanted to share my version of the DoorOpen code. I implemented your teachings to my own project, that I’ve been working on for a some time.

Feel free to use my code as you want.

In my version there is a trigger volume around the door. Being inside the trigger volume and pressing R opens the door. If the door is already open, it will be closed. The door opening angle and speed are set in a timeline, just like in the course.

Here’s the code and a blueprint from one of my doors.

OpenDoor.h

Copyright Ville Happo 2016
 
#pragma once
 
#include "Components/ActorComponent.h"
#include "OpenDoor.generated.h"
 
///needed for creating blueprint events
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDoorEvent);
 
 
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
    GENERATED_BODY()
 
public:
    // Sets default values for this component's properties
    UOpenDoor();
 
    // Called when the game starts
    virtual void BeginPlay() override;
   
    // Called every frame
    virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
 
    // door opening event for blueprint
    UPROPERTY(BlueprintAssignable)
    FDoorEvent OnOpen;
 
    // door closing event for blueprint
    UPROPERTY(BlueprintAssignable)
    FDoorEvent OnClose;
 
    //Tiggervolume chosen for the door
    UPROPERTY(EditAnywhere)
    ATriggerVolume* TriggerDoor = nullptr;
 
    //Assigned to default pawn at beginplay (doesn't exist before runtime)
    AActor* ActorThatOpens = nullptr;
 
    UInputComponent* InputComponent = nullptr;
 
private:
    // Send broadcast to blueprint if player is in the door trigger and is pressing "R"
    void IsPlayerInterracting();
 
    //Boolean for checking if door should be opened or closed
    bool doorIsOpen = false;
 
    //Setting up Interraction key "R" to control doors
    void SetupInputComponent();
};

OpenDoor.cpp

// Copyright Ville Happo 2016
 
#include "BuildingEscape.h"
#include "OpenDoor.h"
 
UOpenDoor::UOpenDoor()
{
    PrimaryComponentTick.bCanEverTick = true;
}
 
// Called when the game starts
void UOpenDoor::BeginPlay()
{
    Super::BeginPlay();
    ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
    SetupInputComponent();
 
    /// Check if the door has a trigger assigned
    if (!TriggerDoor)
    {
        UE_LOG(LogTemp, Error, TEXT("%s Missing trigger volume"), *GetOwner()->GetName());
    }
}
 
void UOpenDoor::SetupInputComponent()
{
    InputComponent = ActorThatOpens->FindComponentByClass<UInputComponent>();
    if (InputComponent)
    {
        InputComponent->BindAction("Interract", IE_Pressed, this, &UOpenDoor::IsPlayerInterracting);
    }
}
 
// Called every frame
void UOpenDoor::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
    Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
}
 
/// If player is overlapping the TriggerDoor --> See if door should be opened or closed based on "bool doorIsOpen"
void UOpenDoor::IsPlayerInterracting()
{
    /// Return if the trigger is lost for some reason
    if (!TriggerDoor) {
        return;
    }
    /// Check if Actor that can open the door is assigned
    if(!ActorThatOpens){
        return;
    }
    if (TriggerDoor->IsOverlappingActor(ActorThatOpens)) {
        if (!doorIsOpen)
        {
            OnOpen.Broadcast();
            doorIsOpen = true;
        }
        else
        {
            OnClose.Broadcast();
            doorIsOpen = false;
        }
    }
}

Door blueprint using events, that are created in the code

I tried to upload a demo video, but sadly I can’t, since I’m a noob at this forum :frowning:

Visit my blog @ www.villevanille.com if you want to see my progress, as I educate myself in game development & 3D modeling. I’d love some feedback :slight_smile:

1 Like

Privacy & Terms