Set animation to last frame/first frame

I am using animation frames as different angles of objects.
The animation is not meant to be played, but to be clicked through by hand.
I have created buttons that will add/subtract frames, so that the player can ‘rotate’ the object both clockwise and counter-clockwise.

I’ve got this working smoothly when I know the exact amount of frames in the animation.

My issue is that I want to create this action for a group of objects, and some objects have more angles/frames than others. So I don’t have one fixed number for the last frame.

Instead of inserting a number (‘12’ in the screenshot below) as the current frame, I want to insert something like ‘final frame’.
Is there a command that can find the last frame of an animation?

P.S. I have played around with looping the animation in the animation settings, and the condition ‘animation is finished’. But I couldn’t get it working with those functions.
Is it maybe possible to link and control animation frames with variables/arrays?

Well, I think that you can get the number of frames of the current animation by using this expression:


In this screenshot I get each frame of my animation, over the total, in the debug console.

Use YOUR_OBJECT.Sprite() to get the current frame of the current animation.

1 Like

Thanks! That sounds like a good start.

I haven’t used the debug console before. Is it possible for the game to reference it?

I should say this is the first time I’m attempting to make a game (I have zero programming skills). So things get a little over my head as soon as it involves some way of writing code.

1 Like

Use the expression builder so that GDevelop writes complex expressions for you. Although it is quite useful to understand this syntax and it’s fairly easy because GDevelop auto-completes what you start typing and gives you documentation on what the function does.

For example, before reading your question, I didn’t even know about AnimationFrameCount() myself. To learn that, I just created a Sprite in an example project, created an event, and used this mechanic to list what functions were available for me on this type of object.

Allow me to give you a basic introduction

YOUR_OBJECT is called an “object” and each type of object has its unique methods. Sometimes, multiple types of object have similar methods and properties. A method, or a function, is an effective way to retrieve data, or calculate something, based on the unique properties of the object. You “call” (execute) a method when it ends with parenthesis “()”. In these parenthesis, it is sometimes required to give parameters to customise the output. For instance, you can calculate the distance between two objects using Object1.Distance(Object2). Use the expression builder or type it in using the auto-completion and the built-in documentation yourself.

Also, I didn’t quite understand what you meant by this:

If you don’t know what the debug console is, well, basically it’s the best way for you to make your program communicate with you as it is being executed. Sometimes, if you struggle on something, and don’t really understand the output of an expression for example, then you can “log a message” on the console to see what exactly is happening in your events sheet at a precise moment. The debug tool is the best ally of a developer, even a no-code developer. I strongly recommend to try and use it as often as possible when facing an issue.

However, to open the debugger, you’ll have to open the preview in “debug mode”:

It opens a new tab at the top of GDevelop:

Use the “pause” button at the top-right of the screen to see details about each instance of object you have placed in the scene, and open the console, at the bottom, to see the messages your events sheet return.

I hope this fast tutorial is useful to you.

1 Like

Thanks so much for all this information! It really helps me. I’ve been confused about the information in parenthesis before. I’ve copied expressions that worked like that, and got it working, but I didn’t understand why it worked. But now I do, more at least. So thank you!

I’ll definitely look up more about the debugger so I can use it in the future.

I asked this because I thought I’d first need to have the framecount be sent to the log, and then to reference that number from the log in an action. But now I released I can use AnimationFrameCount() instead of a number in the ‘current frame’ action.

It only works when I use AnimationFrameCount()-1 though. If I don’t do the minus one, the loop doesn’t work somehow. But I’ve read similar things from people saying that if an animation loops the last frame is skipped. Maybe this is related. For now I’ll just work around this by duplicating the last frame. Maybe later I’ll be able to understand the issue and actually find a fix for it :sweat_smile:

You have to subtract 1 because the first frame of an animation is not 1, it’s 0. Indeed, the first frame is frame number 0. However, because the AnimationFrameCount() method gives the number of frames, it will be greater than the last frame’s number by a difference of 1.

For example, if your animation has 50 frames, then the last frame will be the frame number 49.

If you want to know why there is this difference, it is because the frames are stored as a list. Your Sprite object has a list of frames for each animation. A list is called an “array”, and in pretty much all programming languages, the first element of an array is the element number 0.

Try using the debug console when you don’t know about a strange-looking behaviour. For example, you could have seen it if you had printed to the console My_Object.Sprite() (you’d have seen it started at 0):

Sometimes, it is how I figure it out myself

Note that in my screenshot I use the function named “ToString()” which takes as parameter (or “argument”) a number. This function allows me to concatenate (merge) strings together to form a single message.

I’m glad I could help :wink: