OnActorBeginOvelap.AddDynamic this is null

Im trying to bind a function to a box trigger that the level script has a reference to.

in the .h

ALevelTrigger* LevelTrigger;

UFUNCTION()
void BeginOverlap(AActor* OverlappedActor, AActor* OtherActor);

begin overlap implementation

void ALevelOneScriptActor::BeginOverlap(AActor* OverlappedActor, AActor* OtherActor)
{
	UE_LOG(LogTemp, Warning, TEXT("OverlappedActor: %s"), *OverlappedActor->GetName());
	UE_LOG(LogTemp, Warning, TEXT("OtherActor: %s"), *OtherActor->GetName());
}

in BeginPlay in level script cpp

LevelTrigger->OnActorBeginOverlap.AddDynamic(this, &ALevelOneScriptActor::BeginOverlap);

This causes a crash and error which points me to this line

check(DelegateOwner->IsValidLowLevelFast(false)); // Most likely the delegate is trying to be used on the stack, in an object it wasn't defined for, or for a class member with a different name than it was defined for. It is only valid for a sparse delegate to be used for the exact class/property name it is defined with.

This seems to mean i cant use ALevelOneScriptActor as a delegate although this seems like a pretty standard thing to do so i dont know what im doing wrong.

Thanks in advance!

Without diving too deep here, you might be binding too early. Your level’s BeginPlay might get called before LevelTrigger is spawned (just guessing). Try to find a different place to bind. It should be somewhere you know the LevelTrigger is actually spawned.

You could test this by just adding a delay to your bind:

FTimerHandle TempTimer;
GetWorld()->GetTimerManager().SetTimer
(
	TempTimer,
	[=]() 
    { 
       LevelTrigger->OnActorBeginOverlap.AddDynamic(this, &ALevelOneScriptActor::BeginOverlap);
    },
	1.0f,
	false
);

Assuming that you know that this is not null when calling AddDynamic() (you could add an assert, or log, or check with a breakpoint,) the next thing to make sure is that your LevelTrigger is tagged with a UPROPERTY() so that GC knows about it.

UPROPERTY()
ALevelTrigger *LevelTrigger;

Cheers guys, the timer didnt fix it but made it a lot easier to see whats going on. Think the UPROPERTY() ultimately fixed it in the end, Thanks!