Custom sounds for custom levers, buttons or other animations
Posted: Sat Jan 25, 2014 8:56 pm
It's probably not an issue in the upcoming LoG2, but I was asked about custom sounds (or removing the hard coded ones) in LoG1. So here is a method that works reasonably well, and allows for the use of custom sounds for buttons, levers, or custom versions of these.
As far as I know there is no way to prevent the default sounds from playing, and there are no lever or button hooks, (and so no easy way to detect the use of switches) ~that I am aware of.
So this is the way I devised.
________________
How to do it:
First, re-define the default Button and Lever sounds to a silent sound effect, placed in your "sounds" folder. Use the following script placed in the sounds.lua file in your project.
This will make ALL buttons and levers silent unless connected to the sound effect script.
Next add the soundEffect script to the map: [note:updated script, now does the connections automatically.]
Place this code in a script object and name it 'customSounds'.When connected to this script, all regular buttons and levers sound like they would normally. The feature of this is that you can now define custom sounds (usually in the sounds.lua file) and have any button or lever play that sound when used; and you do this by appending the capital letters 'SND' and then the name of the sound effect to the object.id.
In a real map you would use the name of your custom sound, but for the example I will use the Ogre's scream with a button. You do this by making a button, and adding 'SND_' plus the name of the sound; in this case: 'ogre_rush_begin'... So you add 'SND_ogre_rush_begin' and you have a button named 'wall_button_1SNDogre_rush_begin'. This makes the button scream when pressed, just as it would play any defined sound, if you used that name instead.
On large multi-floor maps, you could use copies of the sound effect script per floor, to keep from needing to connect switches to a script on a different floor. Duplicated scripts will work just the same.
Video example.
UPDATED!:
I updated the script, and it will now auto-update all levers and buttons to have sounds ~without manual connections. I also changed the token from an Asterisk to the letters 'SND_', because it seems that anything but an alphanumeric character messes up calling the object in scripts.
As far as I know there is no way to prevent the default sounds from playing, and there are no lever or button hooks, (and so no easy way to detect the use of switches) ~that I am aware of.
So this is the way I devised.
________________
How to do it:
First, re-define the default Button and Lever sounds to a silent sound effect, placed in your "sounds" folder. Use the following script placed in the sounds.lua file in your project.
Code: Select all
defineSound{
name = "modButton",
filename = "assets/samples/env/button_01.wav",
loop = false,
volume = 1,
minDistance = 1,
maxDistance = 10,
}
defineSound{
name = "modLever",
filename = "assets/samples/env/lever_01.wav",
loop = false,
volume = 1,
minDistance = 1,
maxDistance = 10,
}
defineSound{
name = "button",
filename = "mod_assets/sounds/silent.wav",
loop = false,
volume = 1,
minDistance = 1,
maxDistance = 10,
}
defineSound{
name = "lever",
filename = "mod_assets/sounds/silent.wav",
loop = false,
volume = 1,
minDistance = 1,
maxDistance = 10,
}
Next add the soundEffect script to the map: [note:updated script, now does the connections automatically.]
Place this code in a script object and name it 'customSounds'.
Code: Select all
for l = 1, getMaxLevels() do
for e in allEntities(l) do
if e.class ~= nil and e.class == "Button" then
e:addConnector('toggle', 'customSounds', 'soundEffect')
elseif e.class ~= nil and e.class == "Lever" then
e:addConnector('any', 'customSounds', 'soundEffect')
end
end
print('Sound connections added to all buttons and levers!')
end
function soundEffect(caller)
if caller and string.match(caller.id, 'SND_') then
local sEffect = string.match(caller.id, "SND_"..".+")
playSoundAt(string.sub(sEffect,5),caller.level,caller.x,caller.y)
elseif caller and caller.class == "Button" then
playSoundAt('modButton', caller.level,caller.x,caller.y)
elseif caller and caller.class == "Lever" then
playSoundAt('modLever', caller.level,caller.x,caller.y)
end
end
In a real map you would use the name of your custom sound, but for the example I will use the Ogre's scream with a button. You do this by making a button, and adding 'SND_' plus the name of the sound; in this case: 'ogre_rush_begin'... So you add 'SND_ogre_rush_begin' and you have a button named 'wall_button_1SNDogre_rush_begin'. This makes the button scream when pressed, just as it would play any defined sound, if you used that name instead.
On large multi-floor maps, you could use copies of the sound effect script per floor, to keep from needing to connect switches to a script on a different floor. Duplicated scripts will work just the same.
Video example.
UPDATED!:
I updated the script, and it will now auto-update all levers and buttons to have sounds ~without manual connections. I also changed the token from an Asterisk to the letters 'SND_', because it seems that anything but an alphanumeric character messes up calling the object in scripts.
Code: Select all
for l = 1, getMaxLevels() do
for e in allEntities(l) do
if e.class == "Button" then
e:addConnector('toggle', 'customSounds', 'soundEffect')
elseif e.class == "Lever" then
e:addConnector('any', 'customSounds', 'soundEffect')
end
end
print('Sound connections added to all buttons and levers!')
end
function soundEffect(caller)
if caller and string.match(caller.id, 'SND_') then
local sEffect = string.match(caller.id, "SND_"..".+")
playSoundAt(string.sub(sEffect,5),caller.level,caller.x,caller.y)
elseif caller and caller.class == "Button" then
playSoundAt('modButton', caller.level,caller.x,caller.y)
elseif caller and caller.class == "Lever" then
playSoundAt('modLever', caller.level,caller.x,caller.y)
end
end