In my game, I want to play a random sound (from a collection of about 15-20 files I have) each time a “coin” is collected.
What ways can I achieve this?
In my game, I want to play a random sound (from a collection of about 15-20 files I have) each time a “coin” is collected.
What ways can I achieve this?
It’s easy, you can use a variable and random(20)
Thanks taliour! This was going to be my approach. Do you have more details about how exactly to do this?
E.g. Can a variable be an array that contains the relative paths to each file? Or is it best to add each sound as an object in the scene, and then create an array of object names?
The problem is that the sound file parameter doesn’t work with strings, it’s another thing… not dynamic
What you can do is play the sound with a simple JS code I wrote some time ago, here:
[url]How can I play sound files programmatically? [Solved] - #9 by Lizard-13]
var sound_manager = runtimeScene.getGame().getSoundManager(); // get the sound manager
sound_manager.playSoundOnChannel("your sound dynamic filename string", 0, false, 100, 1); // play dynamic file sound, channel = 0, loop = false, volume = 100, pitch = 1
Put this code instead the action to play the sound, as a sub-event. The problem now is that you have to know JS to get the variable info from the scene:
var filename = runtimeScene.getVariables().get("SoundFilesStructure").getChild("variable name that contains a filename string")
Then you can use this value, read from a the scene variable array called SoundFilesStructure, and put it in the code to play a sound:
sound_manager.playSoundOnChannel(filename, 0, false, 100, 1); // play filename sound, channel = 0, loop = false, volume = 100, pitch = 1
The idea of GD is no-programming, I know, but it’s a missing feature, so a give a workaround
PS: In the link there is a C++ implementation too.
You dont need code, just create an variable and make it like this. When coliding coin, variable = random(20)
When variable equals (4 for example) = play this sound