Selecting a sound at random from a predefined list of multiple sounds?


  • Posts: 59
This is my first post and I wanted to start off with a sincere thank you to the folks behind Stencyl.  I'm actually a composer and sound designer who works with indie game developers, and for the longest time I've been kicking around the idea of trying to present my music & sfx portfolio as an actual playable "game," in addition to the standard boring demo reel.  Stencyl is finally helping me to do that.

So I downloaded everything just yesterday and have been through the tutorials.  Now I have my own playable one-level platforming thing just about built out.  The player goes around the level collecting coins, and each time he gets a coin (8 in all), a different song begins (presenting the next song in my portfolio).

But what I would like to do - and I don't know if it's possible - is to provide about 3 or 4 different sound effects for each event.  I'd like to have four different little "hups" or "oofs" on file, and the CPU will pull one at random every time the user jumps ("hup") or lands ("oof").

Need help with figuring out if it's possible to even make a sound when the player hits the ground.  And need help figuring out if possible to have an event randomly select from a pool of sound files each time that it occurs.  Basically I'm trying to make the game audio as robust/diverse as possible. Thanks again for your help!
Need audio for your next project?  Go to


  • *
  • Posts: 2587
You'll need a code call to convert a name into a sound. Then, you use a random number to select the sound, fech that number index from the list, use the entry in the list to get the sound name and play it. So, answering your question, it is possible.

That's basically the logic. I know the call to get an actor type by name, but a sound by name I do not know. Someone else can post here the required code. I'm also curious about this.

EDIT: For a sound when the player hits the ground, the platformer kit should have something bout this.

I guess once someone post here the code call to get a sound by name, I can make a quick behavior to use it a upload it to Forge.
My Stencyl resources are available here:
Cutscenes, RPG Elements, Particles, Map System and many more.

Legendary Hoamaru

  • *
  • Posts: 1088
Getting sound by name:  First, you need to get a list of the games sounds and save it to a list attribute (code block):

_SoundList = Assets.get().getResourcesOfType(SoundClip);

Then, you can use the values in the list with "Equals" blocks to check if it matches a name.

A simpler solution though, is to create a global custom block that takes and returns a sound.

You can then add any sounds to a list.  You can create another global custom block that returns an item from a list as a sound using a single code block.

Use a random number with the list size to select a sound randomly from the list.

(We need an insert attachment function for better formatting)

Edit:  There is an error in the second screenshot.  You'll need to subtract the list length by one.

« Last Edit: June 11, 2011, 02:48:10 am by Legendary Hoamaru »


  • Posts: 59
Thanks very much! I'll be able to get back to working on it tomorrow, and I'll let you know if I was too dumb to follow your instructions.  :)
Need audio for your next project?  Go to