#Any way to get animation length outside of signals?

12 messages · Page 1 of 1 (latest)

last yarrow
#

I have a animated sprite 2d and I'm trying to pass the length of an animation into a var. I cannot use await, because it's an FSM and the state can leave but the await continues and I don't want that. So actually if you know how to kill current awaits that could work too!

But yea how to do this:
var anim_length = animated_sprite_2d.animation.length (something like this)

hasty tundra
#

Depending on what you need to do, couldn't you emit a signal once the animation is finished? Or is it necessary that you cam get the animation length? (A maybe unrefined method for the latter is to manually enter the length of the animation since you know how long it is)

last yarrow
# hasty tundra Depending on what you need to do, couldn't you emit a signal once the animation ...

yea i really needed it no signals, i found a solution actually:

func get_current_animation_length(animated_sprite: AnimatedSprite2D = animated_sprite_2d) -> float:
    var current_animation_name = animated_sprite.animation
    if current_animation_name == "":
        print("No animation is currently playing.")
        return -1.0

    var sprite_frames = animated_sprite.sprite_frames
    if sprite_frames and sprite_frames.has_animation(current_animation_name):
        var num_frames = sprite_frames.get_frame_count(current_animation_name)
        var anim_speed = animated_sprite.speed_scale * sprite_frames.get_animation_speed(current_animation_name)
        if anim_speed > 0:
            return num_frames / anim_speed
        else:
            print("Animation speed is zero or negative.")
            return -1.0
    else:
        print("Animation not found: ", current_animation_name)
        return -1.0```
hasty tundra
#

oh I see, nice job finding something! If you don't mind me asking, what are you trying to do if you can't use signals?

last yarrow
#

its a combo attack fsm using the same state to determine the next combos. i was using await anim_finished (which is amazing cause it dont need signal connection) but as i leave and reenter the await will continue and i cannot find a way to break awaits. so next best thing was this to put inside a timer and then just count from there, i have a ton of animations so i didnt want finished going off accidently entering the state (which will end it right away)

#

^ thats my logic behind it anyway 😅

hasty tundra
#

I see, thanks for telling me, sounds interesting!

wicked rain
last yarrow
wicked rain
#

When you create a scene and instantiate it like this, you can access node's variables, it's nodes too. If you dynamically create this node, it has unique variables etc. you probably could try to access it using object in same tree, doing some access/branch stuff.

#

scene is _this_node passed as argument, btw gotta change name after refactor

wicked rain