How to create a random sounds?

Is it possible to create a random of sounds? I want the video game to play a random sounds and each sound is related to an object. For example, if the sound says the name of an animal, that animal should appear on the screen.

Gdevelop can give you a random number.
"RandomInRange(1,4) will give you 1, 2, 3, or 4.

So you can have a variable animal with n children, like that:
animal.1=dog
animal.2=horse
and so on.

So if you set your variable “currentanimal” to:
VariableString(animal[ToString(RandomInRange(1,Variable(numberofanimals)))])
You’ll get an random animal name.

To get a specific object from this name, you have to put all your objects in the same object group, let’s call it “animals”. Each object’s name is the animal’s name.
Then you put the special event For each instance of an object.
So for each instance or group animals, you check if the object name string is equal to your currentanimal string. If yes, show it. The other ones, hide them.

For the sound, I don’t think you can dynamically access a specific sound file with your “currentanimal” string. I’m not sure, someone else can confirm. It would mean that you’ll have to write a lot of events like that:
If currentanimal=dog, then play dog.mp3
If currentanimal=horse, then play horse.mp3, and so on.

1 Like

Almost :

  • it should be VariableString(animal[RandomInRange(1,Variable(numberofanimals)))])
  • indexes are zero based (first element index is always 0), so use Random instead of RandomInRange

image


That requires all the animals to be placed in the scene and hidden. If only one animal is displayed on screen, then it’s simpler to delete it and just create the randomly selected one :


That’s correct.

2 Likes

Could you give me a little more detail please?

Sorry @Jessi, could be a bit more specific? Is that request directed and @Bluzima or me, and what part?

How do I make it so that the object that has already left the random is not repeated? I only want each object to output once. Help me please @MrMen