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);
};