Hello. I am new to non-visual scripting (been using Stencyl so far), so I have a bit of a hard time, because many things can go wrong.

I've been trying (unsuccessfully) to call the AnimationPlayer node so that I can change the play speed via code.

The AnimationPlayer is a child-node of my player scene. I am trying to double the animation speed while my character is sprinting, instead of making a new animation.

I tried many things, but non make any difference in my animation playback speed. I tried using call node. I tried the $ symbol like that: $AnimationPlayer.playback_speed = 2

I placed it in a simple if/else function that detects if the sprint button is pressed (the sprint works fine, only the animation is the problem). I even tried doing an onready var animation_speed = $AnimationPlayer.playback_speed at the beginning of the code and then call the animation_speed variable instead.

Not sure what I should do to make this work.

Thanks in advance

Welcome to the forums @DrFelis!

If the AnimationPlayer node is a child of the player node, and the player node is where the script is attached to, then the following code should set the animation speed (untested, but I am almost positive it should work):

# I'm going to assume is_sprinting is a boolean that is true when the player is
# sprinting and false when the player is not.
if is_sprinting:
	# One way: use "$" to get the AnimationPlayer node
	$AnimationPlayer.playback_speed = 2.0
else:
	# Another way: use "get_node" ("$" is shorthand for get_node BTW)
	get_node("AnimationPlayer").playback_speed = 1.0

And then it should change the speed. You don't need to use onready to set the speed, unless you want to set the animation speed to a certain number when the object is initialized.

The AnimationPlayer is a child of the player node, and the script is atttached to the player node indeed. The code you posted is similar to how I did it:

I temporarily used big values to really notice if there's any difference. The function is a _physics_process(delta).

Still, the animation speed doesn't change while I am sprinting.

By the way, I am also using an AnimationTree that is a child-node of my player scene. Could it possibly interfere?

The AnimationTree is probably causing the issue then, as the documentation notes:

Note: When linked with an AnimationPlayer, several properties and methods of the corresponding AnimationPlayer will not function as expected. Playback and transitions should be handled using only the AnimationTree and its constituent AnimationNode(s). The AnimationPlayer node should be used solely for adding, deleting, and editing animations.

Edit: you might be able to change the speed through the AnimationTree though.

Thanks a lot for the help! I guess I will have to create a separate animation for sprinting after all, since my AnimationTree is interfering.

You should be able to duplicate your animation in the animation player then scale down the animation loop to make it effectively faster, no?

Yeah, that's what I am going to do in the end. I just though that the playback_speed would be a cleaner solution as it would just be a couple of lines in code, instead of having four new animations. But I am glad that I asked here. It was educational as I know now that the problem wasn't in my code.

2 years later