Odd Global Variable 'texthighlight' Created After Creating Widget,newbutton Object

I was checking the variables in my game and I noticed that there is a global variable called ‘TextHighlight’. After a lot of searching I found it was being created right after calling widget.newButton() to build a custom buttom. I ran a search on my project and I don’t have any variables called TextHighlight.

If I roll back to daily build 1043 the variable is not there at the return statement below, if I update to build 1049 or higher (currently using 1072) then the global variable is there.

I am using Lua Glider 1.7.2 as my IDE

I doubt the variable is causing any problems but I am curious about it, and I really don’t like having global various lingering.

Here is the code that is creating the button:

 [lua]

local getButton_basic = function( listenerFunction )

   local button

   if listenerFunction  then

      button = widget.newButton{left = screenWidth*0.35, top = screenHeight*0.4, 

                                       width = screenHeight*0.2, height = screenWidth*0.2,

                                       defaultFile = graphicsPath…“button_normal.png”,

                                       overFile = graphicsPath…“button_pressed.png”,

                                       onEvent = listenerFunction }

   else

      button = widget.newButton{left = screenWidth*0.35, top = screenHeight*0.4, 

                                       width = screenHeight*0.2, height = screenWidth*0.2,

                                       defaultFile = graphicsPath…“button_normal.png”,

                                       overFile = graphicsPath…“button_pressed.png” }       

   end

   return button

end

[/lua]

Any clues as to what this is would be great. My guess is that it’s just a minor bug in Corona’s code, since I don’t see any other globals coming from the API (besides physics).

Thanks!

-Treb

Widgets 2.0 introduced some new globals that would be nice if they were not globals.  Here are a few that i have run into so far … wrapped in my declare statements so that the global check does not barf:

declare(“themeTable”)

declare(“textHighlight”)

declare(“viewLabel”)

declare(“mask”)

Damn that’s too bad, I was hoping it was just them forgetting to include ‘local’ before a variable somewhere.

I hope they find a way to get by without using those.

What does the declare("") do? I apologize for making this off-topic but I haven’t seen that before.

Thanks!

they probably did just forget some local or perhaps there is some bad code where they should be passing variables … who knows.

The declare is just part of the logic in my app that you have to use to create a global … else the app tosses an exception.  Helps me make sure i don’t do what you have discovered in the Widgets.

Found the code for it in the community share somewhere quite a while back:

Put it at the top of your main … you may want to just comment the setmetatable part out for production.

------------------------------------------------------------ ------------------------------------------------------------ --Force the use of declare("globalvariablename") if you want to use globals -- this will cut down on their use or misuse (unintended use of globals) -- local declaredNames = {} function declare (name, initval) rawset(\_G, name, initval) declaredNames[name] = true end setmetatable(\_G, { \_\_newindex = function (t, n, v) if not declaredNames[n] then error("attempt to write to undeclared var. "..n, 2) else rawset(t, n, v) -- do the actual set end end, \_\_index = function (\_, n) if not declaredNames[n] then error("attempt to read undeclared var. "..n, 2) else return nil end end, })

Wow that is a great little bit of code, going to save me some time and mistakes :slight_smile:

Appreciate your help mslack!

Nice.  @mslack, you might want to post it on handy code snippet thread:

http://forums.coronalabs.com/topic/15731-handy-code-snippets/

It would help many others. 

Naomi

Thanks for pointing out these global variables. They are unintended and will be rectified asap.

Thanks again.

Widgets 2.0 introduced some new globals that would be nice if they were not globals.  Here are a few that i have run into so far … wrapped in my declare statements so that the global check does not barf:

declare(“themeTable”)

declare(“textHighlight”)

declare(“viewLabel”)

declare(“mask”)

Damn that’s too bad, I was hoping it was just them forgetting to include ‘local’ before a variable somewhere.

I hope they find a way to get by without using those.

What does the declare("") do? I apologize for making this off-topic but I haven’t seen that before.

Thanks!

they probably did just forget some local or perhaps there is some bad code where they should be passing variables … who knows.

The declare is just part of the logic in my app that you have to use to create a global … else the app tosses an exception.  Helps me make sure i don’t do what you have discovered in the Widgets.

Found the code for it in the community share somewhere quite a while back:

Put it at the top of your main … you may want to just comment the setmetatable part out for production.

------------------------------------------------------------ ------------------------------------------------------------ --Force the use of declare("globalvariablename") if you want to use globals -- this will cut down on their use or misuse (unintended use of globals) -- local declaredNames = {} function declare (name, initval) rawset(\_G, name, initval) declaredNames[name] = true end setmetatable(\_G, { \_\_newindex = function (t, n, v) if not declaredNames[n] then error("attempt to write to undeclared var. "..n, 2) else rawset(t, n, v) -- do the actual set end end, \_\_index = function (\_, n) if not declaredNames[n] then error("attempt to read undeclared var. "..n, 2) else return nil end end, })

Wow that is a great little bit of code, going to save me some time and mistakes :slight_smile:

Appreciate your help mslack!

Nice.  @mslack, you might want to post it on handy code snippet thread:

http://forums.coronalabs.com/topic/15731-handy-code-snippets/

It would help many others. 

Naomi

Thanks for pointing out these global variables. They are unintended and will be rectified asap.

Thanks again.