Was wondering if I could have some help understanding where I’m going wrong with adding this event listener. How do I listen to an object that hasn’t been created yet like a weapon that hasn’t spawned in yet?
I’m creating a Hit-scan freeze weapon for my player. I’ve set up a timer in my Hit-scan weapon script and am broadcasting an event there. I am currently listening to it in an enemy script but can’t get the listener to fire I’ve set up the listen in the begin play. I’m adding a function here to change a IsFrozen bool, for some reason the listener won’t fire:
Snippet of my enemy script:
void ANPC_Enemy::BeginPlay()
{
Super::BeginPlay();
AActor* weaponHitscan = UGameplayStatics::GetActorOfClass(GetWorld(), AWeapon_Hitscan::StaticClass());
_FreezeWeapon = Cast<AWeapon_Hitscan>(weaponHitscan);
_FreezeWeapon->OnEnemyFroze.AddUniqueDynamic(this, &ANPC_Enemy::SetEnemyFreezeState);
}
void ANPC_Enemy::SetEnemyFreezeState(bool isFrozen)
{
_IsFrozen = isFrozen;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Purple, TEXT("Works"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Purple, FString::Printf(TEXT("%d"), isFrozen ));
}
I know I’m casting here to an instance in the world but this is just an example blueprint of the class WeaponHitscan I’ve dragged into the scene, I can’t seem to get a reference to the hitscan weapon on the actual player because it’s not loaded in straight away.
I’ve added “_FreezeWeapon” as a TObjectPtr<> in the header. I wanted to use TSubclass<> as that’s what I thought you used for elements that aren’t in the world space but it won’t take any functions.
I’ve checked my broadcast and it fires properly. Not sure on what else to try.
Thanks for reading