OnComponentBeginOverlap works but not OnComponentEndOverlap

Aside from the code, here’s a list of things I’ve tried out that still don’t work:

  • Changing from AddUniqueDynamic to AddDynamic
  • Changing from OnComponentEndOverlap to OnActorEndOverlap
  • Changing both OnComponentBeginOverlap and OnComponentEndOverlap from protected to private

Below is my code. Any help is appreciated.

LevelStreamingTrigger.cpp:

ALevelStreamingTrigger::ALevelStreamingTrigger()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	OverlapVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("OverlapVolume"));
	OverlapVolume->bGenerateOverlapEvents = true;
	RootComponent = OverlapVolume;

	OverlapVolume->OnComponentBeginOverlap.AddUniqueDynamic(this, &ALevelStreamingTrigger::OverlapBegins);
	OverlapVolume->OnComponentEndOverlap.AddUniqueDynamic(this, &ALevelStreamingTrigger::OverlapEnds);
}


void ALevelStreamingTrigger::OverlapBegins(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (OtherActor->IsA(ACharacter::StaticClass()))
	{
		ACharacter* MyCharacter = UGameplayStatics::GetPlayerCharacter(this, 0);
		if (OtherActor == MyCharacter && LevelToLoad != "")
		{
			FLatentActionInfo LatentInfo;
			UGameplayStatics::LoadStreamLevel(this, LevelToLoad, true, true, LatentInfo);
		}
	}
	
}

void ALevelStreamingTrigger::OverlapEnds(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (OtherActor->IsA(ACharacter::StaticClass()))
	{
		ACharacter* MyCharacter = UGameplayStatics::GetPlayerCharacter(this, 0);
		if (OtherActor == MyCharacter && LevelToLoad != "")
		{
			FLatentActionInfo LatentInfo;
			UGameplayStatics::UnloadStreamLevel(this, LevelToLoad, LatentInfo);
		}
	}
}

LevelStreamingTrigger.h:

UCLASS()
class PROCEDURALGENERATOR_API ALevelStreamingTrigger : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ALevelStreamingTrigger();

protected:
	UPROPERTY(EditAnywhere)
		FName LevelToLoad;

private:
	// Overlap volume to trigger level streaming
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
		UBoxComponent* OverlapVolume;

	UFUNCTION()
		void OverlapBegins(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

	UFUNCTION()
		void OverlapEnds(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};
1 Like

Are you using a blueprint that inherits from ALevelStreamingTrigger? If so, did you create these event handlers before or after creating the blueprint? Does your OverlapVolume have a collision profile that supports overlapping? Are you using hot reload?

On a side note, please bump your own threads instead of bumping a thread that is almost nine months old.

Fixed it! For some reason, the LevelStreamingTrigger instance doesn’t update accordingly to the code. Therefore, I put the AddDynamic from the constructor to BeginPlay. For anyone of you who’s stuck with this problem, you may also follow this guide right here:

1 Like

Yes to all the questions you’ve asked. Thanks for contributing to help!

This is because you added the listeners after creating the blueprint of the type and are using hot reload. A practice I’ve come to use is to always close the editor whenever I’m going to make a change to code, make the change, then relaunch the editor from Visual Studio (Debug → Start without debugging). It’s annoying at first, but I’ve never run into this kind of problem since.