[Solved] Use animation number in an action

Hi everyone,

Short question:

Is it posible to read the number of the animation from a sprite, transform it to a string and add it to the expresion Change to "Level "+ StringAnimationNumber?

SOLVED Quick Answer: (Thanks to @UlisesFreitas)

"Level " + ToString(Level_button.Animation())

My case explained:

I’ve been using gdevelop for only a few weeks and I don’t have previous programming experience. Until now, I’ve been able to move forward with the wiki, your posts and youtube videos but I feel stuck with this.

I’ve done a scene to choose levels that works well:

  • There are sprites (text) for every number and when you click on a number it goes to the indicated level.
    This way, if I had 100 levels, I should include 100 lines, so I’m trying to think a more eficient way to automatise and create only one condition that changes for whatever be the number of the level.

Mi first idea has been:
I’ve create a sprite called “Level_button” with different animations where the animation #1 is an image of a number1, #2 is a number2, etc

In the events I want to replace: Change to scene “Level 2” for something like

Change to scene "Level " + AnimationNumber.Level_button

I don’t know if this is even posible or how to write it properly.

My second idea has been:
Create an object variable and assign it a value for every instance that is the number of the image
In the events: Change to scene "Level " + Level_button.VariableString(Nivel)

This way it works, but I still feel that it could be more automatic than change manually every value of the instance variable if is the same that the animation number.

Additionally, with theses changes I lose the versatilty of the text objects because I can’t give them different “animations” (as an image sprite).

Any idea of how could implement this idea with a text sprite?

Thank you for your time!

Level_button.AnimationName() this the way to get the animation name of the Sprite Object

Thank you for your reply Ulises.

But If i’m understanding well
I think that in the end this wouldn’t optimise because I would have to write and call for the specific name of every animation, right?. So, it would be really the same(or even with extra steps) that writing a line directly Change to “Level 2”

Is there something like "Level_button.AnimationNumber "?

Thanks again!

Is what i do in this game [Game] A Pixel Adventure Legion 1.1.0 - HTML5 - Desktop - Android

Do you mean this line?

With variables I’ve been able to do it for a “next level button” but in this case I’m trying to use the animation number that gdevelop assigns automatically to every sprite animation.

It works the way I had it, but It’s not “clean enough” and requires include new lines for every new level added.

It’s more like a stubborn thing, because I think that it could be better organised

Thanks again, I’ll check your demo :wink:

Why new lines?
What I do in the game is change the gs_game_level based on the obj_end_level variable inst_next_level
So in each Scene Level_1, Level_2, … I have an Object Sprite of obj_end_level with the variable number, I’m sure there will be another way to do it with the logic you are proposing but, since you already has the button added why don’t just change the button variable number

I think we are not talking about the same thing…

the sprite “Level_button” has several animations.

For every animation I set an “Instance Variables” called “Nivel” (Level in spanish)

For Animation #1, I set Nivel to 1,
For Animation #2, I set Nivel to 2
For Animation #3, I set Nivel to 3

And then the event is: Change to scene "Level " + Level_button.VariableString(Nivel)

This works ok.

I’m just trying to figure out if there is a way even shorter (maybe not) where we can avoid create and assign values for the Instance Variable and use the Animation number# instead



@cion I speak spanish if you want to talk

Maybe you could try Level_button.Animation() I’m not sure if this way you get the animation frame, I read at this post

1 Like

@UlisesFreitas many thanks for your implication with my question

If I use Level_button.Animation() I have to incluide manually the number of the animation between the (), but the point would be that gdevelop obtains automatically the number of the animation that is set from the Instance (object) that you are clicking

I’m not sure if is this what you want to achieve but
The events

Note mouse cursor is not displaying but every time the animarion frame change is because i click on Level_button
This way yoou get the animation frame with no need of variable definitions

1 Like

IT WORKS. THANK YOU @UlisesFreitas

When you first told me about use Level_button.Animation(), I tried, but without including ToString, so it didn’t worked and I though it would be necessary to include an specific value between the ()

It works as expected when I’ve written:
Change to scene "Level " + ToString(Level_button.Animation())

Win Win, if you think this is solved please add [Solved] to the title this way your post could help others

I have been thinking a little bit more about this, just for fun.

If someone wants to do something similar but their scenes (levels) don’t have a name type “level 1” but something only with words. The solution also works with this

But in this case it’s not necessary add “ToString” because it’s already string


This would have worked too for my initial question if I would named every sprite animation like: 1, 2, etc

Or for example, if you name the sprite animation “Forest” and the scene “Forest”, it would be just:
Change to scene spritename.AnimationName()

Hope this could be useful for someone in the future :wink: