0 votes

Want I'm looking to do is set a variable to the the number of times an animated sprite has looped and then use that as a signal to do something. For example, every time the animation loops, it adds one to a variable, and then when the variable reaches 10, it calls a function to do something.

Anyone know an easy line of code to do this?


in Engine by (18 points)

1 Answer

+1 vote

I can think of a few ways that would probably work...

If the sprite is being animated via an AnimationPlayer, you can add a call method track at the end of the animation timeline. That'll call a function you define every time the animation reaches the end.

In that function, you could manage your counter and/or decide to stop the repeating animation and call your mentioned "done" function.

Alternatively, you could set the animation to not loop (so, to only play once). Then, you could hook up the animation's animation_finished signal. In the code called by that signal, you could manage your "counter" variable. If you haven't yet reached the maximum number of plays, just play the animation again. Otherwise, call your "done" function.

by (10,866 points)
edited by

That's just what I was looking for. Thanks so much.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.