tween.lua and love.audio.setVolume

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jmartin82
Prole
Posts: 2
Joined: Mon Aug 10, 2015 7:58 pm

tween.lua and love.audio.setVolume

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: tween.lua and love.audio.setVolume

Post 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.
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: tween.lua and love.audio.setVolume

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: tween.lua and love.audio.setVolume

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: tween.lua and love.audio.setVolume

Post 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
jmartin82
Prole
Posts: 2
Joined: Mon Aug 10, 2015 7:58 pm

Re: tween.lua and love.audio.setVolume

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests