Has anyone ever had issues with a Boolean global variable getting reset? I have a global in main.lua that I set to false at the top of the file. Then when I initialize game network on a success I set it to true. Then later in menu.lua (a storyboard scene) I check if that variable is true to show the global leaderboards or if false a local leaderboard. Works the first time I hit the button in the menu. But the next time I hit the button the value has been reset to false. Weird. I can post code if no one has any ideas. I even tried putting the Boolean in _G and storyboard. Neither worked.
I have had similar thing happen to me but I was using a reserved variable name without realising (variable name was ‘status’).
Could that be it? What is the name?
loggedIntoGC. I think this was from the game network examples. Wonder if they use it internally. But I would have thought adding this to storyboard would have worked.
Yeah that sounds unlikely.
I’m stumped. Sounds like you are using it as a switch on a button so all I can think of is that perhaps you forgot to check phase in the button event listener function and its getting fired multiple times on touch…
Doing:
fred = “Flintstone”
and
_G.fred = “Flintstone”
are the exact same. The first instance adds “fred” to _G automatically, the second instance, you can access the variable fred without the _G. We did a blog post a couple of weeks ago on the problems of using globals. You don’t know what other libraries are doing and you can’t assume that any global you create is safe even with the _G prefix.
Now in theory, adding your variable to the storyboard object kind of gets around this problem, but then again, if we change storyboard, we might overwrite your variable. In the blog post, we offered a completely safe way to have variables that are available to all your modules without going global. Please see:
http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
In this tutoral we recommend creating a module called “myData” that holds your global data. You of course could call this file: my_G.lua do:
local my_G = require(“my_G”)
in each of your modules and then do a global search/replace on your code converting any _G’s to my_G to quickly fix code that has been dependent on _G.
@Rob thanks for the explanation. ya, I saw that blog entry. i’ll try the my_G approach. but the fact that putting it into storyboard and it got reset from true to false between the time I hit the button that opened game network and then close game network, tells me there is something else strange going on.
Here’s my code that is having issues:
[lua] local function showGameCenter(event)
if (storyboard.loggedIntoGC) then
native.showAlert(“loggedIntoGC”, “true”, { “OK” } )
else
native.showAlert(“loggedIntoGC”, “false”, { “OK” } )
end
if (storyboard.loggedIntoGC) then
if (system.getInfo(“platformName”) == “Android”) then
gameNetwork.show( “leaderboards” )
else
gameNetwork.show( “leaderboards”, { leaderboard={ category=“highscore”, timeScope=“Week” } } )
end
else
// show local score boards
end
end
[/lua]
showGameCenter is triggered by a button:
[lua]
local button4 = widget.newButton{
id = “scores”,
sheet = buttonSheet,
defaultFrame = 7,
overFrame = 8,
onRelease = showGameCenter
}
[/lua]
the first time I press the button loggedIntoGC is true (set true earlier by my login code). The next time the button is pressed it’s been set to false. By what I do not know.
Wow, it gets stranger. If I comment out line 10 above ( gameNetwork.show( “leaderboards” ) ) the loggedIntoGC value remains true. What in gameNetwork.show could possibly be wiping out my boolean?
So I can get it to work by doing the following (where loggedIn is a local to menu.lua):
[lua]
local function showGameCenter(event)
if (loggedIn == nil) then
loggedIn = storyboard.loggedIntoGC
end
if (loggedIn) then
if (system.getInfo(“platformName”) == “Android”) then
gameNetwork.show( “leaderboards” )
else
gameNetwork.show( “leaderboards”, { leaderboard={ category=“highscore”, timeScope=“Week” } } )
end
[/lua]
In other words, saving away the original global in a local remembers the setting. Don’t like this at all and I wish I could figure out what the root cause was.
Weird
@Rob Ya, I wish I had an explanation… I’ve put logging EVERYWHERE that storyboard.loggedIntoGC is set and it’s only set to TRUE once during initialization. But unless I save it away in the local variable, it gets lost. I might be misunderstanding something about the Lua language. I’ll post if I find anything further out.
I can see the gameNetwork module changing a global but there is no reason for it to be modifying anything in the storyboard object. Have you tried renaming the variable to something else?
yes, I’ve tried renaming it with no difference
If you’re renamed it to something else, then your code is resetting it somewhere. The chances of a module hitting two different variable names is pretty slim and since you are also putting it in another table too, that would protect against any globals, so what’s left is something in your code resetting it.
I have had similar thing happen to me but I was using a reserved variable name without realising (variable name was ‘status’).
Could that be it? What is the name?
loggedIntoGC. I think this was from the game network examples. Wonder if they use it internally. But I would have thought adding this to storyboard would have worked.
Yeah that sounds unlikely.
I’m stumped. Sounds like you are using it as a switch on a button so all I can think of is that perhaps you forgot to check phase in the button event listener function and its getting fired multiple times on touch…
Doing:
fred = “Flintstone”
and
_G.fred = “Flintstone”
are the exact same. The first instance adds “fred” to _G automatically, the second instance, you can access the variable fred without the _G. We did a blog post a couple of weeks ago on the problems of using globals. You don’t know what other libraries are doing and you can’t assume that any global you create is safe even with the _G prefix.
Now in theory, adding your variable to the storyboard object kind of gets around this problem, but then again, if we change storyboard, we might overwrite your variable. In the blog post, we offered a completely safe way to have variables that are available to all your modules without going global. Please see:
http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/
In this tutoral we recommend creating a module called “myData” that holds your global data. You of course could call this file: my_G.lua do:
local my_G = require(“my_G”)
in each of your modules and then do a global search/replace on your code converting any _G’s to my_G to quickly fix code that has been dependent on _G.
@Rob thanks for the explanation. ya, I saw that blog entry. i’ll try the my_G approach. but the fact that putting it into storyboard and it got reset from true to false between the time I hit the button that opened game network and then close game network, tells me there is something else strange going on.
Here’s my code that is having issues:
[lua] local function showGameCenter(event)
if (storyboard.loggedIntoGC) then
native.showAlert(“loggedIntoGC”, “true”, { “OK” } )
else
native.showAlert(“loggedIntoGC”, “false”, { “OK” } )
end
if (storyboard.loggedIntoGC) then
if (system.getInfo(“platformName”) == “Android”) then
gameNetwork.show( “leaderboards” )
else
gameNetwork.show( “leaderboards”, { leaderboard={ category=“highscore”, timeScope=“Week” } } )
end
else
// show local score boards
end
end
[/lua]
showGameCenter is triggered by a button:
[lua]
local button4 = widget.newButton{
id = “scores”,
sheet = buttonSheet,
defaultFrame = 7,
overFrame = 8,
onRelease = showGameCenter
}
[/lua]
the first time I press the button loggedIntoGC is true (set true earlier by my login code). The next time the button is pressed it’s been set to false. By what I do not know.