Using sound effects as _G.

Hi there,

Not sure if i’m committing a faux pas here, but I thought i’d check and ask for your opinion!

I’m currently using a bunch of sound effects for my game as _G. variables set in main.lua (i’m still using Director class by the way). The main reason is primarily because i’ve hit a problem with using 200 local variables upvalue error (something along these lines) so since these sound effects are used throughout my all my .lua files i thought of setting them as _G.

So, i’ve currently got a bunch of: _G.sound1=audio.loadSound(“assets/sound1.wav”) etc in my main.lua. And in my other .lua fles, i set a local soundEffect variable and play the sound using: local soundEffect=audio.play(_G.sound1); This means, at the end of these .lua files, i simply: audio.stop();soundEffect=nil;

Because i’m using globals here, how do i (or do i need to) remove these? Normally, if it was a local variable i’d also add: audio.dispose(sound1);sound1=nil.

Just wondering, would the following be better (so, still using _G. and inserted in main.lua):

  local function onSystem(event)   if event.type == "applicationStart" then      \_G.sound1 = audio.loadSound("assets/sound1.wav") etc...     elseif event.type == "applicationExit" then       audio.dispose(sound1); sound1 = nil;       Runtime:removeEventListener( "system", onSystem ) --note: do i also need to set this here?     end   end   Runtime:addEventListener( "system", onSystem )  

Would this approach be ok and do you see any issues with this?

Thanks for reading!

Cheers,

An easy way to solve the 200 local variables problem is to put all your local variables into one table, i.e.

[lua]

local v = {}

v.score = 100

v.bullets = 3

v.onGround = false

v.grid = {}

[/lua]

This way you’re only using one of your 200 limit for as many variables, objects and tables as you want.

As for sounds, I also load them in main.lua so they can be used throughout the app, but again I put them in a table:

[lua]

_G.sounds = {}

sounds.beep = audio.loadSound(“beep.mp3”)    

sounds.hit = audio.loadSound(“hit.mp3”)

[/lua]

And then call them like this - I don’t bother assigning them to a local variable and disposing/nilling:

[lua]

audio.play(sounds.beep)

[/lua]

Hi @Appvism,

For this, I think you should definitely read Rob’s recent tutorial on modulizing audio:

http://www.coronalabs.com/blog/2013/06/04/tutorial-handling-cross-scene-audio/

The core principle should work fine with Director too.

Take care,

Brent

Thanks for the replies and feedback/info back!

Cheers,

Hi there,

Just wanted to confirm, if i use something like local v={} to store my local variables, such as v.flagGameActive=false, on scene change can i simply use: v=nil rather than specifically using: v.flagGameActive=nil?

If i nil the table, would everything inside this get nil’ed as well?

This approach of using a table certainly helps in minimising the 200 upvalue error! Very useful!

Thanks

An easy way to solve the 200 local variables problem is to put all your local variables into one table, i.e.

[lua]

local v = {}

v.score = 100

v.bullets = 3

v.onGround = false

v.grid = {}

[/lua]

This way you’re only using one of your 200 limit for as many variables, objects and tables as you want.

As for sounds, I also load them in main.lua so they can be used throughout the app, but again I put them in a table:

[lua]

_G.sounds = {}

sounds.beep = audio.loadSound(“beep.mp3”)    

sounds.hit = audio.loadSound(“hit.mp3”)

[/lua]

And then call them like this - I don’t bother assigning them to a local variable and disposing/nilling:

[lua]

audio.play(sounds.beep)

[/lua]

Hi @Appvism,

For this, I think you should definitely read Rob’s recent tutorial on modulizing audio:

http://www.coronalabs.com/blog/2013/06/04/tutorial-handling-cross-scene-audio/

The core principle should work fine with Director too.

Take care,

Brent

Thanks for the replies and feedback/info back!

Cheers,

Hi there,

Just wanted to confirm, if i use something like local v={} to store my local variables, such as v.flagGameActive=false, on scene change can i simply use: v=nil rather than specifically using: v.flagGameActive=nil?

If i nil the table, would everything inside this get nil’ed as well?

This approach of using a table certainly helps in minimising the 200 upvalue error! Very useful!

Thanks