2017-04-12 19:20:28

Hey all. I've been wondering whether there is a possibility to load all the sounds in a specific folder in BGT instead of typing blablah.load( and so on. I want a way that makes it easier so that if I have a lot of sounds I won't need to write all of their names. Thanks

2017-04-12 20:08:05

Yep!
Depends what you're trying to do with them, though.
I'll assume, for this example, that you're loading them into a dictionary or something:

// (dir is the path to the directory where the sounds are.)
dictionary@ load_all(string dir) {
    dictionary ret;
    string[] files=find_files(dir + "*.ogg");
    for(uint i=0; i<files.length(); i++) {
        sound@ snd=null;
        sound temp;
        @snd=temp; // This is because BGT dictionaries and object creation are kinda ridiculous.
        ret.set(files[i], @snd);
        // If you don't have any .ogg and .wav files with the same name, you could use string_trim_right to make the key shorter, to save on typing when you want to play it.
    }
    files=find_files(dir + "*.wav");
    for(uint i=0; i<files.length(); i++) {
        sound@ snd=null;
        sound temp;
        @snd=temp;
        ret.set(files[i], @snd);
    }
    return ret;
}

I find it easier to just use the sound pool, but it does make manipulating any properties other than position somewhat cumbersome.
Philip Bennefall wrote an example preload function, somewhere, a while back. I'm not sure where--it might be on the blastbay forums, even. Other than large files like music, I don't usually notice enough lag, even when my computer is at its worst and pretending it barely has 1g of ram, to feel like preloading all of the smaller sounds is necessary. But music... yeah, I preload music. No one wants the game to pause mid level for a song to load, and streaming oggs results in a significant performance hit on slower systems.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2017-04-12 20:14:42

No, what I want is the following: I have a folder called sounds and the sounds are named from 1 to 100. They have the format OGG. I want them to be loaded, and to be played 2d on a map. For example, if the user presses space, it'll play the sounds I loaded randomly. And if he walks around the map while the sound is playing, he shall hear it in the distance.

2017-04-13 03:24:47

Well, you already have numbered files. Why not just use a for loop?

Kai

Spill chuck you spots!

2017-04-13 16:04:31

And that's where it gets confusing. Using a for loop may need an array, and I don't know in which way shall I specify the loop to load all of the sounds as it will not need strings.

2017-04-13 17:48:37 (edited by CAE_Jones 2017-04-13 17:50:31)

Are you wanting to play the sounds randomly all over the map, or for the player to place sounds on the map and hear them as they move around?
If it's the first one, you can just do this:

sound_pool pool;

int map_width=100, map_height=100;

for(uint i=1; i<=100; i++) {
pool.play_2d("sounds/amb" + i + ".ogg", 0, 0, random(0, map_width), random(0, map_height), true);
}

If it's the second one, it's even easier:

int player_x=0, player_y=0;

if(key_pressed(KEY_SPACE)) {
pool.play_2d("sounds/amb" + random(1, 100) + ".ogg", player_x, player_y, player_x, player_y, true);
}

Then just use pool.update_listener_2d when the player moves.

If, though, you want to do the second one, but preload the sounds beforehand, combine it with the example in my first reply. BGT keeps sounds in memory after they've been loaded, so you can use the sound_pool, even though it isn't very practical to preload into it directly.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.