Changing music volume always at text?

  • #1, by NovelWednesday, 12. March 2014, 23:19 10 years ago
    I have a very cool soundtrack now for my game, but it includes lyrics and I fear that it might interfer with the speech. One possibility is to really put it in the background by turnin the volume down all the time and just put it up when it's certain that nobody speaks.
    Another possibility would be to turn it down anytime someone starts to speak, but that's pretty exhausting to script manually, so I was wondering if that can be automated by using LUA maybe?

    Newbie

    100 Posts


  • #2, by afrlmeThursday, 13. March 2014, 00:59 10 years ago
    I don't see why you couldn't use the registerHookFunction to check for displayed text or the registerEventHandler with textStarted, textStopped handles to dip & rise the volume wink
    -- * local variables * --
    local val = 10 -- amount to raise/lower the volume by...
    
    -- * text started lower volume * --
    function onTextStarted(text)
     if text:getLink(VTextOwner):getId().tableId == eCharacters then setVolume(eMusicVolume, (getVolume(eMusicVolume) - val)) end
    end
    
    -- * text stopped raise volume * --
    function onTextStopped(text)
     if text:getLink(VTextOwner):getId().tableId == eCharacters then setVolume(eMusicVolume, (getVolume(eMusicVolume) + val)) end
    end
    
    -- * the event listeners for text start & stop * --
    registerEventHandler("textStarted", "onTextStarted")
    registerEventHandler("textStopped", "onTextStopped")
    

    This is just a quick example. I've not tested the code.

    Imperator

    7278 Posts

  • #3, by NovelSunday, 23. March 2014, 20:36 10 years ago
    Awesome! It works perfectly! Thank you! smile

    Newbie

    100 Posts

  • #4, by afrlmeSunday, 23. March 2014, 20:44 10 years ago
    haha... I guess sometimes I can et things right on the first try... unusual for something I devised off the top of my noggin'! smile

    Welcomes, anyway.

    * edit: added script to the script index & vs functions page of the new wiki wink

    Imperator

    7278 Posts

  • #5, by NovelSaturday, 27. December 2014, 12:00 10 years ago
    I'd like to enable the player to turn the music off and on via a key command. So I guess I need to turn the music volume to zero and then toggle a "bypass"-condition in which I'd embed your LUA script.

    A very simple task... but I still haven't learned the LUA-basics so I fail to solve it. Help!

    Newbie

    100 Posts

  • #6, by afrlmeSaturday, 27. December 2014, 13:19 10 years ago
    You could do this with a condition & a value.

    -- updated lower/raise volume.
    -- * local variables * --
    local val = 10 -- amount to raise/lower the volume by...
    
    -- * text started lower volume * --
    function onTextStarted(text)
     if text:getLink(VTextOwner):getId().tableId == eCharacters and Conditions["music_muted"].ConditionValue then setVolume(eMusicVolume, (getVolume(eMusicVolume) - val)) end
    end
    
    -- * text stopped raise volume * --
    function onTextStopped(text)
     if text:getLink(VTextOwner):getId().tableId == eCharacters and Conditions["music_muted"].ConditionValue then setVolume(eMusicVolume, (getVolume(eMusicVolume) + val)) end
    end
    
    -- * the event listeners for text start & stop * --
    registerEventHandler("textStarted", "onTextStarted")
    registerEventHandler("textStopped", "onTextStopped")
    


    mute music script.
    if Conditions["music_muted"].ConditionValue then
     Conditions["music_muted"].ConditionValue = false -- set unmuted
     setVolume( eMusicVolume, Values["music_vol"].Int ) -- restore music volume
    else
     Conditions["music_muted"].ConditionValue = true -- set muted
     Values["music_vol"].Int = getVolume(eMusicVolume) -- store current music volume
     setVolume(eMusicVolume, 0) -- mute music volume
    end
    

    ...create the condition & value & add this script to an execute a script action part inside of a key input (released) action.

    Imperator

    7278 Posts

  • #7, by NovelSaturday, 27. December 2014, 21:39 10 years ago
    Hm, doesn't work yet. Do the condition and the value have to go in a certain place? I just put them in some scene and didn't change anything. Toggeling the music with the script works then, but when the music is off, it comes back after any text is said.

    Newbie

    100 Posts

  • #8, by afrlmeSaturday, 27. December 2014, 23:23 10 years ago
    ah yeah er sorry I should have added "not" before the conditions in the dynamic volume script part.

    -- * local variables * --
    local val = 10 -- amount to raise/lower the volume by...
    
    -- * text started lower volume * --
    function onTextStarted(text)
     if text:getLink(VTextOwner):getId().tableId == eCharacters and not Conditions["music_muted"].ConditionValue then setVolume(eMusicVolume, (getVolume(eMusicVolume) - val)) end
    end
    
    -- * text stopped raise volume * --
    function onTextStopped(text)
     if text:getLink(VTextOwner):getId().tableId == eCharacters and not Conditions["music_muted"].ConditionValue then setVolume(eMusicVolume, (getVolume(eMusicVolume) + val)) end
    end
    
    -- * the event listeners for text start & stop * --
    registerEventHandler("textStarted", "onTextStarted")
    registerEventHandler("textStopped", "onTextStopped")
    

    Imperator

    7278 Posts

  • #9, by NovelMonday, 29. December 2014, 13:13 10 years ago
    Works! I love you. :-)

    Newbie

    100 Posts

  • #10, by wimfThursday, 11. February 2016, 10:09 8 years ago
    Hello. Nice script. It is possible to have the same effect (updated lower/raise volume) on the narration voice ?

    Thank in advance

    Forum Fan

    238 Posts

  • #11, by afrlmeThursday, 11. February 2016, 16:44 8 years ago
    Yeah, you need to remove the if query bits from the 2 functions.
    -- * local variables * --
    local val = 10 -- amount to raise/lower the volume by...
    
    -- * text started lower volume * --
    function onTextStarted(text)
     if not Conditions["music_muted"].ConditionValue then setVolume(eMusicVolume, (getVolume(eMusicVolume) - val)) end
    end
    
    -- * text stopped raise volume * --
    function onTextStopped(text)
     if not Conditions["music_muted"].ConditionValue then setVolume(eMusicVolume, (getVolume(eMusicVolume) + val)) end
    end
    
    -- * the event listeners for text start & stop * --
    registerEventHandler("textStarted", "onTextStarted")
    registerEventHandler("textStopped", "onTextStopped")
    

    ... assuming you are using the same script as above. I just removed the queries that check if the text belongs to a character. Now it should work for any displayed text.

    Imperator

    7278 Posts