This is code, where i use Notify
Notify:
h
DECLARE_MULTICAST_DELEGATE_OneParam(FOnNotify, USkeletalMeshComponent*)
UCLASS()
class MYPROJECT_API UReloadAnimNotify : public UAnimNotify
{
GENERATED_BODY()
public:
virtual void Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation) override;
FOnNotify OnNotified;
};
cpp
void UReloadAnimNotify::Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation)
{
OnNotified.Broadcast(MeshComp);
Super::Notify(MeshComp, Animation);
}
weapon comp:
h
bool IsReloadInProgress = false;
void InitAnim();
UFUNCTION()
void OnReloadFinishing(USkeletalMeshComponent* MeshComp);
bool CanFire() const;
bool CanReload() const;
cpp:
bool UWeaponComponent::CanFire() const
{
return CurrentWeapon && !IsReloadInProgress;
}
bool UWeaponComponent::CanReload() const
{
return !IsReloadInProgress && CurrentWeapon->CurrentAmmo.CountOfClips != 0 && CurrentWeapon->CanReoad();
}
void UWeaponComponent::InitAnim()
{
if (!CurrentReloadAnimation) return;
const auto NotifyEvents = CurrentReloadAnimation->Notifies;
for (auto NotifyEvent : NotifyEvents)
{
auto ReloadFinishedNotify = Cast<UReloadAnimNotify>(NotifyEvent.Notify);
if (ReloadFinishedNotify)
{
ReloadFinishedNotify->OnNotified.AddUObject(this, &UWeaponComponent::OnReloadFinishing);
break;
}
}
}
void UWeaponComponent::OnReloadFinishing(USkeletalMeshComponent* MeshComp)
{
ACharacter* Character = Cast<AMainCharacter>(GetOwner());
if (!Character) return;
if (Character->GetMesh() != MeshComp) return;
IsReloadInProgress = false;
}
void UWeaponComponent::ChangeClip()
{
if (!CanReload()) return;
CurrentWeapon->ChangeClip();
IsReloadInProgress = true;
PlayAnimMontage(CurrentReloadAnimation);
}
void UWeaponComponent::Fire()
{
if (!CurrentWeapon && CurrentWeapon->CurrentAmmo.CountOfBullets == 0 && CanFire() == false ) return;
if (CanFire() == false) return;
CurrentWeapon->Fire();
}
What i must to do?