You should have the trigger thing (in this case, the freeze gun) emit the events (“things got frozen, yo”) and then either use some kind of generalized event bus, or have the emitter decide whom to emit the events to at the time of emitting.
(Other users’ comment)
You should make the action event driven.
I’m not sure if I’m misunderstanding here but I feel like my weapon is emitting/Broadcasting my event. I have a fire function in my weapon that does the following:
if(UKismetSystemLibrary::LineTraceSingle(GetWorld(), StartLoc, EndLoc, UEngineTypes::ConvertToTraceType(ECC_Visibility), true, {this, GetOwner()},
EDrawDebugTrace::ForDuration, Hit, true, FLinearColor::Red,FLinearColor::Green, 5.0f))
{
if(Hit.GetActor()->ActorHasTag("GameRuleTarget"))
{
_TimeToFreeze = 5;
_IsFrozen = true;
//Emitting Event
OnEnemyFroze.Broadcast(_IsFrozen);
//Start timer
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Black, TEXT("Started Timer"));
GetWorld()->GetTimerManager().SetTimer(_TimerDecrease, this, &AWeapon_Hitscan::DecreaseCountdown, 1.f, false);
return true;
}
}
The decreaseCountdown just broadcasts setting the value back to false after the timer is up.
The event is created and broadcasted in the freeze weapon and is emitted on the action of the player shooting the target. The problem I have is the event listener on the enemy class (Shown in original post) won’t work. I think there is something wrong in that first code snippet.
The connection between the freeze weapon and the enemy is the issue. I know it isn’t the best practice to have these lose connections between classes but I think it is warranted in this case for use in my behaviour tree.