Ever wanted to have random background sound effects in your game? Here's something to get you started.
First some concept:
Make in your sounds directory some subdirectory (mine is called animals) and fill it with group of filenames like this for example:
snd1.ogg
snd2.ogg
snd3.ogg
etc.
then create file Sound_daemon.script (in scripts) which will read following:
method Initialize(group,range,minfreq,maxfreq,volume)
{
//sound group is the path+filename without number
this.SD_sound_group = group;
//How many sounds are in the group?
this.SD_range = range;
// Minimum time which should pass from the last occurence
this.SD_minfreq = minfreq;
// Maximum time which should pass from the last occurence
this.SD_maxfreq = maxfreq;
// Group volume
this.SD_volume = volume;
this.infinite = true;
}
method Done()
{
this.infinite = false;
}
method RunDaemon()
{
}
and also create file scripts/sound_daemon_body.script
while(this.infinite) {
// First wait for the random ammount of time from range
var wait =
Random(this.
SD_minfreq,
this.
SD_maxfreq);
var ent = this.entity;
{
var t =
Random(1,
this.
SD_range);
//grab random sound var fname= "sounds/" + this.SD_sound_group + t+ ".wav";
}
}
Game.
DestroyEntity(this.
entity);
// destroy our entity so we won't mess our memory.
then make some global variables and assign objects to them. I put it into game.script this:
SD_layer1 = new Object("scripts\sound_daemon.script");
SD_layer2 = new Object("scripts\sound_daemon.script");
SD_layer3 = new Object("scripts\sound_daemon.script");
note that you can have as much background sound layers as you want.
Last step is to put into scene_init.script
SD_layer1.Initialize("animals/snd",7,6000,12000,40);
SD_layer1.RunDaemon();
what does it mean?
"animals/snd" is the group filename. (snd1, snd2 ... snd7 in my case)
7 is number of group members (7 files)
6000 is minimum time needed for Random (it's in miliseconds)
12000 is maximum time needed for Random (it's in miliseconds)
(those two parameters are specifying borders in which sound can happen)
40 is volume
When you want to stop sound_daemon, just send
In my test run I had three layers created this way. I have background crickets with pretty often occurence.
second layer are 7 different chirping birds more rarely.
and last layer is faint whispering hearing very rarely.
I think you will find your own application for this simple object.
Huge thanks goes to Mnemonic and Jerrot!
EDITED: for my stupid and silly mistake I made...
EDITED: put away filetype. See below.