Adding functions to an event: trouble with specifics

Hey, so I’m trying to emulate some blueprint event behavior in C++ and am getting stuck on how exactly to handle it. Here’s the blueprint:

So, from within my character class I can call GetSprite()->OnFinishedPlaying.Add, but I’m not sure how to actually set up a function to be passed in.

In my .h, I’m declaring the function that I want OnFinishedPlaying to call (ATest2DGameCharacter::EndAttack) as a multicast delegate:


DECLARE_MULTICAST_DELEGATE(EndAttack)

Then, within the character class header I’m declaring the function:


	UFUNCTION()
	void EndAttack();

From within my .cpp, I then try to add the function to OnFinishedPlaying:


	GetSprite()->OnFinishedPlaying.Add(&ATest2DGameCharacter::EndAttack);

Obviously I’m handling it wrong though, because the compiler isn’t even seeing my function as a delegate.


Error D:\GameProjects\Test2DGame\Source\Test2DGame\Test2DGameCharacter.cpp(74) : note: No constructor could take the source type, or constructor overload resolution was ambiguous
Error D:\GameProjects\Test2DGame\Source\Test2DGame\Test2DGameCharacter.cpp(74) : note: Reason: cannot convert from 'void (__cdecl ATest2DGameCharacter::* )(void)' to 'const TScriptDelegate<FWeakObjectPtr>'
Error D:\GameProjects\Test2DGame\Source\Test2DGame\Test2DGameCharacter.cpp(74) : error C2664: 'void TMulticastScriptDelegate<FWeakObjectPtr>::Add(const TScriptDelegate<FWeakObjectPtr> &)': cannot convert argument 1 from 'void (__cdecl ATest2DGameCharacter::* )(void)' to 'const TScriptDelegate<FWeakObjectPtr> &'


So, how can I set this up probably?

1 Like

To clarify you want the EndAttack() function to be called when the GetSprite()->OnFinishedPlaying() Event is called?

Yup, exactly.

Hmm after looking it up, OnFinishedPlaying isn’t the UFUNCTION(BlueprintNativeEvent) I assumed it was.
I have no idea how to use a FFlipbookFinishedPlaySignature…

That said, you could declare a C++ function:
UFUNCTION(BlueprintCallable, Category = “Any Category Name”) void CustomOnFinishedPlayingCall();

You can then connect that function to the OnFinishedPlaying Event in the blueprint.
Its kind of a loopy work around: (C++)OnFinishedPlaying -> (Blueprint)OnFinishedPlaying -> (C++)CustomOnFinishedPlayingCall()
But it does technically let you code behavior for that event in C++.

You can even add returns and arguments to the function with no problems:
UFUNCTION(BlueprintCallable, Category = “Any Category Name”) int32 CustomOnFinishedPlayingCall(float Infloat);

I haven’t been able to figure that out either. But Programmatically if you want to skip around blueprints in general. A good Algorithm would be to set up your character and NPC’s on every Tick, Check to see if your FlipBookComponent is Playing(). If it is not, Turn SetLooping() to True and Call the Play() function before changing your Characters Animation State. Then for things like Attacking. Change Your Character to Their Attack Animation and set SetLooping() To False. That way your attack animation is the last to play and Once it Ends, The Next Tick will Turn it Back on

Sorry for Necro, but I was also wondering how to use OnFinishedPlaying and this thread is about the only topic that shows up.

Since calling AddDynamic has no effect on OnFinishedPlaying, I ended up creating an FScriptDelegate Object that you can use for the OnFinishedPlaying multicast like so:

First, declare your handler function in the header. It must be a UFUNCTION but it does not need any modifiers:

UFUNCTION()
virtual void AnimationCompleteHandler();

Then, when you want to bind the event in your cpp file do this:

FScriptDelegate Delegate;
Delegate.BindUFunction(this, "AnimationCompleteHandler");
GetSprite()->OnFinishedPlaying.Add(Delegate);

This way, AnimationCompleteHandler will actually be called when the animation is complete. I hope this helps anyone else who is looking for the solution.

1 Like