How to start or stop sprite flipbook from looping or other alternatives

Hoping I’m posting this in the right place…

Ok so, I want to have my character play a crouch animation while the crouch button is held down and stop the animation when it has finished (no looping). When I release the crouch button I want to play an uncrouch animation and as soon as that animation has finished, I want to revert back to my idle/run animations (with looping) ect. (I’ve tried multiple things but there is little to no info online that tells me how to check when my animations have finished)

Even with the code as it is right now for whatever reason, after pressing and releasing crouch once, all my standard animations (running/idle/jumping) stop looping… The problem seems to stem from the uncrouch function as if I release the button before theI have absolutely no idea why that is. Especially after setting looping back to true after the uncrouch function finishes. Any help is appreciated!

The documentation for Paper2D seems to be terrible. You should be able to use this

OnFinishedPlaying | Unreal Engine Documentation

So you would Add to that delegate for Uncrouch and then remove it in your function.

I’m curious on how to right that out in code

would It be done like this by chance?
I don’t really have any examples to play around with so I’m not sure If I’m doing it right

*write

If possible, I’d love to discuss this in the gamedev.tv discord :slight_smile:

Have you done the GameDev.tv Unreal C++ course?

Yup! it was great! Finished 100% However, some of the setups I’m trying to do in this project weren’t necessarily done in the course which is ok but It just means a little more researching for me and asking around.

I ask as delegates are shown in the course. The signature for OnFinishedPlaying is void () so you would have

// within class definition in the header
UFUNCTION()
void FinishedAnimation(); 

// Adding 
GetSprite()->OnFinishedPlaying.AddDynamic(this, &ASonicWorldReimaginedCharacter::FinishedAnimarion);
// Removing
GetSprite()->OnFinishedPlaying.RemoveDynamic(this, &ASonicWorldReimaginedCharacter::FinishedAnimarion);

Yeah I remember that section sort of. Didn’t really get it at first but the comments helped me out. I’m hoping that I’m putting this together right.


Right now, I’m testing the add dynamic event using the UE_LOG. For some reason, even when activate my crouch function, the Text never appears. Maybe there is something I’m not getting…

And you made it a UFUNCTION()?


yes

How do you have this setup so I can test it on my end?

in the header file I have:

 // The animation to play while Crouching
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Animations)
class UPaperFlipbook* CrouchAnimation;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Animations)
class UPaperFlipbook* CrouchReturnAnimation;

//Handles Crouching
void Crouch();
void UnCrouch();

UFUNCTION()
void FinishedAnimation();

in the cpp file I have:

//Crouch (This Portion goes into the SetupPlayerInputComponent Function)
PlayerInputComponent->BindAction(“Crouch”, IE_Pressed, this, &ASonicWorldReimaginedCharacter::Crouch);
PlayerInputComponent->BindAction(“Crouch”, IE_Released, this, &ASonicWorldReimaginedCharacter::UnCrouch);

void ASonicWorldReimaginedCharacter::Crouch()
{
// Are we crouching?
GetCharacterMovement()->Crouch(true);
GetSprite()->SetFlipbook(CrouchAnimation);
GetSprite()->OnFinishedPlaying.AddDynamic(this, &ASonicWorldReimaginedCharacter::FinishedAnimation);
}

void ASonicWorldReimaginedCharacter::FinishedAnimation()
{
UE_LOG(LogTemp, Warning, TEXT(“FINISH”));
}

void ASonicWorldReimaginedCharacter::UnCrouch()
{
//This Section is a WIP
GetSprite()->OnFinishedPlaying.RemoveDynamic(this, &ASonicWorldReimaginedCharacter::FinishedAnimation);
//GetSprite()->SetFlipbook(CrouchReturnAnimation);
//GetCharacterMovement()->Crouch(false);

}

These would be the essentials to get started with it. I’ll note that I am using the paper character class so I have the built in idle/run animations as well

It appears to be working on my end

Have you tried rebuilding?

Yup. That’s like really weird… I tried experimenting some by putting the the onfinishedplaying code into my attack function. In game when I executed the function a couple of times, the game crashed for a sec and then the output log threw up these errors:
PIE: Server logged in
PIE: Play in editor total start time 0.042 seconds.
LogOutputDevice: Warning: Script Stack (0 frames):
LogStats: FPlatformStackWalk::StackWalkAndDump - 1.800 s
LogOutputDevice: Error: === Handled ensure: ===
LogOutputDevice: Error: Ensure condition failed: InvocationList[ CurFunctionIndex ] != InDelegate [File:C:\Program Files\Epic Games\UE_4.27\Engine\Source\Runtime\Core\Public\UObject/ScriptDelegates.h] [Line: 556]
LogOutputDevice: Error: Stack:
LogOutputDevice: Error: [Callstack] 0x00007ffe6833e519 UE4Editor-SonicWorldReimagined-4859.dll!<lambda_504b220f9d24112e82be46ffe98e211b>::operator()() [C:\Program Files\Epic Games\UE_4.27\Engine\Source\Runtime\Core\Public\UObject\ScriptDelegates.h:556]
LogOutputDevice: Error: [Callstack] 0x00007ffe68334dc1 UE4Editor-SonicWorldReimagined-4859.dll!TBaseDynamicMulticastDelegate<FWeakObjectPtr,void>::__Internal_AddDynamic() [C:\Program Files\Epic Games\UE_4.27\Engine\Source\Runtime\Core\Public\Delegates\DelegateSignatureImpl.inl:1110]
LogOutputDevice: Error: [Callstack] 0x00007ffe68337473 UE4Editor-SonicWorldReimagined-4859.dll!ASonicWorldReimaginedCharacter::Tick() [C:\Users\bluej\Downloads\Sonic World Project\SonicWorldReimagined\Source\SonicWorldReimagined\SonicWorldReimaginedCharacter.cpp:105]
LogOutputDevice: Error: [Callstack] 0x00007ffdf71c04da UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffdf718c06e UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffdf83a410e UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffdf83ad2e4 UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffe5a812398 UE4Editor-Core.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffe5a8127ce UE4Editor-Core.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffe5a8240bd UE4Editor-Core.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffdf83c93be UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffdf83cfaba UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffdf7ab349f UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffdf7abe2fc UE4Editor-Engine.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffe58f814c3 UE4Editor-UnrealEd.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffe59882ba6 UE4Editor-UnrealEd.dll!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ff6a07687a0 UE4Editor.exe!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ff6a0780fcc UE4Editor.exe!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ff6a07810ba UE4Editor.exe!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ff6a07840dd UE4Editor.exe!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ff6a0795984 UE4Editor.exe!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ff6a079853a UE4Editor.exe!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffebd7254e0 KERNEL32.DLL!UnknownFunction
LogOutputDevice: Error: [Callstack] 0x00007ffebde2485b ntdll.dll!UnknownFunction
LogStats: SubmitErrorReport - 0.000 s
LogStats: SendNewReport - 1.441 s
LogStats: FDebug::EnsureFailed - 3.250 s

Anyway Is there some way you could perhaps send me that test project? Maybe I can look inside to compare mine vs yours

I think you would need AddUniqueDynamic so it only gets added if it’s not already there.

1 Like

Does the sprite have to be not looping for it to work?
I tried to do this in a new paper2d starterproject but it’s also not working… This time I called the function on tick.



Calling it on tick would kind of go against the idea of events. You bind it once and it’ll call your function when it happens.

Ok so when I add the function to the jump release action, it works perfectly. Which tells me something with OnFinishedPlaying is failing. Can you tell me what OnFinishedPlaying is supposed to do by chance?

You are using OnFinishedPlaying in your code too right?

Privacy & Terms