Implementing an Event-Driven Door

One thing that I wish the section dug into was event driven door opening and closing. I really wanted to get the code executed every tick down as much as possible, and having one of the blueprint events broadcasted constantly seemed like a red flag. This was a little more confusing to get going than I would of hoped, but I’m happy with the result.

The first thing I needed to do was add some methods to handle an overlap begin and end event in my OpenDoor.h file:

UFUNCTION() void PressurePlateOverlapBegan(AActor* OverlappedActor, AActor* OtherActor); UFUNCTION() void PressurePlateOverlapEnded(AActor* OverlappedActor, AActor* OtherActor);

Some interesting bits on this. I needed to add UFUNCTION() for the multi-cast delegates to work. On initially running this code I got a complaint about argument names. I thought it meant something with UFUNCTION(), but it actually needed the argument names (OverlappedActor and OtherActor); I originally had those omitted.

The implementation for these two methods was quite simple:

void UOpenDoor::PressurePlateOverlapBegan(AActor* OverlappedActor, AActor* OtherActor) { if (GetTotalMassOfActorsOnPlate() >= TriggerMass) { OnOpen.Broadcast(); } } void UOpenDoor::PressurePlateOverlapEnded(AActor* OverlappedActor, AActor* OtherActor) { if (GetTotalMassOfActorsOnPlate() < TriggerMass) { OnClose.Broadcast(); } }

To wire these up, In BeginPlay after checking that PressurePlate existed, I did this:
PressurePlate->OnActorBeginOverlap.AddDynamic(this, &UOpenDoor::PressurePlateOverlapBegan); PressurePlate->OnActorEndOverlap.AddDynamic(this, &UOpenDoor::PressurePlateOverlapEnded);

That code felt a little odd. OnActorBeginOverlap seemed like a single method signature in C++, but that interesting macro I found from googling (AddDynamic) somehow knew what to do with it.

Lastly, I removed all the code from the TickComponent method except the Super call. It was all handled via events now.

Did anyone else give this a shot? Is this the best way to do it?

Thanks :slight_smile:

I read the header of the base class, UActorComponent, and it shows that TickComponent is not pure-virtual so you should be able to remove TickComponent from the sub-class entirely, from both header and .cpp files, and set PrimaryComponentTick.bCanEverTick = false; in your component’s constructor for pure, event-driven nirvana.

Haven’t tried it yet…