Can't override AnimationPlayer 'play' function

Godot Version

4.5

Question

I’m trying to create an animation player that will play the animation according to a beat. Therefore I make an instance of AnimationPlayer and override the play function. but for some reason I can’t make it. when i call the play function it always uses the original. this is the node:

class_name RhythmAnimationPlayer
extends AnimationPlayer

@export var round_start_beat: bool = false
@export var conductor: RhythmConductor

var start_beat: float = 0.0
var speed_factor: float = 1.0
var active_animation: StringName = &""
var play_direction: float = 1.0

func _ready() -> void:
    if not is_instance_valid(conductor):
        push_warning("No conductor provided")
        self.active = false

func _process(delta: float) -> void:
    if not is_playing():
        return
    var beat_offset := (conductor.current_beat - start_beat) * play_direction
    var seconds := beat_offset * speed_factor
    seek(seconds, true, false)

func play(
    name: StringName = &"",
    custom_blend: float = -1.0,
    custom_speed: float = 1.0,
    from_end: bool = false
) -> void:
    if not is_instance_valid(conductor):
        push_error("No conductor provided")
        return
    
    speed_factor = custom_speed
    if round_start_beat:
        start_beat = roundf(conductor.current_beat)
    else:
        start_beat = conductor.current_beat
    active_animation = name
    
    self.play_direction = -1.0 if from_end else 1.0
    super.play(name, custom_blend, 1.0, from_end)

And this is how I call the play function:

extends CanvasLayer

@onready var rhythm_animation_player: RhythmAnimationPlayer = $RhythmAnimationPlayer

func _ready() -> void:
    rhythm_animation_player.play("new_animation", -1.0, 1.0, true)

Also,
when i change the func name (for example ‘play_synced’) and call it, everything works perfectly. So it means the override fails for some reason.

You can’t override built-in functions that aren’t specifically virtual. Depending on your project settings, this warning/error can appear if you try to:

The method "play()" overrides a method from native class "AnimationPlayer".
This won't be called by the engine and may not work as expected.

What I would do is create a separate function, like play_synced as you suggested, and always call that from other nodes.

1 Like