Download

Get AnimNotify in AnimMontage

i created some anim notify classes in C++

For example, when u want to equip something, u don’t always want to attach the item mesh to the pawn at the start or end of the equip animation.
So a notify can be set for this, but it’s optional.

So in the equipment class i need to check if there is an AttachEquipmentAnimNotify present in the AnimMontage to know if the notify will handle attaching or the equip call of the equipment should handle it.

Meanwhile i found the solution, so wanted to share
Maybe i should add it to the wiki but don’t know how to get started.

Using the AnimNotify in your AnimMontage can be found here. The C++ code example provided in this page seem to be out-dated.

Header file



// Copyright 2015 Deborggraeve Randy. All Rights Reserved.

#pragma once

#include "Animation/AnimNotifies/AnimNotify.h"
#include "SwatAnimNotifyAttachEquipment.generated.h"

/**
 * 
 */
UCLASS()
class SWATRELOADED_API USwatAnimNotifyAttachEquipment : public UAnimNotify
{
public:
	GENERATED_UCLASS_BODY()
	
public:
	virtual void Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation) override;
	
};


Source file



// Copyright 2015 Deborggraeve Randy. All Rights Reserved.

#include "SwatReloaded.h"
#include "SwatAnimNotifyAttachEquipment.h"

USwatAnimNotifyAttachEquipment::USwatAnimNotifyAttachEquipment(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	
}

void USwatAnimNotifyAttachEquipment::Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation)
{
	Super::Notify(MeshComp, Animation);

	if (MeshComp && MeshComp->GetOwner())
	{
		auto SwatCharacter = Cast<ASwatCharacter>(MeshComp->GetOwner());
		if (SwatCharacter)
		{
			auto SwatHandheldEquipment = SwatCharacter->GetCurrentHandheldEquipment();
			if (SwatHandheldEquipment)
			{
				SwatHandheldEquipment->AttachMeshToPawn();
			}
		}
	}
}


Finally in my equip method i added to following to see if the AttachToMesh is handled by the AnimNotify or not




	// Attach the mesh to the pawn
	if (SwatHandheldEquipment)
	{
		// Check if we have anim events
		bool bNotifyFound = false;
		if (AnimMontage != nullptr)
		{
			for (auto AnimNotify : AnimMontage->Notifies)
			{
				if (AnimNotify.Notify && AnimNotify.Notify->IsA<USwatAnimNotifyAttachEquipment>())
				{
					bNotifyFound = true;
					break;
				}
			}
		}

		// Equip the mesh if not handled by animation events
		if (!bNotifyFound || EquipTime <= 0.0f)
		{
			SwatHandheldEquipment->AttachMeshToPawn();
		}
	}


Nicely done!

At the time I didn’t have this alternative, so what I did was taking a pointer to the anim, get the time for the anim socket - you can actually get the number & time for each,etc - and set a timer for it. But this option is great, well done!