Page 1 of 1

tween.lua and love.audio.setVolume

Posted: Mon Aug 10, 2015 8:00 pm
by jmartin82
Hi,

Does anyone know if it is possible to use tween.lua to fade out audio using love.audio.setVolume? The tween.lua github page says the target must be a table, so I feel like I might be out of luck since setVolume is a function. I'm not sure if I can pass the target into tween.lua in any way.

Re: tween.lua and love.audio.setVolume

Posted: Mon Aug 10, 2015 9:21 pm
by Roland_Yonaba
Just a random idea.
Create a tween object that changes gradually the volume level from a value to another (Here, from 0 to 1).

Code: Select all

local volume = {level = 0}
local volumeTweener = tween.new(duration, volume, {level = 1}, easingFunction)
Then, in love.update, use this :

Code: Select all

function love.update(dt)
  -- some code
  volumeTweener:update(dt)
  love.audio.setVolume(volume.level)
  -- rest of the code
end
This just popped in my head, I haven't tried it. I don't know if calling love.audio.setVolume repetively every dt is advised, so I'll let people provide some feedback.

Re: tween.lua and love.audio.setVolume

Posted: Mon Aug 10, 2015 10:05 pm
by Ulydev
Roland_Yonaba wrote:Just a random idea.
This is what I always do, and it works perfectly. :awesome:
Though I don't know if it affects performance.

Re: tween.lua and love.audio.setVolume

Posted: Tue Aug 11, 2015 7:57 am
by bartbes
It's a bit awkward, but metatables can do this too!

Code: Select all

local volume = setmetatable({}, {
    __index = function() return 0 end, -- the initial value
    __newindex = function(self, k, v) return love.audio.setVolume(v) end,
})
local volumeTweener = tween.new(duration, volume, {volume = 1}, easingFunction)
You don't even need to keep a reference to the 'volume' table.

Re: tween.lua and love.audio.setVolume

Posted: Tue Aug 11, 2015 12:39 pm
by Roland_Yonaba
bartbes wrote:It's a bit awkward, but metatables can do this too!

Code: Select all

local volume = setmetatable({}, {
    __index = function() return 0 end, -- the initial value
    __newindex = function(self, k, v) return love.audio.setVolume(v) end,
})
local volumeTweener = tween.new(duration, volume, {volume = 1}, easingFunction)
You don't even need to keep a reference to the 'volume' table.
Lövely, Bartbes.
You have been blissed. :3

Re: tween.lua and love.audio.setVolume

Posted: Tue Aug 11, 2015 4:41 pm
by jmartin82
Thank you so much, everyone!

I ended up using Roland_Yonaba's suggestion since it looked a little cleaner than the metatables (which also just scare me right now, haha). Seems like it works perfectly!