After collision i wanna to play random sound from my choice.

I have couple of sound clips i wanna play after collision how to do that? How to randomize that?

Check this: [url]How can I play sound files programmatically? [Solved]]
I can help you to get the random filename if you don’t know how :slight_smile:

how to do it on my example?

What a name for a sound filename!
How the possible random sounds are called?

For example, if the names are like: material_wood_1.ogg, material_wood_2.ogg, material_wood_3.ogg, etc.
You can do (pseudo-something): “material_wood_” + random(1, 3).toString() + “.ogg”

But if the possible names are like: wood.ogg, metal.ogg, plastic.ogg, etc. (there is no relation between them).
*You could rename the files to use the easy way (above).
*Or you can use a structure variable, that maps a number with a filename:

Materials 0 "wood" 1 "metal" 2 "plastic"
Then you can get a random material name: scene.Variables().get(“Materials”).get(random(0,2).toString()).getAsString() + “.ogg”

Let me know how you are naming the files and I will be glad to help you :slight_smile:

stenjanje1
stenjanje2
stenjanje3
stenjanje4
stenjanje5

do i need to change red row with variable?

It should look like this:


Note that the JavaScript event is a sub-event, the code inside is:

[code]// random number between 1 and 5
var random_1_5 = Math.floor(Math.random()*5) + 1;

// get the sound manager
var sound_manager = runtimeScene.getGame().getSoundManager();

// play random sound, channel = 0, loop = false, volume = 100, pitch = 1
sound_manager.playSoundOnChannel("stenjanje " + random_1_5.toString() + “.ogg”, 0, false, 100, 1);[/code]
It’s pretty self-explanatory, I’ve assumed that the filenames are “.ogg”, if they are “.wav” you have to change the “.ogg” part in the last line of the code.

:exclamation: Remember to add the sound files in the resources manager, so they are exported to the project compilation directory, if the names are dynamic GD can’t do it for you :wink:

Java is not supported in native :frowning:

Then you have to use C++, try this code:

For the C++ event, this is the includes list:

<iostream> <stdlib.h> "GDCpp/Runtime/String.h" "GDCpp/Runtime/CommonTools.h" "GDCpp/Runtime/RuntimeGame.h" "GDCpp/Runtime/SoundManager.h"
And this is the code:

[code]// Random number between 1 and 5
int random_1_5 = rand()%5 + 1;

// Get the sound manager
SoundManager* sound_manager = &(scene.game->GetSoundManager());

// Play random sound, channel = 0, loop = false, volume = 100, pitch = 1
sound_manager->PlaySoundOnChannel(“stenjanje” + gd::String::From(random_1_5) + “.ogg”, 1, false, 100, 1);[/code]

Thanks man you saved me :wink:
Karma +

when i’m trying to use another bunch of sound files on another scene with names “misaubija” 1-5 it reports this error:

Linking of project failed: Please go on our website to report this error, joining this file:
C:\Users\tool\AppData\Local\Temp/GDTemporaries/LatestCompilationOutput.txt

If you think the error is related to an extension, please contact its developer.


C:\Users\tool\AppData\Local\Temp/GDTemporaries/GD0x1709e270RuntimeObjectFile.o:GD0x174c3d10SourceFile.cpp:(.text+0x0): multiple definition of `GDCppCode0x174c2dc0(RuntimeScene&)’

C:\Users\tool\AppData\Local\Temp/GDTemporaries/GD0x1709e270RuntimeObjectFile.o:GD0x174c3d10SourceFile.cpp:(.text+0x0): first defined here

C:\Users\tool\AppData\Local\Temp/GDTemporaries/GD0x1709e270RuntimeObjectFile.o:GD0x174c3d10SourceFile.cpp:(.text+0x0): multiple definition of `GDCppCode0x174c2dc0(RuntimeScene&)’

C:\Users\tool\AppData\Local\Temp/GDTemporaries/GD0x1709e270RuntimeObjectFile.o:GD0x174c3d10SourceFile.cpp:(.text+0x0): first defined here

C:/Program Files (x86)/GDevelop/CppPlatform/MinGW32/bin/ld.exe: C:\Users\tool\AppData\Local\Temp/GDTemporaries/GD0x1709e270RuntimeObjectFile.o: bad reloc address 0x12 in section `.text$ZN2gd6String4FromIiEES0_T[_ZN2gd6String4FromIiEES0_T]’

collect2.exe: error: ld returned 1 exit status