How would I set up a randomised sound effect array/list

I’m not much of a programmer so I hope I got the terminology right.

I’m looking to have a character make a sound chosen from a selection (of three in this case) when they take damage.
Ideally the sound would be chosen at random, although I’d be interested to know how to do it in sequence too.

Hi wOOtist and welcome. There’s an extension called Choose a random value. I haven’t used it. You can also use RandomInRange() in an expression to choose from your values. If you have a play with those things and have more questions then lots of people here will be able to help you.

1 Like

Thanks for the response!
I had a look at the extension, it should come in handy.
A lot of the difficulty I’m facing with this might have a really simple solution; I can’t figure out how to apply a value to any sounds.
Is there a way to do that that I’m missing, or a way to bring them in as their own assets like I can with objects?

Do you mean how do you actually load in a sound? Yes, as far as I know, they are not considered assets like images and text so you need to call them in an event. And when you’re setting up the event it will ask you to locate the sound file.




I’ll think about it later if no one else comes along, but you might need to have variables representing each sound in an array. And then choose randomly from the array. You can set up an array by pressing ‘v’ while in your scene editor screen to access the scene variables.

And here is some info about sounds:
https://wiki.gdevelop.io/gdevelop5/all-features/audio#playing_a_sound_or_a_music_file

1 Like

I’ve been loading them in like that as default sounds for every action (shot, impact, death, etc). I’m just hoping to put a few alts in for the sake of variety in a way that’s similar to switching between frames for a sprite animation. It’s not vital but it wouldn’t hurt to know how to do it.

Messing around with variables seems a bit complex at the moment; I’m still very new to this so the only way I’ve used them is for a very basic HP system. Any extra insight would be appreciated though, I can always come back to it when I have a better idea of what I’m doing!

I’ve read through that wiki page a few times, honestly between it and some other forum/Reddit posts I think my best bet will be working with channels, I was just hoping there was a simpler solution that I was missing.

Either way, thanks for the help so far!

You can’t give the sound file name using a variable - it’s fixed while editing.

You’ll need to generate a random number, and then have a set of events, each which checks if the random number is certain value, and then playing a certain named sound, like :

3 Likes

God dang, you’re everywhere on this forum.

I gave that a try and it seems to be working, I’ll need to put it to practice a bit more to understand what’s actually happening and then fine tune the heck out of it but it’s a start.

Cheers!

Ah thank goodness someone with brains came along. Although I hope I would have figured out something similar by actually doing it.

1 Like

It seems like you were on the right track and hey, MrMens solution used the extension you mentioned so you still have a rabbit hole to go down!

You’ve both been a big help, thanks heaps :slight_smile:

The RandomInRange(…)? That a standard expression in GDevelop. No extension needed.

That extension lets you set an array of values, and select one at random. It encapsulates a few events into one command.

1 Like

Oh so it is, the icon is similar enough to the one the extension has that I got confused.

Hi mate, you also mentioned playing the sounds in sequence this would simply be a variable the +1 each time a sound plays and if the variable > than number of sounds variable = 0 again to make it cycle through.

1 Like

Sweet, cheers!
I’ve messed around with variables a bit more since, the poor left side of my brain is really struggling to be creative with them though so thanks for the help :sweat_smile:

And simpler to that method is to use the mod function, it’ll increment and reset to 0 in one command. The following statement will cycle from 0 to 3 and then repeat again :

image

1 Like

Ok sweet, that should tidy things up a bit

Since you’re here I have an extra question that’s somewhat related
I’ll expand on it below but the main thing I want to ask is what would be the easiest way to have something trigger every time the value of a variable is changed?

I want to have one of these sounds trigger every time the player health goes down, rather than having it apply every time they’re in collision with an enemy.
Having the sounds play when in collision has it cycle very quickly and overlap, and if I add the ‘play once’ condition then they wont play additional times if there’s prolonged contact with an enemy.
I have a short cooldown on damage being able to be taken and I could apply the same kind of thing to the damage sounds, but I figured I’d rather learn how to do this for future use.

Play the sound every time you decrease the player health. You have an action to decrease the player health, so add the events I posted above as a subevent of the decrease health action (minus the check for collision action).

1 Like

There are multiple independent actions that decrease player health and there will most likely be more spread across multiple enemies and hazards.

I’m hoping to have the damage sounds be tied to the player taking damage from any source/when the variable itself changes, rather than having them tied to each and every source of damage. It just seems easier to keep track of.

Ok, so instead of decreasing health in a number of events, set a variable (decreaseHealth??) in these events and remove the action that decreases the health variable.

Then in a separate event, check whether decreaseHealth is greater than 0, decrease the health variable by the value of decreaseHealth, set decreaseHealth to 0 and play the sounds.

1 Like

Oh my god I think that worked, I could cry, thank you so much!
It took me a while to understand where to put the variables; my brain seemed to reject the concept of making ‘damage’ a scene variable, I need to work with scene variables rather than object variables more often methinks.

2 Likes