Specify name of sound file to play using expression

As a developer creating games for language learning, I want to be able to specify the sound file to be played using an expression, rather than having to pre-select a specific file.

I’m not sure, but is this functionality covered in this card on the Trello roadmap?

Use case 1: Language learning game
My language game supports learning 3 languages. The user can choose which language they want to learn. When the player clicks on an image, it plays the audio for that image in the language the user is learning.
Code: Play the sound [ObjectName + LanguageChoice].wav
Result: Play the sound BeerFrench.wav (or BeerSpanish.wav, BeerEnglish.wav)

Use case 2: Multiple versions of RPG dialog
My RPG game includes dialog in four languages. The user can choose which language they want to play in. When a dialog string plays, it plays in the selected language.
Code: Play the sound [DialogString + Language].wav

Use case 3: Unique environmental sounds
My main character has different types of shields. When each shield collides with an object, it makes a specific sound.
Code: When shield collides with object, Play sound [Collision + ShieldType].wav

Benefits

Benefit 1: I only need one event in the code to play the sound. Without this function, I need a different event for each language option:

  • If language is English, play BeerEnglish.wav
  • If language is French, play BeerFrench.wav
    etc.

Benefit 2: I can add a new language to the game just by adding the language-specific audio resources, and defining the [LanguageChoice] variable.

2 Likes

This should be great to have indeed!


If you need this fast you can use this solution with JSevent:

gdjs.evtTools.sound.playSound(runtimeScene, “https://resources.gdevelop-app.com/examples/platformer/jump.wav”, true, 100, 1);

Documentation:

gdjs.evtTools.sound.playSound( runtimeScene, soundFile, loop, volume, pitch);

if you wrap this JS event in a custion function you can use it like an basic action :slight_smile:

soundFile argument is the name of the ressource in the ressource tab.


For devs

The documentation about gdjs.evtTools.sound  is non existing.

File in the codebase.

Thanks @Bouh for the explanation! It’s great to know that I can do this now using Javascript. I haven’t figured out how to do that yet, but I’ll add that to my learning path on GDevelop and will study your examples. I so appreciate the active moderation and support in this community!

1 Like