Create AnimNotify in c++

Hi. I’m trying to create AnimNotifies in code so I can drive notifies from a excel datatable. The code below seems to work, it adds a notify to notify track 0 in my UAnimMontage is visible in the editor and fires events in game. However if I click the notify the editor crashes immediately.

I can see in the call stack that it’s an index out of range error but I’m not sure where it’s coming from. Given that it crashes when I click on it, is there something I need to set for the notifies property editor not to throw a fit?

Any ideas? This has me stumped. I’m not a very seasoned coder so baby steps are appreciated.
Thanks in advance.

Code:

montage->Notifies.Empty();

	// Insert a new notify record and spawn the new notify object
	int32 NewNotifyIndex = montage->Notifies.Add(FAnimNotifyEvent());
	FAnimNotifyEvent& NewEvent = montage->Notifies[NewNotifyIndex];
	NewEvent.NotifyName = FName("Combo");
	
	class UObject* AnimNotifyClass = ConstructObject<UObject>(UAnimNotifyState::StaticClass());
	NewEvent.NotifyStateClass = Cast<UAnimNotifyState>(AnimNotifyClass);

	// Set default duration to 1 frame for AnimNotifyState.
	if (NewEvent.NotifyStateClass)
	{
		NewEvent.NotifyName = FName(*NewEvent.NotifyStateClass->GetNotifyName());
		NewEvent.SetDuration(1 / 30.f);
		NewEvent.EndLink.Link(montage, NewEvent.EndLink.GetTime());
	}
	else
	{
		NewEvent.NotifyName = FName(*NewEvent.Notify->GetNotifyName());
	}
	montage->MarkPackageDirty();

Call Stack:

Unknown exception - code 00000001 (first/second chance not available)

Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.6\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 679] 
Array index out of bounds: -1 from an array of size 1

KERNELBASE + 293772 bytes
UE4Editor_Core + 3092333 bytes
UE4Editor_Core + 1597672 bytes
UE4Editor_Core + 1479042 bytes
UE4Editor_Persona + 685626 bytes
UE4Editor_Persona + 669553 bytes
UE4Editor_PropertyEditor + 2498525 bytes
UE4Editor_PropertyEditor + 2763666 bytes
UE4Editor_PropertyEditor + 2435518 bytes
UE4Editor_PropertyEditor + 2657981 bytes
UE4Editor_PropertyEditor + 2659926 bytes
UE4Editor_Kismet + 3608592 bytes
UE4Editor_Kismet + 3823122 bytes
UE4Editor_Kismet + 3803976 bytes
UE4Editor_Kismet + 1037444 bytes
UE4Editor_Persona + 2113709 bytes
UE4Editor_Persona + 3134492 bytes
UE4Editor_Persona + 3156828 bytes
UE4Editor_Persona + 2124127 bytes
UE4Editor_Persona + 1838417 bytes
UE4Editor_Persona + 1853142 bytes
UE4Editor_Persona + 2169587 bytes
UE4Editor_Persona + 2086953 bytes
UE4Editor_Slate + 427589 bytes
UE4Editor_Slate + 156365 bytes
UE4Editor_Slate + 832709 bytes
UE4Editor_Slate + 770598 bytes
UE4Editor_Core + 2484697 bytes
UE4Editor_Core + 2400747 bytes
UE4Editor_Core + 2489259 bytes
UE4Editor_Core + 2387170 bytes
user32 + 5054 bytes
user32 + 4567 bytes
UE4Editor_Core + 2490470 bytes
UE4Editor!FEngineLoop::Tick() + 3434 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\launchengineloop.cpp:2193]
UE4Editor!GuardedMain() + 479 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\launch.cpp:131]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.6\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

NewEvent.NotifyStateClass = Cast(AnimNotifyClass);

That line is wrong as you are casting a UClass to a UAnimNotifyState. What you want is UAnimNotifyState::StaticClass()

Sorry, the programmer that set that struct up broke convention, so I made a bad assumption. Typically, SomeThingClass means the static class (UClass) of the instance and not an instance.

I just looked at CreateNewNotifyAtCursor() and in that code only one of the things will be true: either it’s a state object or a notify object. One of those casts are going to fail.

My guess is that the Notify field is not being zeroed to NULL, where their code is zeroing in both paths. Try zeroing it.

Hi Joe, thanks for getting back to me.
I tried UAnimNotifyState::StaticClass() but that returns a UClass so I have to cast to UAnimNotifyState anyway right? Maybe I’m missing something?
NewEvent.NotifyStateClass = UAnimNotifyState::StaticClass();

I’ve set things up the same way as I found in the source code at SAnimNotifyTrack::CreateNewNotifyAtCursor and my code above seems to work fine and fires notify events with my custom notify, it’s just when I click on it in persona that things go haywire.
Do I need to register a new notify with persona in any particular way that may cause a crash when I try to view its properties?

Apologies if I’m doing something silly here but the code I have seems to be working for the most part.

So it seems I overlooked one little thing. I forgot to link my new notify event to my montage. I added this line just after creating my new event and naming it and everything seems to be working as expected.
NewEvent.Link(montage, 0.05f);

Thanks for your help Joe, I hope I didn’t waste too much of your time.

Neglected to link the new notify to the montage. Silly mistake. The line below fixed it.

NewEvent.Link(montage, 0.05f);

Where do you put your code?