Class AnimationPlayer
- Namespace
- Godot
- Assembly
- GodotSharp.dll
An animation player is used for general-purpose playback of animations. It contains a dictionary of AnimationLibrary resources and custom blend times between animation transitions.
Some methods and properties use a single key to reference an animation directly. These keys are formatted as the key for the library, followed by a forward slash, then the key for the animation within the library, for example "movement/run"
. If the library's key is an empty string (known as the default library), the forward slash is omitted, being the same key used by the library.
AnimationPlayer is better-suited than Tween for more complex animations, for example ones with non-trivial timings. It can also be used over Tween if the animation track editor is more convenient than doing it in code.
Updating the target properties of animations occurs at the process frame.
public class AnimationPlayer : AnimationMixer, IDisposable
- Inheritance
-
AnimationPlayer
- Implements
- Inherited Members
Constructors
AnimationPlayer()
public AnimationPlayer()
Properties
AssignedAnimation
If playing, the current animation's key, otherwise, the animation last played. When set, this changes the animation, but will not play it unless already playing. See also CurrentAnimation.
public string AssignedAnimation { get; set; }
Property Value
Autoplay
The key of the animation to play when the scene loads.
public string Autoplay { get; set; }
Property Value
CurrentAnimation
The key of the currently playing animation. If no animation is playing, the property's value is an empty string. Changing this value does not restart the animation. See Play(StringName, double, float, bool) for more information on playing animations.
Note: While this property appears in the Inspector, it's not meant to be edited, and it's not saved in the scene. This property is mainly used to get the currently playing animation, and internally for animation playback tracks. For more information, see Animation.
public string CurrentAnimation { get; set; }
Property Value
CurrentAnimationLength
The length (in seconds) of the currently playing animation.
public double CurrentAnimationLength { get; }
Property Value
CurrentAnimationPosition
The position (in seconds) of the currently playing animation.
public double CurrentAnimationPosition { get; }
Property Value
MovieQuitOnFinish
If true
and the engine is running in Movie Maker mode (see MovieWriter), exits the engine with Quit(int) as soon as an animation is done playing in this AnimationPlayer. A message is printed when the engine quits for this reason.
Note: This obeys the same logic as the AnimationFinished signal, so it will not quit the engine if the animation is set to be looping.
public bool MovieQuitOnFinish { get; set; }
Property Value
PlaybackDefaultBlendTime
The default time in which to blend animations. Ranges from 0 to 4096 with 0.01 precision.
public double PlaybackDefaultBlendTime { get; set; }
Property Value
SpeedScale
The speed scaling ratio. For example, if this value is 1
, then the animation plays at normal speed. If it's 0.5
, then it plays at half speed. If it's 2
, then it plays at double speed.
If set to a negative value, the animation is played in reverse. If set to 0
, the animation will not advance.
public float SpeedScale { get; set; }
Property Value
Methods
AnimationGetNext(StringName)
Returns the key of the animation which is queued to play after the animationFrom
animation.
public StringName AnimationGetNext(StringName animationFrom)
Parameters
animationFrom
StringName
Returns
AnimationSetNext(StringName, StringName)
Triggers the animationTo
animation when the animationFrom
animation completes.
public void AnimationSetNext(StringName animationFrom, StringName animationTo)
Parameters
animationFrom
StringNameanimationTo
StringName
ClearQueue()
Clears all queued, unplayed animations.
public void ClearQueue()
GetBlendTime(StringName, StringName)
Returns the blend time (in seconds) between two animations, referenced by their keys.
public double GetBlendTime(StringName animationFrom, StringName animationTo)
Parameters
animationFrom
StringNameanimationTo
StringName
Returns
GetMethodCallMode()
For backward compatibility. See AnimationMixer.AnimationCallbackModeMethod.
[Obsolete("This method is deprecated.")]
public AnimationPlayer.AnimationMethodCallMode GetMethodCallMode()
Returns
GetPlayingSpeed()
Returns the actual playing speed of current animation or 0
if not playing. This speed is the SpeedScale property multiplied by custom_speed
argument specified when calling the Play(StringName, double, float, bool) method.
Returns a negative value if the current animation is playing backwards.
public float GetPlayingSpeed()
Returns
GetProcessCallback()
For backward compatibility. See AnimationMixer.AnimationCallbackModeProcess.
[Obsolete("This method is deprecated.")]
public AnimationPlayer.AnimationProcessCallback GetProcessCallback()
Returns
GetQueue()
Returns a list of the animation keys that are currently queued to play.
public string[] GetQueue()
Returns
- string[]
GetRoot()
For backward compatibility. See RootNode.
[Obsolete("This method is deprecated.")]
public NodePath GetRoot()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsPlaying()
Returns true
if an animation is currently playing (even if SpeedScale and/or custom_speed
are 0
).
public bool IsPlaying()
Returns
Pause()
Pauses the currently playing animation. The CurrentAnimationPosition will be kept and calling Play(StringName, double, float, bool) or PlayBackwards(StringName, double) without arguments or with the same animation name as AssignedAnimation will resume the animation.
See also Stop(bool).
public void Pause()
Play(StringName, double, float, bool)
Plays the animation with key name
. Custom blend times and speed can be set.
The fromEnd
option only affects when switching to a new animation track, or if the same track but at the start or end. It does not affect resuming playback that was paused in the middle of an animation. If customSpeed
is negative and fromEnd
is true
, the animation will play backwards (which is equivalent to calling PlayBackwards(StringName, double)).
The AnimationPlayer keeps track of its current or last played animation with AssignedAnimation. If this method is called with that same animation name
, or with no name
parameter, the assigned animation will resume playing if it was paused.
Note: The animation will be updated the next time the AnimationPlayer is processed. If other variables are updated at the same time this is called, they may be updated too early. To perform the update immediately, call advance(0)
.
public void Play(StringName name = null, double customBlend = -1, float customSpeed = 1, bool fromEnd = false)
Parameters
name
StringNamecustomBlend
doublecustomSpeed
floatfromEnd
bool
PlayBackwards(StringName, double)
Plays the animation with key name
in reverse.
This method is a shorthand for Play(StringName, double, float, bool) with custom_speed = -1.0
and from_end = true
, so see its description for more information.
public void PlayBackwards(StringName name = null, double customBlend = -1)
Parameters
name
StringNamecustomBlend
double
Queue(StringName)
Queues an animation for playback once the current one is done.
Note: If a looped animation is currently playing, the queued animation will never play unless the looped animation is stopped somehow.
public void Queue(StringName name)
Parameters
name
StringName
Seek(double, bool, bool)
Seeks the animation to the seconds
point in time (in seconds). If update
is true
, the animation updates too, otherwise it updates at process time. Events between the current frame and seconds
are skipped.
If updateOnly
is true, the method / audio / animation playback tracks will not be processed.
Note: Seeking to the end of the animation doesn't emit AnimationFinished. If you want to skip animation and emit the signal, use Advance(double).
public void Seek(double seconds, bool update = false, bool updateOnly = false)
Parameters
SetBlendTime(StringName, StringName, double)
Specifies a blend time (in seconds) between two animations, referenced by their keys.
public void SetBlendTime(StringName animationFrom, StringName animationTo, double sec)
Parameters
animationFrom
StringNameanimationTo
StringNamesec
double
SetMethodCallMode(AnimationMethodCallMode)
For backward compatibility. See AnimationMixer.AnimationCallbackModeMethod.
[Obsolete("This method is deprecated.")]
public void SetMethodCallMode(AnimationPlayer.AnimationMethodCallMode mode)
Parameters
SetProcessCallback(AnimationProcessCallback)
For backward compatibility. See AnimationMixer.AnimationCallbackModeProcess.
[Obsolete("This method is deprecated.")]
public void SetProcessCallback(AnimationPlayer.AnimationProcessCallback mode)
Parameters
SetRoot(NodePath)
For backward compatibility. See RootNode.
[Obsolete("This method is deprecated.")]
public void SetRoot(NodePath path)
Parameters
path
NodePath
Stop(bool)
Stops the currently playing animation. The animation position is reset to 0
and the custom_speed
is reset to 1.0
. See also Pause().
If keepState
is true
, the animation state is not updated visually.
Note: The method / audio / animation playback tracks will not be processed by this method.
public void Stop(bool keepState = false)
Parameters
keepState
bool
Events
AnimationChanged
Emitted when a queued animation plays after the previous animation finished. See also Queue(StringName).
Note: The signal is not emitted when the animation is changed via Play(StringName, double, float, bool) or by an AnimationTree.
public event AnimationPlayer.AnimationChangedEventHandler AnimationChanged
Event Type
CurrentAnimationChanged
Emitted when CurrentAnimation changes.
public event AnimationPlayer.CurrentAnimationChangedEventHandler CurrentAnimationChanged