Using the frame number of a sprite animation in a script

Godot 4.4.1

Situation

Hi, I currently have my character set up with 3 states (1-3 ) and 8 directions (1-8). Every animation has 8 frames, so its 3x a 8x8spritesheet. Every sheet has an identical structure and frame number.

And I have a code segment that translates the variables
vAnimState (integer, 1-3) to vStateName (string) and
vAnimDirection (integer, 1-8) to vDirectionName (string)
to define the animation that shall be played.

Currently I have this code here set up to determine which animation to use:
grafik
*(So in the end it plays for example the animation “animLin_walk_upleft”)

Movement and Animation works so far, but everytime I change the direction, the new animation starts at its first frame. Thanks to the isometric view and only 8 directions and frames its barely noticeable, but therefore it looks a bit like lagging, when the animation is reset at every direction change.

Question

Is there a way to get the number of the current frame thats shown when the direction changes - and to use a frame number to define at which frame the other animation shall resume?

(So I could build a math equation that uses the value of the current animation + current frame nr and the value of the new animation, to define at which frame the new animation should start)

If your ‘vAnimPlayer’ is an AnimationPlayer node, you can save the ‘current_animation_position’ and use the advance() method after the play:

var currPos = vAnimPlayer.current_animation_position
vAnimPlayer.play("animLin_" + vStateName + "_" + vDirectionName)
vAnimPlayer.advance(currPos)

If it doesn’t work, maybe use the method seek(currPos, true), because it’ll trigger an update.
It’ll work if all animations have the same length (in seconds).

1 Like

Sounds good.
I’ll test it and report back :slight_smile:

Took me a while to figure everything out properly, but it works like a charm :slight_smile:

Thank you!

1 Like