This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Simple helper script for AnimationPlayers - proper play, reverse, pause, resume

RossRoss Posts: 199Member
edited August 2016 in Resources
Since the built-in functions of AnimationPlayer nodes are so limited, I've been using this little script as an add-on whenever I need to do anything serious with them. I thought it might be useful to others. It lets you actually pause animations, play or reverse them from their current position, or just resume play in the last direction—each with a single function call.

I didn't hook up blending times since I don't use them and it seemed like a wasted argument, but that would be totally easy to add in if you felt like it. And I put an 'r' on each function name to differentiate them from the default ones, you'll probably want to change that to something that makes sense to you.

Here's the code, or just grab the attachment:
extends AnimationPlayer

# Implements reasonable controls for animations. Put on AnimationPlayer Nodes.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var dir = 1.0
var t = 0.0
var anim

func _process(dt):
t = get_pos()

func rplay(animation):
dir = 1.0
play(animation, -1, dir)
seek(t)
anim = animation

func rreverse(animation):
dir = -1.0
play(animation, -1, dir)
seek(t)
anim = animation

func rplay_from_start(animation):
dir = 1.0
play(animation, -1, dir)
seek(0.0)
anim = animation

func rreverse_from_end(animation):
dir = -1.0
play(animation, -1, dir)
seek(get_current_animation_length())
anim = animation

func rresume():
play(anim, -1, dir)
seek(t)

# stop() works fine as pause.

func rstop(to_start): # stops and resets time to one end or the other.
stop()
if to_start:
t = 0
seek(t, true)
else:
t = get_current_animation_length()
seek(t, true)

func rseek(animation, time, update): 
set_current_animation(animation)
anim = animation
t = time
seek(t, update)




Tags :

Comments

  • WombatTurkeyWombatTurkey Posts: 14Member
    Thanks, could definitely be useful!
  • saguarosaguaro Posts: 8Member
    Thank you!
  • MegalomaniakMegalomaniak Posts: 2,580Admin
    Nice, that is very useful, thanks!
  • AkienAkien Posts: 70Member Godot Leader
    Thanks for sharing, that will be a nice addition to the asset sharing platform in 2.1 ;)<br /><br />If many devs start using it and think it really improves the workflow, maybe some of those methods could be upstreamed in the C++ directly.
Sign In or Register to comment.