Requesting help on writing an audio manager that I can share across the game.

Hi,

From experience of writing my previous games, I used an audio manager that each object can access if it wants to playback a sound effect. So I was thinking of doing the same thing with Corona but due to the nature of Lua, I find it not that obvious as it is in regular languages.

What I used to do is to create a static class so each object can access it directly and either asks that class to play sound for him or get handle of preloaded sound effect and play it back himself (so he can listen to it’s events, if he’s interested).

So how can I achieve such thing with Lua?

Things that prevented me from writing this on my own are:

1- How to make sure that class is initialized once because if I put require(“audio_manager”) on top of every class that wants to play a sound, I have no idea if Lua/Corona goes and loads all files for every require call.

2- How to give it’s handle to every object that wants to access it? Should I pass it’s handle as a parameter to my constructors?

first step would be on how to initialize a class once and load all SFX’s there and share it between different objects so they would not each init that class.

Thanks. [import]uid: 206803 topic_id: 35993 reply_id: 335993[/import]

I gave this alot of thought and as I could not find a way to do static with Lua, I think I’m down with two options here:

  1. require my audio manager on top of my gameplay screens and pass it’s reference to constructor of each object, so later on they have a reference to call it.

  2. I somehow find a way to do static stuff with Lua, which I’m yet to.

Appreciate any lights shed on the matter. [import]uid: 206803 topic_id: 35993 reply_id: 143296[/import]

Couldn’t you set it up as an AudioManager class and use custom event dispatching?

http://www.ardentkid.com/blog/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk#eventdispatching
[import]uid: 33275 topic_id: 35993 reply_id: 143299[/import]

Hi there,

You can easily create a static class in Corona:

--  
audioManager.lua  
--  
  
local audioManager = {}  
  
audioManager.popSound = audio.loadSound("pop.wav") -- create more lines like this if you wish  
  
audioManager.pop = function()  
 audio.play(audioManager.popSound)  
end -- audioManager.pop  
  
audioManager.fadeOutAllSounds = function()  
 -- put a function in here that does what you want  
end -- audioManager.fadeOutAllSounds  
  
--  
--  
  
return audioManager  

Then call this from somewhere like your main.lua:

audioManager = require("audioManager")  

Just use it as a global and call the methods from anywhere you want like this:

audioManager.pop()  

or

audioManager.fadeOutAllSounds()  

You’ll never notice any performance hit with the global way, unless you call 100 methods per second. By the way, Lua is smart, so if you require the same module from different places, Lua only loads it into memory once. [import]uid: 70134 topic_id: 35993 reply_id: 143310[/import]

I gave this alot of thought and as I could not find a way to do static with Lua, I think I’m down with two options here:

  1. require my audio manager on top of my gameplay screens and pass it’s reference to constructor of each object, so later on they have a reference to call it.

  2. I somehow find a way to do static stuff with Lua, which I’m yet to.

Appreciate any lights shed on the matter. [import]uid: 206803 topic_id: 35993 reply_id: 143296[/import]

Couldn’t you set it up as an AudioManager class and use custom event dispatching?

http://www.ardentkid.com/blog/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk#eventdispatching
[import]uid: 33275 topic_id: 35993 reply_id: 143299[/import]

Hi there,

You can easily create a static class in Corona:

--  
audioManager.lua  
--  
  
local audioManager = {}  
  
audioManager.popSound = audio.loadSound("pop.wav") -- create more lines like this if you wish  
  
audioManager.pop = function()  
 audio.play(audioManager.popSound)  
end -- audioManager.pop  
  
audioManager.fadeOutAllSounds = function()  
 -- put a function in here that does what you want  
end -- audioManager.fadeOutAllSounds  
  
--  
--  
  
return audioManager  

Then call this from somewhere like your main.lua:

audioManager = require("audioManager")  

Just use it as a global and call the methods from anywhere you want like this:

audioManager.pop()  

or

audioManager.fadeOutAllSounds()  

You’ll never notice any performance hit with the global way, unless you call 100 methods per second. By the way, Lua is smart, so if you require the same module from different places, Lua only loads it into memory once. [import]uid: 70134 topic_id: 35993 reply_id: 143310[/import]

I gave this alot of thought and as I could not find a way to do static with Lua, I think I’m down with two options here:

  1. require my audio manager on top of my gameplay screens and pass it’s reference to constructor of each object, so later on they have a reference to call it.

  2. I somehow find a way to do static stuff with Lua, which I’m yet to.

Appreciate any lights shed on the matter. [import]uid: 206803 topic_id: 35993 reply_id: 143296[/import]

Couldn’t you set it up as an AudioManager class and use custom event dispatching?

http://www.ardentkid.com/blog/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk#eventdispatching
[import]uid: 33275 topic_id: 35993 reply_id: 143299[/import]

Hi there,

You can easily create a static class in Corona:

--  
audioManager.lua  
--  
  
local audioManager = {}  
  
audioManager.popSound = audio.loadSound("pop.wav") -- create more lines like this if you wish  
  
audioManager.pop = function()  
 audio.play(audioManager.popSound)  
end -- audioManager.pop  
  
audioManager.fadeOutAllSounds = function()  
 -- put a function in here that does what you want  
end -- audioManager.fadeOutAllSounds  
  
--  
--  
  
return audioManager  

Then call this from somewhere like your main.lua:

audioManager = require("audioManager")  

Just use it as a global and call the methods from anywhere you want like this:

audioManager.pop()  

or

audioManager.fadeOutAllSounds()  

You’ll never notice any performance hit with the global way, unless you call 100 methods per second. By the way, Lua is smart, so if you require the same module from different places, Lua only loads it into memory once. [import]uid: 70134 topic_id: 35993 reply_id: 143310[/import]

I gave this alot of thought and as I could not find a way to do static with Lua, I think I’m down with two options here:

  1. require my audio manager on top of my gameplay screens and pass it’s reference to constructor of each object, so later on they have a reference to call it.

  2. I somehow find a way to do static stuff with Lua, which I’m yet to.

Appreciate any lights shed on the matter. [import]uid: 206803 topic_id: 35993 reply_id: 143296[/import]

Couldn’t you set it up as an AudioManager class and use custom event dispatching?

http://www.ardentkid.com/blog/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk#eventdispatching
[import]uid: 33275 topic_id: 35993 reply_id: 143299[/import]

Hi there,

You can easily create a static class in Corona:

--  
audioManager.lua  
--  
  
local audioManager = {}  
  
audioManager.popSound = audio.loadSound("pop.wav") -- create more lines like this if you wish  
  
audioManager.pop = function()  
 audio.play(audioManager.popSound)  
end -- audioManager.pop  
  
audioManager.fadeOutAllSounds = function()  
 -- put a function in here that does what you want  
end -- audioManager.fadeOutAllSounds  
  
--  
--  
  
return audioManager  

Then call this from somewhere like your main.lua:

audioManager = require("audioManager")  

Just use it as a global and call the methods from anywhere you want like this:

audioManager.pop()  

or

audioManager.fadeOutAllSounds()  

You’ll never notice any performance hit with the global way, unless you call 100 methods per second. By the way, Lua is smart, so if you require the same module from different places, Lua only loads it into memory once. [import]uid: 70134 topic_id: 35993 reply_id: 143310[/import]