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.

4 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.

1 Like

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

Up!
Having a lot of SFX for the same action and not being able to pick them on random without making one event for each sound is annoying, and I can’t get Bouh’s solution to work properly.

While it’s still under review, I do have an extension that’s going through review that makes this easier: New extension: Audio by Filename by github-actions[bot] · Pull Request #1030 · GDevelopApp/GDevelop-extensions · GitHub

You can scroll to the bottom to download the example project and extension to see if it’ll help you out.

Edit: Make sure you review the comments in the example project, there are some pretty specific steps you need to take to avoid delays, but after doing that it should let you use whatever expressions you want (so long as your filenames are named accordingly)

2 Likes

Although I agree. But, this should work for file stuff in general (such as 3D cube faces)