Non repeating random

Posted By: jumpman

Non repeating random - 05/10/18 18:20

Hello friends,

I have a series of different sound effects, for example, metal impact sounds. When a sword hits a shield, I want it play a random impact sound, but never 2 of the same sounds in a row. Whats the pseudo code for this?
Posted By: txesmi

Re: Non repeating random - 05/10/18 18:53

Hi,
play next sound into an array when the random sample returns the same of the previous play.

Code:
SOUND *sndArray[SOUND_COUNT];
...
int _i = random(SOUND_COUNT);
if(_i == my->skSound)
	my->skSound = (_i + 1) % SOUND_COUNT;
else
	my->skSound = _i;
ent_playsound(me, sndArray[my->skSound], 100);



Salud!
Posted By: jumpman

Re: Non repeating random - 05/12/18 05:02

Hi txesmi!!

Thank you for taking the time! skSound is a local variable correct?

How do you fill that sndArray with sounds? Is each sound in that array a string?
Posted By: txesmi

Re: Non repeating random - 05/12/18 07:26

You are welcome laugh

Originally Posted By: jumpman
skSound is a local variable correct?

Yes, a variable to save the index of the played sound at the end, local or not.

Originally Posted By: jumpman
Is each sound in that array a string?

Nops. They are pointers to SOUND structs.

Originally Posted By: jumpman
How do you fill that sndArray with sounds?

I use to build some helper functions by the array: create, destroy and functionality.

Code:
#define SND_METAL_COUNT     5
SOUND *sndMetal[SND_METAL_COUNT];

void sndMetalCreate () {
	sndMetal[0] = snd_create("metald01.wav");
	sndMetal[1] = snd_create("metald02.wav");
	sndMetal[2] = snd_create("metald03.wav");
	sndMetal[3] = snd_create("metald04.wav");
	sndMetal[4] = snd_create("metald05.wav");
}

void sndMetalRemove () {
	int _i = 0;
	for (; _i<SND_METAL_COUNT; _i+=1)
		snd_remove(sndMetal[_i]);
}

SOUND *sndMetalRandom () {
	static int _indexOld = 0;
	int _i = random(SND_METAL_COUNT - 1);
	if (_i >= _indexOld)
		_i += 1;
	_indexOld = _i;
	return sndMetalArray[_i];
}

...

sndMetalCreate ();
...
ent_playsound(me, sndMetalRandom(), 100);
...
sndMetalRemove ();


Notice that I used a little bit cheaper algorithm. It also mantains a uniform probability the other did not.

Salud!
Posted By: jumpman

Re: Non repeating random - 05/15/18 19:04

Thank you txesmi!

Is constantly creating and removing sounds OK for the engine?
Posted By: Kartoffel

Re: Non repeating random - 05/15/18 20:28

yes but keep in mind that this might cause stuttering while the file is being loaded.

usually you create a sound once and keep it (a more sophisticated approach would be only loading sounds in areas of the game where they are needed)

I'm also sure that txesmi's example was meant that way.

when setting up your application you call sndMetalCreate() to create all the sounds needed and you then can play them as much as you want.
once you don't need the sounds anymore (like closing your application) you call sndMetalRemove() to remove those sounds again.
Posted By: txesmi

Re: Non repeating random - 05/15/18 20:39

Originally Posted By: Kartoffel
I'm also sure that txesmi's example was meant that way.

Yes, of course.
© 2024 lite-C Forums