Code: Select all
sound_ambience_handle = -1
ambience_table = {
level1_amb = "amb_low_wind", level1_vol = 50,
level2_amb = "amb_dungeon_wind_drips", level2_vol = 30,
[... etc ...]
}
function level_ambience(level)
local level_amb = "level" .. level .. "_amb"
local level_vol = "level" .. level .. "_vol"
if (sound_ambience_handle > -1) then
dsb_stopsound(sound_ambience_handle)
end
sound_ambience_handle = dsb_sound(snd[ambience_table[level_amb]], true)
dsb_set_soundvol(sound_ambience_handle, ambience_table[level_vol])
end
Code: Select all
function sys_enter_level(level)
level_ambience(level + 1)
end
DSB seems to continue playing whatever ambient sound it was playing when it was saved. This means that I get multiple sound streams playing together, because the sys_enter_level function is run upon a save game reload, and plays another sound on top of what was playing when it was saved.
This seems to be because the sound_ambience_handle was lost when reloading.
I tried saving the sound handle in an exported variable, so that the sound channel is retained between saves/reloads, but this doesn't seem to work reliably.
Code: Select all
dsb_export("save_data")
save_data = {
sound_ambience_handle
}
How should I construct this so that I can reliably manage the ambience across both level changes and saves/reloads?