2 registered members (AndrewAMD, SBGuy),
987
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Non repeating random
[Re: jumpman]
#472653
05/10/18 18:53
05/10/18 18:53
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, play next sound into an array when the random sample returns the same of the previous play.
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!
|
|
|
Re: Non repeating random
[Re: jumpman]
#472663
05/12/18 07:26
05/12/18 07:26
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
You are welcome skSound is a local variable correct? Yes, a variable to save the index of the played sound at the end, local or not. Is each sound in that array a string? Nops. They are pointers to SOUND structs. How do you fill that sndArray with sounds? I use to build some helper functions by the array: create, destroy and functionality.
#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!
Last edited by txesmi; 05/12/18 07:44. Reason: error!! fixed
|
|
|
Re: Non repeating random
[Re: jumpman]
#472720
05/15/18 20:28
05/15/18 20:28
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
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.
POTATO-MAN saves the day! - Random
|
|
|
|