Ask here your questions to get help using GDevelop.
User avatar
By Lizard-13
#63952 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:
Code: Select allMaterials
        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 :)
User avatar
By Lizard-13
#63969 It should look like this:
RandomSound.png
RandomSound.png (19.48 KiB) Viewed 184 times

Note that the JavaScript event is a sub-event, the code inside is:
Code: Select all// 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);

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.

:!: 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 ;)
User avatar
By Lizard-13
#63979 Then you have to use C++, try this code:
RandomSound.png
RandomSound.png (19.17 KiB) Viewed 151 times


For the C++ event, this is the includes list:
Code: Select all<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: Select all// 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);
By tool
#64004 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