Animate sprites to letters of a word?

How do I iterate over the letters of a text, then use those values to animate a sprite?

I am making a simple language game. nothing fancy. I have a word as a scene variable eg “maybe” and I also have a sprite with 27 images. 1 image for each letters of the alphabeta plus 1 blank image

then, I mulitple several of these sprites on screen, all invisible and width set from the original size to 0. This allows each spirite to hold a letter but be invisible when it is empty

I want gDev to look at the variable eg “maybe” and then iterate over the word to get each letter then, animate the sprite to their corresponding letter

IN SUMMARY:
if the word is “maybe”, gDev should:

  1. iterate to distribute the letters to the sprites like [M][A][Y][B][E]. Then set the width from 0 to its original size
  2. leave the other sprites without letters to stay blank and with width of 0

SCENE EXAMPLES WITH DIFFERENT WORDS:

MAYBE on screen will look like => [M][A][Y][B][E][][][][][]
REPORT on screen will look like => [R][E][P][O][R][T][][][][]
DREAM on screen will look like => [D][R][E][A][M][][][][][]

notice how the trailing [] are empty and width is 0

Hi iansama,

This can be achieved by using the repeat loop and the StrAt() / StrLength() expression. You’ll need to create a variable so you can store what the current character number is add then just use the StrAt() expression to change the animation based on that variable.

I made a quick example with the online editor but instead of changing existing objects on the screen I just created some new ones. I use a space press to trigger the below.

First I store / reset some variables such as the word I want to spell and what character you’re up to in the repeat loop.
image

Then I have a repeat loop that repeats based of the number of characters in the word.
image

I then create the object and set its animation based on the current character count within the loop. For example B would be 0 and G would be 3.

Finally I increase the character count variable by one so the next loop I get the next character.
image
(Not the letter_pos_x variables are just for spacing out the objects)

Once all together, the word BUNGUS is shown on screen.
image

If you’re not wanting to create the objects and instead replace what is already in the scene, you can use the same logic but instead give the objects on the scene an object variable (eg ID) and instead of creating an object just update the existing one.
Something like the below should work.

Hey BigChalkGames, thanks for the comprehensive reply. I am going to try it in a bit and then confirm if everything is working good.

I just wanted to ask where do you learn the expressions for gDev? is there a playground? I am new to gDev, since last night.

1 Like

All the current expressions can be found here: Expressions reference - GDevelop documentation

GDev also has a wide range of official and community made extensions that can expand the functionally of the base software (eg: wanting to use toFixed()) and if they don’t have want you need you’re able to use JavaScript within event blocks (you can use this to pass through objects / strings / numbers)

Extensions: Install new extensions - GDevelop documentation
JavaScript: JavaScript Code events - GDevelop documentation