Randomly selecting a sound from a list

So i thought id post this here because i can’t figure out a way to do this without spaghetti code and work arounds

((Using @mathbrush and @Draconis 's sound addons))

Does anyone know how i would make an array of sounds for the same ‘event’ and have it decide which at random when triggered

Im not too knowledgable on “random” / tables or lists in Inform just yet

Thanks all!

1 Like

There are a couple of ways of doing this. I opened up my Inform to practice this and had your game file opened so I’m using examples from that.

The first is to make a table that just has one column (which I’ve called currentsound), and to pick a random row from it:

Table of random sounds
currentsound
sound of bookcheck
sound of addspellscroll

Instead of thinking:
	Choose a random row from the table of random sounds;
	play currentsound entry;

The second is to make a bigger table that categorizes the songs by their emotional content (which I’ve called ‘motif’ here). To pick a song with the right ‘motif’, you can sort the table randomly and then pick the first row with that motif:

A motif is a kind of value. The motifs are spooky, nice, and mean.

Table of sorted random sounds
currentmotif	currentsound
nice	sound of bookcheck
nice	sound of addspellscroll
mean	sound of playerdeath

Instead of sleeping:
	sort the table of sorted random sounds in random order;
	choose a row with currentmotif of nice in the table of sorted random sounds;
	play currentsound entry;

You don’t have to use values or motifs, though, you could also do this with just rooms:

Table of sorted random sounds
currentroom	currentsound
the clearing	sound of bookcheck
the aether clearing	sound of addspellscroll
the clearing	sound of playerdeath

Instead of sleeping:
	sort the table of sorted random sounds in random order;
	let temp be the location of the player;
	if temp is a currentroom listed in the table of sorted random sounds:
		choose a row with currentroom of temp in the table of sorted random sounds;
		play currentsound entry;

(note that, in case the code doesn’t copy right, there should be tabs between columns)

2 Likes

Spectacular response thank you.

I may use the motif version in my specific example as it is going to be mood based responses from an NPC. Hope this helps others too!

1 Like

i can also confirm that proceeding these methods with the audio layer name is practical

eg.

play currentsound entry on midground

1 Like

a little look at how i’ve used this to give a small audio prompt to set the tone of a line of dialogue (I’ve added enc to the name of some variables to allow for multiple characters using this method)

1 Like