I have a possible solution, but it's a lot of work for potentially not much gain.
1. At the top of your init.lua script for your mod, you can redefine the defineSound method to capture the data as it's defined. AS long as this comes before the include "assets/.../standard_assets.lua" then you should capture all of that data too.
2. Define a script entity which has source code built in listing these sounds.
3. Place that script entity in your level.
A quick draft (untested)...
init.lua
Code: Select all
g_sounds = { };
oldDefineSound = defineSound;
function defineSound(data)
table.insert(g_sounds, data.name)
oldDefineSound(data)
end
import "assets/.../standard_assets.lua";
-- Other Imports
function build_sound_db_source()
local lines = { }
table.insert(lines, "data = { }");
for _, value in ipairs(g_sounds) do
table.insert(lines, "table.insert(data, \"" .. value .. "\")");
end
return table.concat(lines, "\n");
end
defineObject{
name = "sound_db",
baseObject = "script_entity",
components = {
{
class = "Script",
source = build_sound_db_source(),
}
}
}
Then you can make a "safe" play sound that checks the sound DB by placing the sound_db object in your dungeon and then calling the following from a script.
In a script entity
Code: Select all
function playSoundSafe(name)
if sound_db.script.data[name] == nil then
Console.warn("Sound not available")
else
playSound(name)
end
end
playSoundSafe("ding")
I've literally just written that without checking anything like the scripting reference, so it likely won't work out of the box (and there is a tiny chance it's not actually a viable option at all), but I think it should work with just a few tweaks - if you really want to go through that much effort to implement this feature!