How do you make a variable visible in all scenes?

So basically the question says it all. In one of my scenes, I created a variable that if it isn’t equal to 1, the game will have sound. If it is equal to 1, the game won’t. So, my code says:

if soundvariable ~= 1 then  
playmusic = audio.loadSound("music.wav")  
playchannel = audio.play(playmusic)  
end  
  

And I also have a mute button that when you touch it, it makes soundvariable = 1. And then the music stops. But when I go to a different scene and then I come back to this scene, the music starts again. How can I make soundvariable visible in all my scenes? Thanks! [import]uid: 38001 topic_id: 11408 reply_id: 311408[/import]

By declaring it without the keyword local in front of it.

I have a few global variables used to track game states, etc. that are declared like so:

_gameState = 1

in my main.lua file.

There are methods using the _G global state, or some such, that I haven’t played with. [import]uid: 5317 topic_id: 11408 reply_id: 41329[/import]

Like Mike said declare it as global by ommitting the local declaration.

It could be handy to put the vars into a container so you can clean them up easily. Otherwise you could lose track of them throughout the dev process.

main.lua:
myvars = {}

declarations:
mvars.a = “a”
myvars.b = “b” [import]uid: 11393 topic_id: 11408 reply_id: 41331[/import]

I second bedhouin’s method, and I will be switching to it in our next project.

Notes have been made.

[lua]globals = {};
globals.gameState = 1;
globals.isPaused = false;

function CleanUpGlobals()
for i=0, #globals do
globals[i] = nil;
end
globals = nil;
end[/lua] [import]uid: 5317 topic_id: 11408 reply_id: 41360[/import]